457N/A/*
553N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
457N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
457N/A *
457N/A * This code is free software; you can redistribute it and/or modify it
457N/A * under the terms of the GNU General Public License version 2 only, as
457N/A * published by the Free Software Foundation.
457N/A *
457N/A * This code is distributed in the hope that it will be useful, but WITHOUT
457N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
457N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
457N/A * version 2 for more details (a copy is included in the LICENSE file that
457N/A * accompanied this code).
457N/A *
457N/A * You should have received a copy of the GNU General Public License version
457N/A * 2 along with this work; if not, write to the Free Software Foundation,
457N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
457N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
457N/A */
457N/A
457N/A/*
457N/A * @test
457N/A * @bug 6855236
457N/A * @summary Compiler Tree API TreePath class generates NullPointerException from Iterator
457N/A * @compile T6855236.java
457N/A * @compile -processor T6855236 -proc:only T6855236.java
457N/A */
457N/A
457N/Aimport java.util.*;
457N/A
457N/Aimport javax.annotation.processing.*;
457N/Aimport javax.lang.model.*;
457N/Aimport javax.lang.model.element.*;
457N/A
457N/Aimport com.sun.source.tree.*;
457N/Aimport com.sun.source.util.*;
457N/A
457N/A@SupportedAnnotationTypes("*")
457N/Apublic class T6855236 extends AbstractProcessor {
457N/A
457N/A private Trees trees;
457N/A
457N/A @Override
457N/A public void init(ProcessingEnvironment pe) {
457N/A super.init(pe);
457N/A trees = Trees.instance(pe);
457N/A }
457N/A
457N/A @Override
457N/A public boolean process(Set<? extends TypeElement> arg0, RoundEnvironment roundEnvironment) {
457N/A // Scanner class to scan through various component elements
457N/A CodeVisitor visitor = new CodeVisitor();
457N/A
457N/A for (Element e : roundEnvironment.getRootElements()) {
457N/A TreePath tp = trees.getPath(e);
457N/A visitor.scan(tp, trees);
457N/A }
457N/A
457N/A return true;
457N/A }
457N/A
494N/A @Override
494N/A public SourceVersion getSupportedSourceVersion() {
494N/A return SourceVersion.latest();
494N/A }
494N/A
457N/A class CodeVisitor extends TreePathScanner<Object, Trees> {
457N/A
457N/A @Override
457N/A public Object visitMethodInvocation(MethodInvocationTree node, Trees p) {
682N/A System.out.println("current path: ");
457N/A for (Tree t : getCurrentPath()) {
682N/A System.out.println(" " + t.getKind() + ": " + trim(t, 64));
682N/A }
457N/A System.out.println("parent path: " + getCurrentPath().getParentPath());
457N/A System.out.println("method select: " + node.getMethodSelect().toString());
457N/A for (ExpressionTree arg : node.getArguments()) {
457N/A System.out.println("argument: " + arg.toString());
457N/A }
457N/A return super.visitMethodInvocation(node, p);
457N/A }
457N/A
457N/A @Override
457N/A public Object visitExpressionStatement(ExpressionStatementTree node, Trees p) {
457N/A ExpressionTree t = node.getExpression();
682N/A System.out.println();
682N/A System.out.println("expression statement: " + trim(t, 64));
457N/A return super.visitExpressionStatement(node, p);
457N/A }
457N/A
457N/A }
457N/A
682N/A private String trim(Tree t, int len) {
682N/A String s = t.toString().trim().replaceAll("\\s+", " ");
682N/A if (s.length() > len)
682N/A s = s.substring(0, len) + "...";
682N/A return s;
682N/A }
682N/A
457N/A}
457N/A
457N/A