Does anyone know about a Java Code Parser? i need to know how many methods a class has, the fields, etc, everything form the code of a .java. I know i could do it by reflection but i need to manipulate the code (add or remove methods/fields etc) thats why i need a parser for the code.
Javassist is almost exactly what i want the problem is that it works with classes and y need it to work with sources (.java). Webpage says that has two levels of API, source and bytecode but i cant find any class to load, modify and write source files. The tutorial and the api never says something about source files.
Joined: Mar 22, 2005
Hmmm, I've never noticed any mention of Javassist being able to work with source files, and I don't think it can do that. Where'd you see that?
Would it be an option to compile the source files (which can be done programmatically), and then work with the class files?
Carlos A. Perez
Joined: Apr 09, 2004
The webpage says:
Javassist (Java Programming Assistant) makes Java bytecode manipulation simple. It is a class library for editing bytecodes in Java; it enables Java programs to define a new class at runtime and to modify a class file when the JVM loads it. Unlike other similar bytecode editors, Javassist provides two levels of API: source level and bytecode level. If the users use the source-level API, they can edit a class file without knowledge of the specifications of the Java bytecode. The whole API is designed with only the vocabulary of the Java language. You can even specify inserted bytecode in the form of source text; Javassist compiles it on the fly. On the other hand, the bytecode-level API allows the users to directly edit a class file as other editors.
But i still havent found any class to work with sources, im gonna try another aproach instead of what i wanted to do. im thinking seriously about starting an own project to have clases to work with surce code as i wanted to work with i think it could be usefull to create source code or modify source code in the same way Javassist does with byte codes.
Joined: Mar 22, 2005
Well, it has a source-level API, but that's different from saying it can deal with source files. Specifically, what it can do is add methods and fields to a class based on Java source fragments. You can give it the source to a method, and it will compile that to bytecode and add it to a class. But it can't create a whole class from a .java source file.
I've been successful with AntLR. They have a Java grammar that I modified to add SCM tokens to my Java source files and write them back out. There was an article on their site about modifying source files. Terrence PArr create a token stream rewrite engine of some sort to handle modifying existing source. In general using a parser generator like JavaCC or AntLR to generate a source code augmentor can lead to file corruption due to whitespace and other tokens. However, I found that with AntLR using the article as a guide, it was relatively easy to write a parser that can add to or remove from Java source. Use Google to search the AntLR site for "token rewrite engine" or I'll try to dig the article up for you if necessary.
Holla at me...<br /><a href="http://codeforfun.wordpress.com" target="_blank" rel="nofollow">http://codeforfun.wordpress.com</a>
There is something called JAVAML - XML for Java source code. An XML representation is very helpful for extracting the details out of the source code. We're often confronted with the need to massively refactor huge amount of code based on syntactic contexts. Being able to express those as XPath expressions or XSLT templates makes this otherwise complex task much, much easier. I have everything you need to be able to parse java source code into xml and then be able to programmatically parse generated xml and be able to modify, put it into database or whatever. You can google JAVAML and use it's dtd, xsl along with JavaCC to accomplish what you want to do or contact me at firstname.lastname@example.org for more information.