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