1 /* 2 * Copyright (c) 2011, 2019, 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 //both noModule and unnamedModule claim the unnamed package, ignore noModule for now: 91 if (f.getName().equals("noModule")) 92 continue; 93 94 f.setAccessible(true); 95 Class<?> ft = f.getType(); 96 if (TypeMirror.class.isAssignableFrom(ft)) 97 print(f.getName(), (TypeMirror) f.get(syms), types); 98 else if(Element.class.isAssignableFrom(ft)) 99 print(f.getName(), (Element) f.get(syms)); 100 } 101 102 if (errors > 0) 103 throw new Exception(errors + " errors occurred"); 104 } 105 106 void print(String label, Element e) { 107 ElemPrinter ep = new ElemPrinter(); 108 System.err.println("Test " + label); 109 ep.visit(e); 110 System.err.println(); 111 } 112 113 void print(String label, TypeMirror t, Types types) { 114 TypePrinter tp = new TypePrinter(); 115 System.err.println("Test " + label); 116 tp.visit(t, types); 117 System.err.println(); 118 } 119 120 void error(String msg) { 121 System.err.println("Error: " + msg); 122 errors++; 123 } 124 125 int errors; 126 127 class ElemPrinter extends ElementScanner14<Void, Void> { 128 @Override 129 public Void visitModule(ModuleElement e, Void p) { 130 show("module", e); 131 indent(+1); 132 super.visitModule(e, p); 133 indent(-1); 134 return null; 135 } 136 137 @Override 138 public Void visitPackage(PackageElement e, Void p) { 139 show("package", e); 140 indent(+1); 141 super.visitPackage(e, p); 142 indent(-1); 143 return null; 144 } 145 146 @Override 147 public Void visitType(TypeElement e, Void p) { 148 show("type", e); 149 indent(+1); 150 super.visitType(e, p); 151 indent(-1); 152 return null; 153 } 154 155 @Override 156 public Void visitVariable(VariableElement e, Void p) { 157 show("variable", e); 158 indent(+1); 159 super.visitVariable(e, p); 160 indent(-1); 161 return null; 162 } 163 164 @Override 165 public Void visitExecutable(ExecutableElement e, Void p) { 166 show("executable", e); 167 indent(+1); 168 super.visitExecutable(e, p); 169 indent(-1); 170 return null; 171 } 172 173 @Override 174 public Void visitTypeParameter(TypeParameterElement e, Void p) { 175 show("type parameter", e); 176 indent(+1); 177 super.visitTypeParameter(e, p); 178 indent(-1); 179 return null; 180 } 181 182 @Override 183 public Void visitUnknown(Element e, Void p) { 184 show("unknown", e); 185 indent(+1); 186 try { 187 super.visitUnknown(e, p); 188 } catch (UnknownElementException ex) { 189 System.err.println("caught " + ex); 190 } 191 indent(-1); 192 return null; 193 } 194 195 void indent(int i) { 196 indent += i; 197 } 198 199 String sp(int w) { 200 StringBuilder sb = new StringBuilder(); 201 for (int i = 0; i < w; i++) 202 sb.append(" "); 203 return sb.toString(); 204 } 205 206 void show(String label, Element e) { 207 System.err.println(sp(indent) + label 208 + ": mods:" + e.getModifiers() 209 + " " + e.getSimpleName() 210 + ", kind: " + e.getKind() 211 + ", type: " + e.asType() 212 + ", encl: " + e.getEnclosingElement()); 213 214 // The following checks help establish why NPE might occur when trying to scan children 215 if (e instanceof ClassSymbol) { 216 ClassSymbol csym = (ClassSymbol) e; 217 if (csym.members_field == null) 218 error("members_field is null"); 219 if (csym.type == null) 220 System.err.println("type is null"); 221 } 222 } 223 224 int indent; 225 }; 226 227 class TypePrinter extends SimpleTypeVisitor14<Void, Types> { 228 @Override 229 public Void defaultAction(TypeMirror m, Types types) { 230 System.err.println(m.getKind() + " " + m + " " + types.asElement(m)); 231 return null; 232 } 233 234 @Override 235 public Void visitUnknown(TypeMirror t, Types types) { 236 try { 237 return super.visitUnknown(t, types); 238 } catch (UnknownTypeException ex) { 239 System.err.println("caught " + ex); 240 return null; 241 } 242 } 243 }; 244 }