test/tools/javac/processing/model/TestSymtabItems.java

Mon, 25 Apr 2011 15:50:30 -0700

author
jjg
date
Mon, 25 Apr 2011 15:50:30 -0700
changeset 983
fb84cfca28a1
parent 900
938dda0bec17
child 1054
111bbf1ad913
permissions
-rw-r--r--

7039019: test cannot run standalone
Reviewed-by: dlsmith

jjg@900 1 /*
jjg@900 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
jjg@900 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@900 4 *
jjg@900 5 * This code is free software; you can redistribute it and/or modify it
jjg@900 6 * under the terms of the GNU General Public License version 2 only, as
jjg@900 7 * published by the Free Software Foundation.
jjg@900 8 *
jjg@900 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@900 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@900 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@900 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@900 13 * accompanied this code).
jjg@900 14 *
jjg@900 15 * You should have received a copy of the GNU General Public License version
jjg@900 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@900 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@900 18 *
jjg@900 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@900 20 * or visit www.oracle.com if you need additional information or have any
jjg@900 21 * questions.
jjg@900 22 */
jjg@900 23
jjg@900 24 /*
jjg@900 25 * @test
jjg@900 26 * @bug 7021183
jjg@900 27 * @summary 269: assertion failure getting enclosing element of an undefined name
jjg@900 28 */
jjg@900 29
jjg@900 30 import java.lang.reflect.Field;
jjg@900 31 import javax.lang.model.element.Element;
jjg@900 32 import javax.lang.model.element.ExecutableElement;
jjg@900 33 import javax.lang.model.element.PackageElement;
jjg@900 34 import javax.lang.model.element.TypeElement;
jjg@900 35 import javax.lang.model.element.TypeParameterElement;
jjg@900 36 import javax.lang.model.element.UnknownElementException;
jjg@900 37 import javax.lang.model.element.VariableElement;
jjg@900 38 import javax.lang.model.type.TypeMirror;
jjg@900 39 import javax.lang.model.type.UnknownTypeException;
jjg@900 40 import javax.lang.model.util.ElementScanner7;
jjg@900 41 import javax.lang.model.util.SimpleTypeVisitor7;
jjg@900 42 import javax.lang.model.util.Types;
jjg@900 43
jjg@900 44 import com.sun.tools.javac.code.Symbol.ClassSymbol;
jjg@900 45 import com.sun.tools.javac.code.Symtab;
jjg@900 46 import com.sun.tools.javac.file.JavacFileManager;
jjg@983 47 import com.sun.tools.javac.main.JavaCompiler;
jjg@900 48 import com.sun.tools.javac.model.JavacTypes;
jjg@900 49 import com.sun.tools.javac.util.Context;
jjg@900 50
jjg@900 51 /**
jjg@900 52 * Scan javac Symtab looking for TypeMirrors and Elements, and ensure that
jjg@900 53 * no exceptions are thrown when used with javax.lang.model visitors.
jjg@900 54 *
jjg@900 55 */
jjg@900 56 public class TestSymtabItems {
jjg@900 57 public static void main(String... args) throws Exception {
jjg@900 58 new TestSymtabItems().run();
jjg@900 59 }
jjg@900 60
jjg@900 61 void run() throws Exception {
jjg@900 62 Context c = new Context();
jjg@900 63 JavacFileManager.preRegister(c);
jjg@900 64 Symtab syms = Symtab.instance(c);
jjg@900 65 JavacTypes types = JavacTypes.instance(c);
jjg@983 66 JavaCompiler.instance(c); // will init ClassReader.sourceCompleter
jjg@900 67
jjg@900 68 // print("noSymbol", syms.noSymbol);
jjg@900 69 // print("errSymbol", syms.errSymbol);
jjg@900 70 // print("unknownSymbol", syms.unknownSymbol);
jjg@900 71 // print("botType", syms.botType);
jjg@900 72 // print("errType", syms.errType);
jjg@900 73 // print("unknownType", syms.unknownType);
jjg@900 74
jjg@900 75 for (Field f: Symtab.class.getDeclaredFields()) {
jjg@900 76 // System.err.println(f.getType() + " " + f.getName());
jjg@900 77
jjg@900 78 // Temporarily ignore methodHandle and transientMethodHandle
jjg@900 79 // during API evolution
jjg@900 80 if (f.getName().toLowerCase().contains("methodhandle"))
jjg@900 81 continue;
jjg@900 82
jjg@900 83 Class<?> ft = f.getType();
jjg@900 84 if (TypeMirror.class.isAssignableFrom(ft))
jjg@900 85 print(f.getName(), (TypeMirror) f.get(syms), types);
jjg@900 86 else if(Element.class.isAssignableFrom(ft))
jjg@900 87 print(f.getName(), (Element) f.get(syms));
jjg@900 88 }
jjg@900 89
jjg@900 90 if (errors > 0)
jjg@900 91 throw new Exception(errors + " errors occurred");
jjg@900 92 }
jjg@900 93
jjg@900 94 void print(String label, Element e) {
jjg@900 95 ElemPrinter ep = new ElemPrinter();
jjg@900 96 System.err.println("Test " + label);
jjg@900 97 ep.visit(e);
jjg@900 98 System.err.println();
jjg@900 99 }
jjg@900 100
jjg@900 101 void print(String label, TypeMirror t, Types types) {
jjg@900 102 TypePrinter tp = new TypePrinter();
jjg@900 103 System.err.println("Test " + label);
jjg@900 104 tp.visit(t, types);
jjg@900 105 System.err.println();
jjg@900 106 }
jjg@900 107
jjg@900 108 void error(String msg) {
jjg@900 109 System.err.println("Error: " + msg);
jjg@900 110 errors++;
jjg@900 111 }
jjg@900 112
jjg@900 113 int errors;
jjg@900 114
jjg@900 115 class ElemPrinter extends ElementScanner7<Void, Void> {
jjg@900 116 @Override
jjg@900 117 public Void visitPackage(PackageElement e, Void p) {
jjg@900 118 show("package", e);
jjg@900 119 indent(+1);
jjg@900 120 super.visitPackage(e, p);
jjg@900 121 indent(-1);
jjg@900 122 return null;
jjg@900 123 }
jjg@900 124
jjg@900 125 @Override
jjg@900 126 public Void visitType(TypeElement e, Void p) {
jjg@900 127 show("type", e);
jjg@900 128 indent(+1);
jjg@900 129 super.visitType(e, p);
jjg@900 130 indent(-1);
jjg@900 131 return null;
jjg@900 132 }
jjg@900 133
jjg@900 134 @Override
jjg@900 135 public Void visitVariable(VariableElement e, Void p) {
jjg@900 136 show("variable", e);
jjg@900 137 indent(+1);
jjg@900 138 super.visitVariable(e, p);
jjg@900 139 indent(-1);
jjg@900 140 return null;
jjg@900 141 }
jjg@900 142
jjg@900 143 @Override
jjg@900 144 public Void visitExecutable(ExecutableElement e, Void p) {
jjg@900 145 show("executable", e);
jjg@900 146 indent(+1);
jjg@900 147 super.visitExecutable(e, p);
jjg@900 148 indent(-1);
jjg@900 149 return null;
jjg@900 150 }
jjg@900 151
jjg@900 152 @Override
jjg@900 153 public Void visitTypeParameter(TypeParameterElement e, Void p) {
jjg@900 154 show("type parameter", e);
jjg@900 155 indent(+1);
jjg@900 156 super.visitTypeParameter(e, p);
jjg@900 157 indent(-1);
jjg@900 158 return null;
jjg@900 159 }
jjg@900 160
jjg@900 161 @Override
jjg@900 162 public Void visitUnknown(Element e, Void p) {
jjg@900 163 show("unknown", e);
jjg@900 164 indent(+1);
jjg@900 165 try {
jjg@900 166 super.visitUnknown(e, p);
jjg@900 167 } catch (UnknownElementException ex) {
jjg@900 168 System.err.println("caught " + ex);
jjg@900 169 }
jjg@900 170 indent(-1);
jjg@900 171 return null;
jjg@900 172 }
jjg@900 173
jjg@900 174 void indent(int i) {
jjg@900 175 indent += i;
jjg@900 176 }
jjg@900 177
jjg@900 178 String sp(int w) {
jjg@900 179 StringBuilder sb = new StringBuilder();
jjg@900 180 for (int i = 0; i < w; i++)
jjg@900 181 sb.append(" ");
jjg@900 182 return sb.toString();
jjg@900 183 }
jjg@900 184
jjg@900 185 void show(String label, Element e) {
jjg@900 186 System.err.println(sp(indent) + label
jjg@900 187 + ": mods:" + e.getModifiers()
jjg@900 188 + " " + e.getSimpleName()
jjg@900 189 + ", kind: " + e.getKind()
jjg@900 190 + ", type: " + e.asType()
jjg@900 191 + ", encl: " + e.getEnclosingElement());
jjg@900 192
jjg@900 193 // The following checks help establish why NPE might occur when trying to scan children
jjg@900 194 if (e instanceof ClassSymbol) {
jjg@900 195 ClassSymbol csym = (ClassSymbol) e;
jjg@900 196 if (csym.members_field == null)
jjg@900 197 error("members_field is null");
jjg@900 198 if (csym.type == null)
jjg@900 199 System.err.println("type is null");
jjg@900 200 }
jjg@900 201 }
jjg@900 202
jjg@900 203 int indent;
jjg@900 204 };
jjg@900 205
jjg@900 206 class TypePrinter extends SimpleTypeVisitor7<Void, Types> {
jjg@900 207 @Override
jjg@900 208 public Void defaultAction(TypeMirror m, Types types) {
jjg@900 209 System.err.println(m.getKind() + " " + m + " " + types.asElement(m));
jjg@900 210 return null;
jjg@900 211 }
jjg@900 212
jjg@900 213 @Override
jjg@900 214 public Void visitUnknown(TypeMirror t, Types types) {
jjg@900 215 try {
jjg@900 216 return super.visitUnknown(t, types);
jjg@900 217 } catch (UnknownTypeException ex) {
jjg@900 218 System.err.println("caught " + ex);
jjg@900 219 return null;
jjg@900 220 }
jjg@900 221 }
jjg@900 222 };
jjg@900 223 }

mercurial