aspose file tools*
The moose likes Java in General and the fly likes Any idea abt how to modify a java source code @ compile time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Any idea abt how to modify a java source code @ compile time?" Watch "Any idea abt how to modify a java source code @ compile time?" New topic
Author

Any idea abt how to modify a java source code @ compile time?

ragunath Mariappan
Greenhorn

Joined: Oct 29, 2007
Posts: 8
Hi All!!

I would like to modify the java source code @ compile time, like

- removing certain statements in the source file
(say for example all the System.out.println statements).
Also i would like to have a copy of the modified source file (i.e. the source file with all the System.out.println statements removed)

- similarly, adding certain statements


Are there any libraries which could be used to modify the java source code (like javassist which modifies the bytecode) ?

Can any suggest any ways by which this could be done?

Thanks in advance!

ragu
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
I think it's more likely that you want to change your logging strategy.

Check out log4j.

There are other logging frameworks as well.

You could write a full-on parser, and modify your build process to pre-process code but you really don't want to.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
ragunath Mariappan
Greenhorn

Joined: Oct 29, 2007
Posts: 8
Hi bill,

Thanks for the suggestion.

But my intention is not to change the logging strategy,
rather i would like,

- to 'add' certain code to certain methods which are annotated with my custom annotations,...
say for example by annotating a method with @computetime (my custom annotation), then i would like to include a generic code which computes the time taken to execute the method.

- to 'delete' or 'modify' the source code of a method depending on how its annotated,...
say for example if a method is annotated with @removeprints(my custom annotation), then i would like to remove all the System.out.println statements in the source file.

- In both above cases i would like to have the modified source file.

Hope this could have made my intention clearer,

I welcome any further suggestions to have it done...

Thanks in advance!!

-ragu
Mike Anna
Ranch Hand

Joined: Jul 08, 2007
Posts: 117
Generaly, its good to mention the kind of environment you are working in.
Now even if I want to answer, I dont know if you are working on spring or no. you see.
- to 'add' certain code to certain methods which are annotated with my custom annotations,...
say for example by annotating a method with @computetime (my custom annotation), then i would like to include a generic code which computes the time taken to execute the method.

This sure is possible using AOP in spring, other wise AspectJ.
- to 'delete' or 'modify' the source code of a method depending on how its annotated,...
say for example if a method is annotated with @removeprints(my custom annotation), then i would like to remove all the System.out.println statements in the source file.


Havent deleted material like this. But if you are thinking of searching for a library which would assist you in code clean up (removing SOPs ..why did you put em in anyway), then I am not aware of any.

- In both above cases i would like to have the modified source file.


To achieve both the cases, by golly, you would have them anyway!! :-)

HTH
ragunath Mariappan
Greenhorn

Joined: Oct 29, 2007
Posts: 8
Thanks Mike !

That really helped.
 
 
subject: Any idea abt how to modify a java source code @ compile time?