2 Replies Latest reply: Apr 1, 2013 1:06 AM by 999156 RSS

    JDK 1.6: Annotation Processing & Compiler Hack


      I am currently using JDK 1.6.0_23
      For one of the requirement, I am going with annotation processing & compiler Hack using Tools.jar available as part of JDK.

      I have refered a PDF - "The Hacker's Guide to javac (PDF) by David Erni and Adrian Kuhn (2008)
      " suggested from page of - http://openjdk.java.net/groups/compiler/

      My requirement is below ->

      Origional Source:
      public void someMethod() {
      Modified Source:
      public void someMethod() {
         int items = new example.Product().getItems();
      Below is my code to generate the variable declaration -
      private TreeMaker make; // fetch it from somewhere
      JCNewClass newProduct = make.NewClass(null, List.<JCExpression>nil(), make.Ident(names.fromString("example.Product")), List.<JCExpression>nil(), null);
      JCFieldAccess fieldAccess = make.Select(newProduct, names.fromString("getItems"));
      JCMethodInvocation getTimeMethodInvocation = make.Apply(List.<JCExpression>nil(), fieldAccess, List.<JCExpression>nil());
      expression = getTimeMethodInvocation;
      JCVariableDecl itemsDeclaration = make.VarDef(modifiers,name,varType,expression);
      System.out.println(itemsDeclaration); // this prints int items = new example.Product().getItems(); 
      This itemsDeclaration, I am adding to a List<JCStatement> of JCBlock of JCMethodDecl.

      However modified code does not compile :(

      If I make below changes - Modified does compile

      JCNewClass newProduct = make.NewClass(null, List.<JCExpression>nil(), make.Ident(names.fromString("Product")), List.<JCExpression>nil(), null);
      Product insteadof example.Product

      2) Add belwo statement in the origional source code
      import examle.Product; 
      What exactly am I missing here ???

      The AST tree is diffcult to understand with minimum documentation & without much help on the interent.

      I hope this is correct forum, for my query.
      It will be a great help.

      Vikas Parikh
        • 1. Re: JDK 1.6: Annotation Processing & Compiler Hack
          996153 wrote:
          What exactly am I missing here ???
          Ask the authors of whatever document you're using. If you want help here, tell what you're trying to achieve functionally and perhaps someone can suggest a proper technical solution for it that does not require this kind of "hack" that will be a maintenance nightmare.
          • 2. Re: JDK 1.6: Annotation Processing & Compiler Hack

            I couldn't contact them, as the white papaer didn't conatin any email address / contact info.

            I have investigated myself on this & would like to share with you guys, so that any other developer
            do not have to invest precious time like I have done.

            To create a New Class, you require a

            Below would work, if class already has imported Product class.
            However, if class has not imported the Product class, then there are 2 options -

            1) Create a
            Ident</pre> from <pre>Symbol
            2) Create a
            JCField</pre> from <pre>Name
            I used a later approach.

            Vikas Parikh

            Edited by: 996153 on Mar 31, 2013 11:04 PM