1

Ok... this is what I want to do.....

  1. Generate Java classes from some expression language....
  2. Assume Step#1 is done... I have Java file. I have created JavaLaxer and JavaParser using Java.g file after modifying Java.g little bit (http://jumbleagilemanuals.blogspot.com/2008/03/10-steps-to-beginning-to-parsing-with.html)
  3. I am using antlr-3.3-complete.jar for obtaining AST tree. Now I have wrote Test class where I am obtaining CommonTree object (see the code below this list)...
  4. Now I want to convert AST tree to XML output so that I can save it in DB...
  5. I want to write Reader class which can read XML file and create Java source..
  6. Use that Java Source to create Object at Runtime...


static final TreeAdaptor adaptor = new CommonTreeAdaptor() {      
    @Override
    public Object create(Token payload) {     
        CommonTree cTree = new CommonTree();
        cTree.token = payload;
        return cTree;        
    }    
};

CharStream c = new ANTLRFileStream(
                "C:/Documents and Settings/kkk/IBM/rationalsdp/workspace17"+
                "/myproject/src/main/java/com/xyz/abc/infrastructure/"+
                "email/service/impl/EmailServiceImpl.java");

JavaLexer lexer = new JavaLexer(c);

CommonTokenStream tokens = new CommonTokenStream();        
tokens.setTokenSource(lexer);

JavaParser parser = new JavaParser(tokens);
parser.setTreeAdaptor(adaptor);

/* AST tree for my java code */
CommonTree tree = (CommonTree) parser.compilationUnit().getTree();  

I am struggling with step-4 to step-6... I couldn't find solution yet!!

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Here is what I want to do... First I want to convert Java File into some XML and store it in DB. Then I want to read back my XML file and convert that XML file to Java File.... Then I want to compile my Java file and create Object at runtime.... Reason why I want to do all these is because We want to have ability to Inject new Java Objects at Runtime without server restart. We might also end up changing functionality for existing Java Object. I thought ANTLR is right way to go about it...but I am struggling by not finding solution for it. – Bakul Brahmbhatt Apr 06 '11 at 18:35
  • There isn't much reason to do this if all you want to do is "inject" new objects that have identical trees; you could just use the original text. So the real issue here is reading one of the XML trees, and deciding where to make a change, and make that change. How do you plan to do that? – Ira Baxter Apr 06 '11 at 22:43

2 Answers2

1

I'm not an ANTLR expert.

However, step 4 should be pretty straightforward. Define a visitor to walk the AST. For each tree node, write out:

  <node type="*type*" value="*value*"> *child1* ... *childn* </node>

where the child_i is the XML for the subtypes. (We do almost exactly this for an ANTLR-like system that I offer commercially; this is built-in).

For step 5, you'll need an XML reader, and knowledge of how to construct ANTLR nodes. Read the generated text and build an XML tree. Walk that tree, and from bottom up, construct ANTLR nodes by calling ANTLR node constructors.

Presumably ANTLR will prettyprint a Java tree that it has, solving 6).

Presumably after prettyprinting, you'll have to run javac on the generated file, and then cause that file to be class-loaded.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • Seems that I don't quite have it right. ANTLR doesn't provide any specific capability to prettyprint a tree. You can write your own prettyprinter, by walking the tree and spitting out tet. That turns out to be more complicated than it sounds. See my SO answer http://stackoverflow.com/a/5834775/120163 – Ira Baxter Jun 20 '14 at 04:24
0

@Ira, Thank you for answer. I will try it out tomorrow and let you know how it goes. Most probably I will not have to traverse Tree and change nodes but that is a possibility. Business requirements are not pretty clear yet so I am not sure how I am going to implement dynamic code generation. It could be combination of below techniques...

1) Generating AST Tree, Saving in DB as XML, on need retrieve back XML, generate AST tree, modify it if needed some changes, Convert AST tree to Java Source, Compile Java Source to class, Load object in memory at runtime and use it.

2) Implement Java class which extends Interface.. Implement new Impl using RAD IDE... Convert that Impl AST tree, save in db as xml, on need retrieve back as xml, generate AST tree, Convert AST tree to Java source, compile Java source to class by help of parent interface implemenation, Load object in memory by help of Factory pattern ( which may either load original IMPL or new IMPL created in this step)

I am newbee to ANTLR so it is quite difficult to understand how it works.. I was hoping once I convert Java to AST tree, I might able to serialize it easily... I was also hoping that there must be simple way to retrieve back AST serialize object and create AST tree in memory and create Java source from it easily. I hate the fact that there are not many example available to use it.