langtools/test/tools/javac/api/TestTreePath.java
author peterz
Fri, 28 May 2010 13:32:40 +0400
changeset 5594 3db39773da2e
parent 3150 a783d225c3e1
child 5520 86e4b9a9da40
permissions -rw-r--r--
6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3150
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
     1
/*
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
     2
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
     4
 *
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
     8
 *
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    13
 * accompanied this code).
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    14
 *
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    18
 *
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    21
 * have any questions.
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    22
 */
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    23
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    24
/*
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    25
 * @test
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    26
 * @bug 6473148
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    27
 * @summary TreePath.iterator() throws NPE
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    28
 */
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    29
import java.io.*;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    30
import java.util.Arrays;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    31
import java.util.Iterator;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    32
import java.util.Set;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    33
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    34
import javax.annotation.processing.*;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    35
import javax.lang.model.SourceVersion;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    36
import javax.lang.model.element.Element;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    37
import javax.lang.model.element.TypeElement;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    38
import javax.lang.model.util.ElementFilter;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    39
import javax.tools.JavaCompiler;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    40
import javax.tools.JavaFileObject;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    41
import javax.tools.StandardJavaFileManager;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    42
import javax.tools.ToolProvider;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    43
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    44
import com.sun.source.tree.Tree;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    45
import com.sun.source.util.*;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    46
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    47
@SupportedAnnotationTypes("*")
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    48
public class TestTreePath extends AbstractProcessor {
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    49
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    50
    @Override
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    51
    public boolean process(Set<? extends TypeElement> annotations,
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    52
            RoundEnvironment roundEnv) {
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    53
        final Trees trees = Trees.instance(this.processingEnv);
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    54
        for (Element element : ElementFilter.typesIn(roundEnv.getRootElements())) {
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    55
            checkTreePath(trees, element, 2);
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    56
            for (Element member : element.getEnclosedElements())
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    57
                checkTreePath(trees, member, 3);
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    58
        }
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    59
        return true;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    60
    }
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    61
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    62
    private void checkTreePath(Trees trees, Element element, int expectedLength) {
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    63
        TreePath path = trees.getPath(element);
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    64
        assert path != null;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    65
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    66
        int enhancedLength = 0;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    67
        for (Tree tree : path)
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    68
            ++enhancedLength;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    69
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    70
        if (enhancedLength != expectedLength)
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    71
            throw new RuntimeException("found path length is wrong");
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    72
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    73
        int normalLoopLength = 0;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    74
        Iterator<Tree> iter = path.iterator();
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    75
        while (iter.hasNext()) {
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    76
          iter.next();
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    77
          ++normalLoopLength;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    78
        }
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    79
        if (normalLoopLength != expectedLength)
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    80
            throw new RuntimeException("found path length is wrong");
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    81
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    82
        TreePath curr = path;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    83
        // using getParent
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    84
        int whileLoopLength = 0;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    85
        while (curr != null) {
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    86
          ++whileLoopLength;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    87
          curr = curr.getParentPath();
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    88
        }
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    89
        if (whileLoopLength != expectedLength)
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    90
            throw new RuntimeException("found path length is wrong");
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    91
    }
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    92
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    93
    @Override
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    94
    public SourceVersion getSupportedSourceVersion() {
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    95
        return SourceVersion.latest();
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    96
    }
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    97
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    98
    File writeTestFile() throws IOException {
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
    99
        File f = new File("Test.java");
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   100
        PrintWriter out = new PrintWriter(new FileWriter(f));
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   101
        out.println("class Test { void method() { } }");
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   102
        out.close();
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   103
        return f;
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   104
    }
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   105
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   106
    public void run() throws IOException {
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   107
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   108
        StandardJavaFileManager fileManager
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   109
            = compiler.getStandardFileManager(null, null, null);
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   110
        Iterable<? extends JavaFileObject> tests
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   111
            = fileManager.getJavaFileObjects(writeTestFile());
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   112
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   113
        JavaCompiler.CompilationTask task =
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   114
            ToolProvider.getSystemJavaCompiler().getTask(
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   115
                    null, null, null,
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   116
                    Arrays.asList("-processor", this.getClass().getName()), null,
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   117
                    tests);
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   118
        task.call();
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   119
    }
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   120
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   121
    public static void main(String[] args) throws IOException {
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   122
        new TestTreePath().run();
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   123
    }
a783d225c3e1 6855544: add missing files
jjg
parents:
diff changeset
   124
}