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
Proton Projects - Moin - 25 Jun 2007 05:07 GMT
On Jun 25, 7:56 am, mfazd...@gmail.com wrote:
> Hello to all,
>
[quoted text clipped - 51 lines]
>
> 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
Mark Space - 25 Jun 2007 06:23 GMT
> The program search line by line text... found the class.. then take
> the name class.. then search again.. until found ' { ' ( braclet/
[quoted text clipped - 6 lines]
>
> 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?
Martin Gregorie - 25 Jun 2007 13:59 GMT
> 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.

Signature
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
Mark Space - 25 Jun 2007 22:11 GMT
>> 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.
JT - 25 Jun 2007 23:10 GMT
>>> 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
[quoted text clipped - 3 lines]
>
> 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?
Mark Space - 26 Jun 2007 01:11 GMT
> 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....
MFO - 26 Jun 2007 03:15 GMT
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
Martin Gregorie - 26 Jun 2007 13:13 GMT
> 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.

Signature
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |