Need help on designning text utilities tool...

M

mfazdlie

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
 
P

Proton Projects - Moin

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
 
M

Mark Space

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?
 
M

Martin Gregorie

Mark said:
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.
 
M

Mark Space

JT said:
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....
 
M

MFO

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
 
M

Martin Gregorie

MFO said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top