test/tools/javac/T6855236.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/T6855236.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,105 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6855236
    1.30 + * @summary Compiler Tree API TreePath class generates NullPointerException from Iterator
    1.31 + * @compile T6855236.java
    1.32 + * @compile -processor T6855236 -proc:only T6855236.java
    1.33 + */
    1.34 +
    1.35 +import java.util.*;
    1.36 +
    1.37 +import javax.annotation.processing.*;
    1.38 +import javax.lang.model.*;
    1.39 +import javax.lang.model.element.*;
    1.40 +
    1.41 +import com.sun.source.tree.*;
    1.42 +import com.sun.source.util.*;
    1.43 +
    1.44 +@SupportedAnnotationTypes("*")
    1.45 +public class T6855236 extends AbstractProcessor {
    1.46 +
    1.47 +    private Trees trees;
    1.48 +
    1.49 +    @Override
    1.50 +    public void init(ProcessingEnvironment pe) {
    1.51 +        super.init(pe);
    1.52 +        trees = Trees.instance(pe);
    1.53 +    }
    1.54 +
    1.55 +    @Override
    1.56 +    public boolean process(Set<? extends TypeElement> arg0, RoundEnvironment roundEnvironment) {
    1.57 +        // Scanner class to scan through various component elements
    1.58 +        CodeVisitor visitor = new CodeVisitor();
    1.59 +
    1.60 +        for (Element e : roundEnvironment.getRootElements()) {
    1.61 +            TreePath tp = trees.getPath(e);
    1.62 +            visitor.scan(tp, trees);
    1.63 +        }
    1.64 +
    1.65 +        return true;
    1.66 +    }
    1.67 +
    1.68 +    @Override
    1.69 +    public SourceVersion getSupportedSourceVersion() {
    1.70 +        return SourceVersion.latest();
    1.71 +    }
    1.72 +
    1.73 +    class CodeVisitor extends TreePathScanner<Object, Trees> {
    1.74 +
    1.75 +        @Override
    1.76 +        public Object visitMethodInvocation(MethodInvocationTree node, Trees p) {
    1.77 +            System.out.println("current path: ");
    1.78 +            for (Tree t : getCurrentPath()) {
    1.79 +                System.out.println("    " + t.getKind() + ": " + trim(t, 64));
    1.80 +            }
    1.81 +            System.out.println("parent path: " + getCurrentPath().getParentPath());
    1.82 +            System.out.println("method select: " + node.getMethodSelect().toString());
    1.83 +            for (ExpressionTree arg : node.getArguments()) {
    1.84 +                System.out.println("argument: " + arg.toString());
    1.85 +            }
    1.86 +            return super.visitMethodInvocation(node, p);
    1.87 +        }
    1.88 +
    1.89 +        @Override
    1.90 +        public Object visitExpressionStatement(ExpressionStatementTree node, Trees p) {
    1.91 +            ExpressionTree t = node.getExpression();
    1.92 +            System.out.println();
    1.93 +            System.out.println("expression statement: " + trim(t, 64));
    1.94 +            return super.visitExpressionStatement(node, p);
    1.95 +        }
    1.96 +
    1.97 +    }
    1.98 +
    1.99 +    private String trim(Tree t, int len) {
   1.100 +        String s = t.toString().trim().replaceAll("\\s+", " ");
   1.101 +        if (s.length() > len)
   1.102 +            s = s.substring(0, len) + "...";
   1.103 +        return s;
   1.104 +    }
   1.105 +
   1.106 +}
   1.107 +
   1.108 +

mercurial