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