test/langtools/tools/javac/tree/ClassTreeTest.java
changeset 47216 71c04702a3d5
parent 30730 d3ce7619db2c
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     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.
       
     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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6570730
       
    27  * @summary com.sun.source.tree.ModifiersTree.getFlags() should return class type
       
    28  * @modules jdk.compiler/com.sun.tools.javac.api
       
    29  *          jdk.compiler/com.sun.tools.javac.file
       
    30  */
       
    31 
       
    32 import java.io.*;
       
    33 import java.util.*;
       
    34 import javax.tools.*;
       
    35 import com.sun.source.tree.*;
       
    36 import com.sun.source.util.*;
       
    37 import com.sun.tools.javac.api.*;
       
    38 
       
    39 public class ClassTreeTest {
       
    40     public static void main(String... args) throws Exception {
       
    41         new ClassTreeTest().run();
       
    42     }
       
    43 
       
    44     void run() throws Exception {
       
    45         JavacTool tool = JavacTool.create();
       
    46         try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
       
    47             List<String> opts = Collections.<String>emptyList();
       
    48             File testSrc = new File(System.getProperty("test.src"));
       
    49             File thisFile = new File(testSrc, ClassTreeTest.class.getSimpleName() + ".java");
       
    50             Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(thisFile);
       
    51             JavacTask task = tool.getTask(null, fm, null, opts, null, fos);
       
    52             for (CompilationUnitTree cu: task.parse()) {
       
    53                 check(cu, "CLASS", Tree.Kind.CLASS);
       
    54                 check(cu, "INTERFACE", Tree.Kind.INTERFACE);
       
    55                 check(cu, "ENUM", Tree.Kind.ENUM);
       
    56                 check(cu, "ANNOTATION_TYPE", Tree.Kind.ANNOTATION_TYPE);
       
    57             }
       
    58 
       
    59             int expected = 4;
       
    60             if (checks != expected)
       
    61                 error("Unexpected number of checks performed; expected: " + expected + ", found: " + checks);
       
    62 
       
    63             if (errors > 0)
       
    64                 throw new Exception(errors + " errors found");
       
    65         }
       
    66     }
       
    67 
       
    68     void check(CompilationUnitTree cu, String name, Tree.Kind k) {
       
    69         checks++;
       
    70 
       
    71         TreeScanner<ClassTree,String> s = new TreeScanner<ClassTree,String>() {
       
    72             @Override
       
    73             public ClassTree visitClass(ClassTree c, String name) {
       
    74                 if (c.getSimpleName().toString().equals(name))
       
    75                     return c;
       
    76                 else
       
    77                     return super.visitClass(c, name);
       
    78             }
       
    79 
       
    80             @Override
       
    81             public ClassTree reduce(ClassTree t1, ClassTree t2) {
       
    82                 return (t1 != null ? t1 : t2);
       
    83             }
       
    84         };
       
    85 
       
    86         ClassTree c = s.scan(cu, name);
       
    87         if (c == null)
       
    88             error("Can't find node named " + name);
       
    89         else if (c.getKind() != k)
       
    90             error("Unexpected kind for node named " + name + ": expected: " + k + ", found: " + c.getKind());
       
    91     }
       
    92 
       
    93     void error(String msg) {
       
    94         System.err.println("Error: " + msg);
       
    95         errors++;
       
    96     }
       
    97 
       
    98     int checks;
       
    99     int errors;
       
   100 
       
   101     class CLASS { }
       
   102     interface INTERFACE { }
       
   103     enum ENUM { }
       
   104     @interface ANNOTATION_TYPE { }
       
   105 }