Tue, 01 Mar 2011 12:00:06 -0800
7021183: 269: assertion failure getting enclosing element of an undefined name
Reviewed-by: mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Symtab.java Mon Feb 28 13:42:24 2011 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Symtab.java Tue Mar 01 12:00:06 2011 -0800 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -26,6 +26,8 @@ 1.11 package com.sun.tools.javac.code; 1.12 1.13 import java.util.*; 1.14 +import javax.lang.model.type.TypeVisitor; 1.15 +import javax.lang.model.element.ElementVisitor; 1.16 1.17 import com.sun.tools.javac.util.*; 1.18 import com.sun.tools.javac.util.List; 1.19 @@ -345,7 +347,12 @@ 1.20 target = Target.instance(context); 1.21 1.22 // Create the unknown type 1.23 - unknownType = new Type(TypeTags.UNKNOWN, null); 1.24 + unknownType = new Type(TypeTags.UNKNOWN, null) { 1.25 + @Override 1.26 + public <R, P> R accept(TypeVisitor<R, P> v, P p) { 1.27 + return v.visitUnknown(this, p); 1.28 + } 1.29 + }; 1.30 1.31 // create the basic builtin symbols 1.32 rootPackage = new PackageSymbol(names.empty, null); 1.33 @@ -355,13 +362,20 @@ 1.34 return messages.getLocalizedString("compiler.misc.unnamed.package"); 1.35 } 1.36 }; 1.37 - noSymbol = new TypeSymbol(0, names.empty, Type.noType, rootPackage); 1.38 + noSymbol = new TypeSymbol(0, names.empty, Type.noType, rootPackage) { 1.39 + public <R, P> R accept(ElementVisitor<R, P> v, P p) { 1.40 + return v.visitUnknown(this, p); 1.41 + } 1.42 + }; 1.43 noSymbol.kind = Kinds.NIL; 1.44 1.45 // create the error symbols 1.46 errSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.any, null, rootPackage); 1.47 + errType = new ErrorType(errSymbol, Type.noType); 1.48 + 1.49 unknownSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.fromString("<any?>"), null, rootPackage); 1.50 - errType = new ErrorType(errSymbol, Type.noType); 1.51 + unknownSymbol.members_field = new Scope.ErrorScope(unknownSymbol); 1.52 + unknownSymbol.type = unknownType; 1.53 1.54 // initialize builtin types 1.55 initType(byteType, "byte", "Byte"); 1.56 @@ -382,9 +396,11 @@ 1.57 1.58 // VGJ 1.59 boundClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Bound, noSymbol); 1.60 + boundClass.members_field = new Scope.ErrorScope(boundClass); 1.61 1.62 // the builtin class of all methods 1.63 methodClass = new ClassSymbol(PUBLIC|ACYCLIC, names.Method, noSymbol); 1.64 + methodClass.members_field = new Scope.ErrorScope(boundClass); 1.65 1.66 // Create class to hold all predefined constants and operations. 1.67 predefClass = new ClassSymbol(PUBLIC|ACYCLIC, names.empty, rootPackage);
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/processing/model/TestSymtabItems.java Tue Mar 01 12:00:06 2011 -0800 2.3 @@ -0,0 +1,221 @@ 2.4 +/* 2.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 7021183 2.30 + * @summary 269: assertion failure getting enclosing element of an undefined name 2.31 + */ 2.32 + 2.33 +import java.lang.reflect.Field; 2.34 +import javax.lang.model.element.Element; 2.35 +import javax.lang.model.element.ExecutableElement; 2.36 +import javax.lang.model.element.PackageElement; 2.37 +import javax.lang.model.element.TypeElement; 2.38 +import javax.lang.model.element.TypeParameterElement; 2.39 +import javax.lang.model.element.UnknownElementException; 2.40 +import javax.lang.model.element.VariableElement; 2.41 +import javax.lang.model.type.TypeMirror; 2.42 +import javax.lang.model.type.UnknownTypeException; 2.43 +import javax.lang.model.util.ElementScanner7; 2.44 +import javax.lang.model.util.SimpleTypeVisitor7; 2.45 +import javax.lang.model.util.Types; 2.46 + 2.47 +import com.sun.tools.javac.code.Symbol.ClassSymbol; 2.48 +import com.sun.tools.javac.code.Symtab; 2.49 +import com.sun.tools.javac.file.JavacFileManager; 2.50 +import com.sun.tools.javac.model.JavacTypes; 2.51 +import com.sun.tools.javac.util.Context; 2.52 + 2.53 +/** 2.54 + * Scan javac Symtab looking for TypeMirrors and Elements, and ensure that 2.55 + * no exceptions are thrown when used with javax.lang.model visitors. 2.56 + * 2.57 + */ 2.58 +public class TestSymtabItems { 2.59 + public static void main(String... args) throws Exception { 2.60 + new TestSymtabItems().run(); 2.61 + } 2.62 + 2.63 + void run() throws Exception { 2.64 + Context c = new Context(); 2.65 + JavacFileManager.preRegister(c); 2.66 + Symtab syms = Symtab.instance(c); 2.67 + JavacTypes types = JavacTypes.instance(c); 2.68 + 2.69 +// print("noSymbol", syms.noSymbol); 2.70 +// print("errSymbol", syms.errSymbol); 2.71 +// print("unknownSymbol", syms.unknownSymbol); 2.72 +// print("botType", syms.botType); 2.73 +// print("errType", syms.errType); 2.74 +// print("unknownType", syms.unknownType); 2.75 + 2.76 + for (Field f: Symtab.class.getDeclaredFields()) { 2.77 +// System.err.println(f.getType() + " " + f.getName()); 2.78 + 2.79 + // Temporarily ignore methodHandle and transientMethodHandle 2.80 + // during API evolution 2.81 + if (f.getName().toLowerCase().contains("methodhandle")) 2.82 + continue; 2.83 + 2.84 + Class<?> ft = f.getType(); 2.85 + if (TypeMirror.class.isAssignableFrom(ft)) 2.86 + print(f.getName(), (TypeMirror) f.get(syms), types); 2.87 + else if(Element.class.isAssignableFrom(ft)) 2.88 + print(f.getName(), (Element) f.get(syms)); 2.89 + } 2.90 + 2.91 + if (errors > 0) 2.92 + throw new Exception(errors + " errors occurred"); 2.93 + } 2.94 + 2.95 + void print(String label, Element e) { 2.96 + ElemPrinter ep = new ElemPrinter(); 2.97 + System.err.println("Test " + label); 2.98 + ep.visit(e); 2.99 + System.err.println(); 2.100 + } 2.101 + 2.102 + void print(String label, TypeMirror t, Types types) { 2.103 + TypePrinter tp = new TypePrinter(); 2.104 + System.err.println("Test " + label); 2.105 + tp.visit(t, types); 2.106 + System.err.println(); 2.107 + } 2.108 + 2.109 + void error(String msg) { 2.110 + System.err.println("Error: " + msg); 2.111 + errors++; 2.112 + } 2.113 + 2.114 + int errors; 2.115 + 2.116 + class ElemPrinter extends ElementScanner7<Void, Void> { 2.117 + @Override 2.118 + public Void visitPackage(PackageElement e, Void p) { 2.119 + show("package", e); 2.120 + indent(+1); 2.121 + super.visitPackage(e, p); 2.122 + indent(-1); 2.123 + return null; 2.124 + } 2.125 + 2.126 + @Override 2.127 + public Void visitType(TypeElement e, Void p) { 2.128 + show("type", e); 2.129 + indent(+1); 2.130 + super.visitType(e, p); 2.131 + indent(-1); 2.132 + return null; 2.133 + } 2.134 + 2.135 + @Override 2.136 + public Void visitVariable(VariableElement e, Void p) { 2.137 + show("variable", e); 2.138 + indent(+1); 2.139 + super.visitVariable(e, p); 2.140 + indent(-1); 2.141 + return null; 2.142 + } 2.143 + 2.144 + @Override 2.145 + public Void visitExecutable(ExecutableElement e, Void p) { 2.146 + show("executable", e); 2.147 + indent(+1); 2.148 + super.visitExecutable(e, p); 2.149 + indent(-1); 2.150 + return null; 2.151 + } 2.152 + 2.153 + @Override 2.154 + public Void visitTypeParameter(TypeParameterElement e, Void p) { 2.155 + show("type parameter", e); 2.156 + indent(+1); 2.157 + super.visitTypeParameter(e, p); 2.158 + indent(-1); 2.159 + return null; 2.160 + } 2.161 + 2.162 + @Override 2.163 + public Void visitUnknown(Element e, Void p) { 2.164 + show("unknown", e); 2.165 + indent(+1); 2.166 + try { 2.167 + super.visitUnknown(e, p); 2.168 + } catch (UnknownElementException ex) { 2.169 + System.err.println("caught " + ex); 2.170 + } 2.171 + indent(-1); 2.172 + return null; 2.173 + } 2.174 + 2.175 + void indent(int i) { 2.176 + indent += i; 2.177 + } 2.178 + 2.179 + String sp(int w) { 2.180 + StringBuilder sb = new StringBuilder(); 2.181 + for (int i = 0; i < w; i++) 2.182 + sb.append(" "); 2.183 + return sb.toString(); 2.184 + } 2.185 + 2.186 + void show(String label, Element e) { 2.187 + System.err.println(sp(indent) + label 2.188 + + ": mods:" + e.getModifiers() 2.189 + + " " + e.getSimpleName() 2.190 + + ", kind: " + e.getKind() 2.191 + + ", type: " + e.asType() 2.192 + + ", encl: " + e.getEnclosingElement()); 2.193 + 2.194 + // The following checks help establish why NPE might occur when trying to scan children 2.195 + if (e instanceof ClassSymbol) { 2.196 + ClassSymbol csym = (ClassSymbol) e; 2.197 + if (csym.members_field == null) 2.198 + error("members_field is null"); 2.199 + if (csym.type == null) 2.200 + System.err.println("type is null"); 2.201 + } 2.202 + } 2.203 + 2.204 + int indent; 2.205 + }; 2.206 + 2.207 + class TypePrinter extends SimpleTypeVisitor7<Void, Types> { 2.208 + @Override 2.209 + public Void defaultAction(TypeMirror m, Types types) { 2.210 + System.err.println(m.getKind() + " " + m + " " + types.asElement(m)); 2.211 + return null; 2.212 + } 2.213 + 2.214 + @Override 2.215 + public Void visitUnknown(TypeMirror t, Types types) { 2.216 + try { 2.217 + return super.visitUnknown(t, types); 2.218 + } catch (UnknownTypeException ex) { 2.219 + System.err.println("caught " + ex); 2.220 + return null; 2.221 + } 2.222 + } 2.223 + }; 2.224 +}