1 /*
   2  * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6855236
  27  * @summary Compiler Tree API TreePath class generates NullPointerException from Iterator
  28  * @compile T6855236.java
  29  * @compile -processor T6855236 -proc:only T6855236.java
  30  */
  31 
  32 import java.util.*;
  33 
  34 import javax.annotation.processing.*;
  35 import javax.lang.model.*;
  36 import javax.lang.model.element.*;
  37 
  38 import com.sun.source.tree.*;
  39 import com.sun.source.util.*;
  40 
  41 @SupportedSourceVersion(SourceVersion.RELEASE_6)
  42 @SupportedAnnotationTypes("*")
  43 public class T6855236 extends AbstractProcessor {
  44 
  45     private Trees trees;
  46 
  47     @Override
  48     public void init(ProcessingEnvironment pe) {
  49         super.init(pe);
  50         trees = Trees.instance(pe);
  51     }
  52 
  53     @Override
  54     public boolean process(Set<? extends TypeElement> arg0, RoundEnvironment roundEnvironment) {
  55         // Scanner class to scan through various component elements
  56         CodeVisitor visitor = new CodeVisitor();
  57 
  58         for (Element e : roundEnvironment.getRootElements()) {
  59             TreePath tp = trees.getPath(e);
  60             visitor.scan(tp, trees);
  61         }
  62 
  63         return true;
  64     }
  65 
  66     class CodeVisitor extends TreePathScanner<Object, Trees> {
  67 
  68         @Override
  69         public Object visitMethodInvocation(MethodInvocationTree node, Trees p) {
  70             System.out.print("current path: ");
  71             for (Tree t : getCurrentPath()) {
  72                 System.out.print('/');
  73                 System.out.print(t);
  74            }
  75             System.out.println();
  76             System.out.println("parent path: " + getCurrentPath().getParentPath());
  77             System.out.println("method select: " + node.getMethodSelect().toString());
  78             for (ExpressionTree arg : node.getArguments()) {
  79                 System.out.println("argument: " + arg.toString());
  80             }
  81             return super.visitMethodInvocation(node, p);
  82         }
  83 
  84         @Override
  85         public Object visitExpressionStatement(ExpressionStatementTree node, Trees p) {
  86             ExpressionTree t = node.getExpression();
  87             System.out.println("expression statement: " + t.toString());
  88             return super.visitExpressionStatement(node, p);
  89         }
  90 
  91     }
  92 
  93 }
  94 
  95