test/tools/javac/tree/ClassTreeTest.java

changeset 0
959103a6100f
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2010, 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 */
29
30 import java.io.*;
31 import java.util.*;
32 import javax.tools.*;
33 import com.sun.source.tree.*;
34 import com.sun.source.util.*;
35 import com.sun.tools.javac.api.*;
36
37 public class ClassTreeTest {
38 public static void main(String... args) throws Exception {
39 new ClassTreeTest().run();
40 }
41
42 void run() throws Exception {
43 JavacTool tool = JavacTool.create();
44 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
45 List<String> opts = Collections.<String>emptyList();
46 File testSrc = new File(System.getProperty("test.src"));
47 File thisFile = new File(testSrc, ClassTreeTest.class.getSimpleName() + ".java");
48 Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(thisFile);
49 JavacTask task = tool.getTask(null, fm, null, opts, null, fos);
50 for (CompilationUnitTree cu: task.parse()) {
51 check(cu, "CLASS", Tree.Kind.CLASS);
52 check(cu, "INTERFACE", Tree.Kind.INTERFACE);
53 check(cu, "ENUM", Tree.Kind.ENUM);
54 check(cu, "ANNOTATION_TYPE", Tree.Kind.ANNOTATION_TYPE);
55 }
56
57 int expected = 4;
58 if (checks != expected)
59 error("Unexpected number of checks performed; expected: " + expected + ", found: " + checks);
60
61 if (errors > 0)
62 throw new Exception(errors + " errors found");
63 }
64
65 void check(CompilationUnitTree cu, String name, Tree.Kind k) {
66 checks++;
67
68 TreeScanner<ClassTree,String> s = new TreeScanner<ClassTree,String>() {
69 @Override
70 public ClassTree visitClass(ClassTree c, String name) {
71 if (c.getSimpleName().toString().equals(name))
72 return c;
73 else
74 return super.visitClass(c, name);
75 }
76
77 @Override
78 public ClassTree reduce(ClassTree t1, ClassTree t2) {
79 return (t1 != null ? t1 : t2);
80 }
81 };
82
83 ClassTree c = s.scan(cu, name);
84 if (c == null)
85 error("Can't find node named " + name);
86 else if (c.getKind() != k)
87 error("Unexpected kind for node named " + name + ": expected: " + k + ", found: " + c.getKind());
88 }
89
90 void error(String msg) {
91 System.err.println("Error: " + msg);
92 errors++;
93 }
94
95 int checks;
96 int errors;
97
98 class CLASS { }
99 interface INTERFACE { }
100 enum ENUM { }
101 @interface ANNOTATION_TYPE { }
102 }

mercurial