aspose file tools*
The moose likes Java in General and the fly likes Java Code Parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Code Parser" Watch "Java Code Parser" New topic
Author

Java Code Parser

Carlos A. Perez
Greenhorn

Joined: Apr 09, 2004
Posts: 27
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42599
    
  65
Manipulating a class in this way can be done with the Javassist library (on SourceForge), which works with existing class files. It's very cool, and eliminates the need for compilation.

If you really need a parser, check out JavaCC, Antlr and SableCC, which are parser generators, and for each of which a predefined Java grammar is available.


Ping & DNS - my free Android networking tools app
Carlos A. Perez
Greenhorn

Joined: Apr 09, 2004
Posts: 27
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42599
    
  65
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
Greenhorn

Joined: Apr 09, 2004
Posts: 27
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.

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42599
    
  65
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.
Clifton Craig
Ranch Hand

Joined: May 26, 2006
Posts: 103
Carlos,

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>
Nimesh Patel
Greenhorn

Joined: Aug 13, 2008
Posts: 1
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 npatel32@gmail.com for more information.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
DontWakeTheZombies

The OP will probably have got an answer or given up sometime in the last two years.

And if you want to advertise, use the Blatant Advertising forum.
[ August 13, 2008: Message edited by: Joanne Neal ]

Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Code Parser