test/tools/javac/api/TestTreePath.java

Fri, 26 Jun 2009 19:12:41 -0700

author
jjg
date
Fri, 26 Jun 2009 19:12:41 -0700
changeset 309
664edca41e34
child 554
9d9f26857129
permissions
-rw-r--r--

6855544: add missing files
Reviewed-by: jjg, mcimadamore, darcy
Contributed-by: mernst@cs.washington.edu, mali@csail.mit.edu, mpapi@csail.mit.edu

     1 /*
     2  * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     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.
     8  *
     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).
    14  *
    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.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 6473148
    27  * @summary TreePath.iterator() throws NPE
    28  */
    29 import java.io.*;
    30 import java.util.Arrays;
    31 import java.util.Iterator;
    32 import java.util.Set;
    34 import javax.annotation.processing.*;
    35 import javax.lang.model.SourceVersion;
    36 import javax.lang.model.element.Element;
    37 import javax.lang.model.element.TypeElement;
    38 import javax.lang.model.util.ElementFilter;
    39 import javax.tools.JavaCompiler;
    40 import javax.tools.JavaFileObject;
    41 import javax.tools.StandardJavaFileManager;
    42 import javax.tools.ToolProvider;
    44 import com.sun.source.tree.Tree;
    45 import com.sun.source.util.*;
    47 @SupportedAnnotationTypes("*")
    48 public class TestTreePath extends AbstractProcessor {
    50     @Override
    51     public boolean process(Set<? extends TypeElement> annotations,
    52             RoundEnvironment roundEnv) {
    53         final Trees trees = Trees.instance(this.processingEnv);
    54         for (Element element : ElementFilter.typesIn(roundEnv.getRootElements())) {
    55             checkTreePath(trees, element, 2);
    56             for (Element member : element.getEnclosedElements())
    57                 checkTreePath(trees, member, 3);
    58         }
    59         return true;
    60     }
    62     private void checkTreePath(Trees trees, Element element, int expectedLength) {
    63         TreePath path = trees.getPath(element);
    64         assert path != null;
    66         int enhancedLength = 0;
    67         for (Tree tree : path)
    68             ++enhancedLength;
    70         if (enhancedLength != expectedLength)
    71             throw new RuntimeException("found path length is wrong");
    73         int normalLoopLength = 0;
    74         Iterator<Tree> iter = path.iterator();
    75         while (iter.hasNext()) {
    76           iter.next();
    77           ++normalLoopLength;
    78         }
    79         if (normalLoopLength != expectedLength)
    80             throw new RuntimeException("found path length is wrong");
    82         TreePath curr = path;
    83         // using getParent
    84         int whileLoopLength = 0;
    85         while (curr != null) {
    86           ++whileLoopLength;
    87           curr = curr.getParentPath();
    88         }
    89         if (whileLoopLength != expectedLength)
    90             throw new RuntimeException("found path length is wrong");
    91     }
    93     @Override
    94     public SourceVersion getSupportedSourceVersion() {
    95         return SourceVersion.latest();
    96     }
    98     File writeTestFile() throws IOException {
    99         File f = new File("Test.java");
   100         PrintWriter out = new PrintWriter(new FileWriter(f));
   101         out.println("class Test { void method() { } }");
   102         out.close();
   103         return f;
   104     }
   106     public void run() throws IOException {
   107         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
   108         StandardJavaFileManager fileManager
   109             = compiler.getStandardFileManager(null, null, null);
   110         Iterable<? extends JavaFileObject> tests
   111             = fileManager.getJavaFileObjects(writeTestFile());
   113         JavaCompiler.CompilationTask task =
   114             ToolProvider.getSystemJavaCompiler().getTask(
   115                     null, null, null,
   116                     Arrays.asList("-processor", this.getClass().getName()), null,
   117                     tests);
   118         task.call();
   119     }
   121     public static void main(String[] args) throws IOException {
   122         new TestTreePath().run();
   123     }
   124 }

mercurial