2
* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
27
* @summary Compiler Tree API TreePath class generates NullPointerException from Iterator
28
* @modules jdk.compiler
29
* @compile T6855236.java
30
* @compile -processor T6855236 -proc:only T6855236.java
35
import javax.annotation.processing.*;
36
import javax.lang.model.*;
37
import javax.lang.model.element.*;
39
import com.sun.source.tree.*;
40
import com.sun.source.util.*;
42
@SupportedAnnotationTypes("*")
43
public class T6855236 extends AbstractProcessor {
48
public void init(ProcessingEnvironment pe) {
50
trees = Trees.instance(pe);
54
public boolean process(Set<? extends TypeElement> arg0, RoundEnvironment roundEnvironment) {
55
// Scanner class to scan through various component elements
56
CodeVisitor visitor = new CodeVisitor();
58
for (Element e : roundEnvironment.getRootElements()) {
59
TreePath tp = trees.getPath(e);
60
visitor.scan(tp, trees);
67
public SourceVersion getSupportedSourceVersion() {
68
return SourceVersion.latest();
71
class CodeVisitor extends TreePathScanner<Object, Trees> {
74
public Object visitMethodInvocation(MethodInvocationTree node, Trees p) {
75
System.out.println("current path: ");
76
for (Tree t : getCurrentPath()) {
77
System.out.println(" " + t.getKind() + ": " + trim(t, 64));
79
System.out.println("parent path: " + getCurrentPath().getParentPath());
80
System.out.println("method select: " + node.getMethodSelect().toString());
81
for (ExpressionTree arg : node.getArguments()) {
82
System.out.println("argument: " + arg.toString());
84
return super.visitMethodInvocation(node, p);
88
public Object visitExpressionStatement(ExpressionStatementTree node, Trees p) {
89
ExpressionTree t = node.getExpression();
91
System.out.println("expression statement: " + trim(t, 64));
92
return super.visitExpressionStatement(node, p);
97
private String trim(Tree t, int len) {
98
String s = t.toString().trim().replaceAll("\\s+", " ");
100
s = s.substring(0, len) + "...";