• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Code Parser

 
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Carlos A. Perez
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 32
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic