views:

109

answers:

5

I am trying to edit a class by parsing it, adding some information to it and then writing it back. I was wondering if there is a tool (similar to something like Doclet (well that uses that same API as it seems to have what I am looking for)) that could accomplish this task?

By this I mean that I would like to take a java source file, load it into a (for example) ClassDoc object and then add some content to it. and from that, write it all back to a file. I will be able to manage the writing back part myself as it is a simple matter of looking at the content and writing it to an ASCII file in a certain format but I am unsure of how to load a java source file into a class like ClassDoc and then how do I edit is content?

Thanks for any pointers on this one,

ExtremeCoder

A: 

Do you have the .class file, or just the .html javadoc output?

If you have the .class file, you can decompile with a utility like JODE, edit the file, then perhaps recompile again if you have all the dependencies.

If you only have the javadoc output, you don't have any of the code, you just have the documentation of what methods are available in the code. Adding something to that and then making it work isn't really possible; all of the details are gone.

Dean J
I have the .java file and I would like to load that not the compiled class. Is that possible?
ExtremeCoder
See @Kent's answer; they seem to have got this right, if you're asking what I think you're asking.
Dean J
+1  A: 

I guess you are trying to dynamically compile a java source file, and load the compiled class.

If my understanding was right, you may want to check out a couple of methods :

for dynamic compiling:

com.sun.tools.javac.Main
public static int compile(String as[]);
public static int compile(String as[], PrintWriter printwriter);

for dynamic class loading: i.e.

URLClassLoader loader=new URLClassLoader(new URL[]{new URL("file:/foo/bar")});

Object model=loader.loadClass("MyClass").newInstance();
Kent
No this is not really what I am trying to do. I have the .java file and I would like to load that not the compiled class. Is that possible?
ExtremeCoder
what do you mean load the .java file? I guess you want to load the source code, do the change, save back (as .java), then compile to .class and load the class. right?
Kent
exactly. that's pretty much it :)
ExtremeCoder
then you can load the .java as normal text file. write some changes to it, and save. then using the compile() method in my post, to compile it. and load the .class by URLClassLoader.
Kent
That's a great answer, Kent; welcome to the site!
Dean J
A: 

Well, let's not use the complicator's glove here - if you only want to add content to a source file, you could always load the file in a String[0], and add the desired content right before the last } character in that file.

OTOH, have you though about load-time bytecode weaving? F.E. something like Javassist, which can help you modify classes before they are loaded.

[0] Edit: a simple FileInputReader will suffice, as it's a plain text file...

Tassos Bassoukos
I will want to edit the content too... I will need to remove some methods and edit the signature lines of some variables and parsing the file can be quite tedious so I am looking for a more ready-made solution to my problem. And I really think that this problem needs to be tackled at the source-code level and not the compiled file.
ExtremeCoder
A: 

You need to understand basic file I/O.

Start Here: http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/essential/io/index.html

Write some meta-data to help you keep up with where your changes are added / removed

class MyClass {
     private Integer age;

     public Integer getAge() { return age; }

     /**[INJECTED]**/
       ... custom stuff here...
     /**[END_INJECTED]**/
}

I'm not really sure the way you're doing this is the best approach to take, but it will be a learning experience, nonetheless.

Derrick
A: 

I have resolved this in another way. Refer to this for more information: http://stackoverflow.com/questions/3351451/using-the-eclipse-ast

ExtremeCoder