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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1054
111bbf1ad913
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial