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