Mon, 25 Apr 2011 15:50:30 -0700
7039019: test cannot run standalone
Reviewed-by: dlsmith
1 /*
2 * Copyright (c) 2011, 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 */
24 /*
25 * @test
26 * @bug 7021183
27 * @summary 269: assertion failure getting enclosing element of an undefined name
28 */
30 import java.lang.reflect.Field;
31 import javax.lang.model.element.Element;
32 import javax.lang.model.element.ExecutableElement;
33 import javax.lang.model.element.PackageElement;
34 import javax.lang.model.element.TypeElement;
35 import javax.lang.model.element.TypeParameterElement;
36 import javax.lang.model.element.UnknownElementException;
37 import javax.lang.model.element.VariableElement;
38 import javax.lang.model.type.TypeMirror;
39 import javax.lang.model.type.UnknownTypeException;
40 import javax.lang.model.util.ElementScanner7;
41 import javax.lang.model.util.SimpleTypeVisitor7;
42 import javax.lang.model.util.Types;
44 import com.sun.tools.javac.code.Symbol.ClassSymbol;
45 import com.sun.tools.javac.code.Symtab;
46 import com.sun.tools.javac.file.JavacFileManager;
47 import com.sun.tools.javac.main.JavaCompiler;
48 import com.sun.tools.javac.model.JavacTypes;
49 import com.sun.tools.javac.util.Context;
51 /**
52 * Scan javac Symtab looking for TypeMirrors and Elements, and ensure that
53 * no exceptions are thrown when used with javax.lang.model visitors.
54 *
55 */
56 public class TestSymtabItems {
57 public static void main(String... args) throws Exception {
58 new TestSymtabItems().run();
59 }
61 void run() throws Exception {
62 Context c = new Context();
63 JavacFileManager.preRegister(c);
64 Symtab syms = Symtab.instance(c);
65 JavacTypes types = JavacTypes.instance(c);
66 JavaCompiler.instance(c); // will init ClassReader.sourceCompleter
68 // print("noSymbol", syms.noSymbol);
69 // print("errSymbol", syms.errSymbol);
70 // print("unknownSymbol", syms.unknownSymbol);
71 // print("botType", syms.botType);
72 // print("errType", syms.errType);
73 // print("unknownType", syms.unknownType);
75 for (Field f: Symtab.class.getDeclaredFields()) {
76 // System.err.println(f.getType() + " " + f.getName());
78 // Temporarily ignore methodHandle and transientMethodHandle
79 // during API evolution
80 if (f.getName().toLowerCase().contains("methodhandle"))
81 continue;
83 Class<?> ft = f.getType();
84 if (TypeMirror.class.isAssignableFrom(ft))
85 print(f.getName(), (TypeMirror) f.get(syms), types);
86 else if(Element.class.isAssignableFrom(ft))
87 print(f.getName(), (Element) f.get(syms));
88 }
90 if (errors > 0)
91 throw new Exception(errors + " errors occurred");
92 }
94 void print(String label, Element e) {
95 ElemPrinter ep = new ElemPrinter();
96 System.err.println("Test " + label);
97 ep.visit(e);
98 System.err.println();
99 }
101 void print(String label, TypeMirror t, Types types) {
102 TypePrinter tp = new TypePrinter();
103 System.err.println("Test " + label);
104 tp.visit(t, types);
105 System.err.println();
106 }
108 void error(String msg) {
109 System.err.println("Error: " + msg);
110 errors++;
111 }
113 int errors;
115 class ElemPrinter extends ElementScanner7<Void, Void> {
116 @Override
117 public Void visitPackage(PackageElement e, Void p) {
118 show("package", e);
119 indent(+1);
120 super.visitPackage(e, p);
121 indent(-1);
122 return null;
123 }
125 @Override
126 public Void visitType(TypeElement e, Void p) {
127 show("type", e);
128 indent(+1);
129 super.visitType(e, p);
130 indent(-1);
131 return null;
132 }
134 @Override
135 public Void visitVariable(VariableElement e, Void p) {
136 show("variable", e);
137 indent(+1);
138 super.visitVariable(e, p);
139 indent(-1);
140 return null;
141 }
143 @Override
144 public Void visitExecutable(ExecutableElement e, Void p) {
145 show("executable", e);
146 indent(+1);
147 super.visitExecutable(e, p);
148 indent(-1);
149 return null;
150 }
152 @Override
153 public Void visitTypeParameter(TypeParameterElement e, Void p) {
154 show("type parameter", e);
155 indent(+1);
156 super.visitTypeParameter(e, p);
157 indent(-1);
158 return null;
159 }
161 @Override
162 public Void visitUnknown(Element e, Void p) {
163 show("unknown", e);
164 indent(+1);
165 try {
166 super.visitUnknown(e, p);
167 } catch (UnknownElementException ex) {
168 System.err.println("caught " + ex);
169 }
170 indent(-1);
171 return null;
172 }
174 void indent(int i) {
175 indent += i;
176 }
178 String sp(int w) {
179 StringBuilder sb = new StringBuilder();
180 for (int i = 0; i < w; i++)
181 sb.append(" ");
182 return sb.toString();
183 }
185 void show(String label, Element e) {
186 System.err.println(sp(indent) + label
187 + ": mods:" + e.getModifiers()
188 + " " + e.getSimpleName()
189 + ", kind: " + e.getKind()
190 + ", type: " + e.asType()
191 + ", encl: " + e.getEnclosingElement());
193 // The following checks help establish why NPE might occur when trying to scan children
194 if (e instanceof ClassSymbol) {
195 ClassSymbol csym = (ClassSymbol) e;
196 if (csym.members_field == null)
197 error("members_field is null");
198 if (csym.type == null)
199 System.err.println("type is null");
200 }
201 }
203 int indent;
204 };
206 class TypePrinter extends SimpleTypeVisitor7<Void, Types> {
207 @Override
208 public Void defaultAction(TypeMirror m, Types types) {
209 System.err.println(m.getKind() + " " + m + " " + types.asElement(m));
210 return null;
211 }
213 @Override
214 public Void visitUnknown(TypeMirror t, Types types) {
215 try {
216 return super.visitUnknown(t, types);
217 } catch (UnknownTypeException ex) {
218 System.err.println("caught " + ex);
219 return null;
220 }
221 }
222 };
223 }