Need help on designning text utilities tool...

Discussion in 'Java' started by mfazdlie@gmail.com, Jun 25, 2007.

  1. Guest

    Hello to all,

    I'm mfazdlie, postgraduate student from malaysia, currently doing a
    research on java tools, my supervisor adviced me to contact you all
    has i am having difficulties in Object Oriented Programming especially
    java programming... I need advice how to build a program that will
    insert a text automatically in java class program/ source code... for
    example...

    i have this java class...


    class mainprog
    {
    public void method1( )
    {
    System.out.println("In method1"); //Inserted automatically by
    another program JAVA or C++

    //bla... bla.. instructions

    System.out.println("End method1"); //Inserted automatically by
    another program JAVA or C++
    }

    public void method2( )
    {
    System.out.println("In method2"); //Inserted automatically by
    another program JAVA or C++

    //bla... bla.. instructions

    System.out.println("End method2"); //Inserted automatically by
    another program JAVA or C++
    }
    }

    Hint:- (Hope this idea help)

    The program search line by line text... found the class.. then take
    the name class.. then search again.. until found ' { ' ( braclet/
    curly bracket) count is = 1 then search again found method.. take
    method name.. then after 1st method identify the ' { ' again count is
    + 1 then insert the [ System.out.println(" In + 'method name'
    "); ]then search again until found ' } ' then count is - 1 and so on,
    before end of the that method, insert [ System.out.println(" End +
    'method name' "); ]

    Really need help on this... no idea how to build this program??

    Your assistance is highly appreciated..

    Desperado...

    MFO
     
    , Jun 25, 2007
    #1
    1. Advertising

  2. On Jun 25, 7:56 am, wrote:
    > Hello to all,
    >
    > I'm mfazdlie, postgraduate student from malaysia, currently doing a
    > research on java tools, my supervisor adviced me to contact you all
    > has i am having difficulties in Object Oriented Programming especially
    > java programming... I need advice how to build a program that will
    > insert a text automatically in java class program/ source code... for
    > example...
    >
    > i have this java class...
    >
    > class mainprog
    > {
    > public void method1( )
    > {
    > System.out.println("In method1"); //Inserted automatically by
    > another program JAVA or C++
    >
    > //bla... bla.. instructions
    >
    > System.out.println("End method1"); //Inserted automatically by
    > another program JAVA or C++
    > }
    >
    > public void method2( )
    > {
    > System.out.println("In method2"); //Inserted automatically by
    > another program JAVA or C++
    >
    > //bla... bla.. instructions
    >
    > System.out.println("End method2"); //Inserted automatically by
    > another program JAVA or C++
    > }
    >
    > }
    >
    > Hint:- (Hope this idea help)
    >
    > The program search line by line text... found the class.. then take
    > the name class.. then search again.. until found ' { ' ( braclet/
    > curly bracket) count is = 1 then search again found method.. take
    > method name.. then after 1st method identify the ' { ' again count is
    > + 1 then insert the [ System.out.println(" In + 'method name'
    > "); ]then search again until found ' } ' then count is - 1 and so on,
    > before end of the that method, insert [ System.out.println(" End +
    > 'method name' "); ]
    >
    > Really need help on this... no idea how to build this program??
    >
    > Your assistance is highly appreciated..
    >
    > Desperado...
    >
    > MFO


    Hi,

    This can be done easily using the java.io packages.
    Try to get the java file as the input
    Using the BufferedReader class exist in the java.io. packages try to
    read the file line by line using readLine() method

    I suggest you not to do new iteration for getting class name or method

    Run the iterator of reading the file top to bottom and you need to
    have simple if statements to check for package, import, class keywords
    and methods...
    Need to have some counter to do increments...and
    finally print the summary...

    This is the fair idea of solving the problem...but you need to handle
    some corner cases like inner classes, anonymous class and runnable
    interface method declarations....

    ~Moin
     
    Proton Projects - Moin, Jun 25, 2007
    #2
    1. Advertising

  3. Mark Space Guest

    wrote:

    > The program search line by line text... found the class.. then take
    > the name class.. then search again.. until found ' { ' ( braclet/
    > curly bracket) count is = 1 then search again found method.. take
    > method name.. then after 1st method identify the ' { ' again count is
    > + 1 then insert the [ System.out.println(" In + 'method name'
    > "); ]then search again until found ' } ' then count is - 1 and so on,
    > before end of the that method, insert [ System.out.println(" End +
    > 'method name' "); ]
    >
    > Really need help on this... no idea how to build this program??


    I may being naive about this but it sounds to me like you are describing
    a parser. I.e., what the compiler does to compile a program. Besides {
    and a count, you'll have to deal with comments (don't count a { if it's
    in a comment), strings (ditto; no {'s within "'s) and possibly several
    more things I've forgot.

    I'd look at a full parser; it'll be easier to modify when you find
    additional complications besides just comments and string (which I'm
    sure there will be; stuff always happens). Maybe use yacc?

    But besides that the idea of making a parser to modify Java and
    instrument it so that methods indicate their entry and exit is interesting.

    Question: What happens if this instrumentation is run twice? Do you
    want to end up with two printlns at the start of each method?

    Question: Why not just use the java.util.logging.Logger.entering()
    method instead of println?
     
    Mark Space, Jun 25, 2007
    #3
  4. Mark Space wrote:
    > I'd look at a full parser; it'll be easier to modify when you find
    > additional complications besides just comments and string (which I'm
    > sure there will be; stuff always happens). Maybe use yacc?
    >

    Hey, this is Java. Use CoCo/R, not lex and yacc.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, Jun 25, 2007
    #4
  5. Mark Space Guest

    Martin Gregorie wrote:
    > Mark Space wrote:
    >> I'd look at a full parser; it'll be easier to modify when you find
    >> additional complications besides just comments and string (which I'm
    >> sure there will be; stuff always happens). Maybe use yacc?
    >>

    > Hey, this is Java. Use CoCo/R, not lex and yacc.
    >
    >


    Sorry, my age is showing. CoCo/R seems like an excellent suggestion.
     
    Mark Space, Jun 25, 2007
    #5
  6. JT Guest

    Mark Space wrote:
    > Martin Gregorie wrote:
    >> Mark Space wrote:
    >>> I'd look at a full parser; it'll be easier to modify when you find
    >>> additional complications besides just comments and string (which I'm
    >>> sure there will be; stuff always happens). Maybe use yacc?
    >>>

    >> Hey, this is Java. Use CoCo/R, not lex and yacc.
    >>
    >>

    >
    > Sorry, my age is showing. CoCo/R seems like an excellent suggestion.

    Ok, so what is CoCo/R?

    http://www.ssw.uni-linz.ac.at/coco/

    What's the reason for doing this?
     
    JT, Jun 25, 2007
    #6
  7. Mark Space Guest

    JT wrote:

    > Ok, so what is CoCo/R?
    >
    > http://www.ssw.uni-linz.ac.at/coco/
    >
    > What's the reason for doing this?


    Compilers and parsers are such a common concept in computer science that
    they have their own specialized implementations and languages.

    Java is a general purpose programming language. For certain
    applications, it's better to use the special purpose tool. CoCo/R is one.

    All this from someone who is not a compiler guy....
     
    Mark Space, Jun 26, 2007
    #7
  8. MFO Guest

    Thank's Guys for your reply..
    I had never learnt this Java Program before.. this is the 1st time...
    so lot of new jargon to me.. i'm appreciate if you guys could share a
    source code of any application related.. so that i can learn myself..
    thru that examples..

    TQ
     
    MFO, Jun 26, 2007
    #8
  9. MFO wrote:
    > Thank's Guys for your reply..
    > I had never learnt this Java Program before.. this is the 1st time...
    > so lot of new jargon to me.. i'm appreciate if you guys could share a
    > source code of any application related.. so that i can learn myself..
    > thru that examples..
    >

    CoCo/R has quite a good PDF manual. Just be sure to download that along
    with the code and executables.

    I was able to use it pretty much straight away, but then I have used lex
    & yacc to solve problems rather than to write compilers. As always, if
    you prefer a book there's an O'Reilly one about lex & yacc that should
    be easily adaptable to learning CoCo/R.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, Jun 26, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. _eee_
    Replies:
    3
    Views:
    506
    Rick Strahl [MVP]
    Feb 28, 2004
  2. Ganesh
    Replies:
    4
    Views:
    388
    Thomas Weidenfeller
    Jan 31, 2005
  3. Marcus Leon

    Ant Utilities?

    Marcus Leon, Feb 4, 2005, in forum: Java
    Replies:
    1
    Views:
    705
    Bryce
    Feb 4, 2005
  4. James Yong
    Replies:
    2
    Views:
    3,657
  5. Mark  Watson
    Replies:
    1
    Views:
    135
    pat eyler
    Apr 4, 2005
Loading...

Share This Page