Mon, 24 Oct 2011 13:00:30 +0100
7098660: Write better overload resolution/inference tests
Summary: Add overload/inference debug diagnostics - added test harness using annotations to check outcome of overload resolution/inference
Reviewed-by: jjg
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 |
darcy@1054 | 26 | * @bug 7021183 7025809 |
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; |
darcy@1054 | 40 | import javax.lang.model.util.*; |
jjg@900 | 41 | |
jjg@900 | 42 | import com.sun.tools.javac.code.Symbol.ClassSymbol; |
jjg@900 | 43 | import com.sun.tools.javac.code.Symtab; |
jjg@900 | 44 | import com.sun.tools.javac.file.JavacFileManager; |
jjg@983 | 45 | import com.sun.tools.javac.main.JavaCompiler; |
jjg@900 | 46 | import com.sun.tools.javac.model.JavacTypes; |
jjg@900 | 47 | import com.sun.tools.javac.util.Context; |
jjg@900 | 48 | |
jjg@900 | 49 | /** |
jjg@900 | 50 | * Scan javac Symtab looking for TypeMirrors and Elements, and ensure that |
jjg@900 | 51 | * no exceptions are thrown when used with javax.lang.model visitors. |
jjg@900 | 52 | * |
jjg@900 | 53 | */ |
jjg@900 | 54 | public class TestSymtabItems { |
jjg@900 | 55 | public static void main(String... args) throws Exception { |
jjg@900 | 56 | new TestSymtabItems().run(); |
jjg@900 | 57 | } |
jjg@900 | 58 | |
jjg@900 | 59 | void run() throws Exception { |
jjg@900 | 60 | Context c = new Context(); |
jjg@900 | 61 | JavacFileManager.preRegister(c); |
jjg@900 | 62 | Symtab syms = Symtab.instance(c); |
jjg@900 | 63 | JavacTypes types = JavacTypes.instance(c); |
jjg@983 | 64 | JavaCompiler.instance(c); // will init ClassReader.sourceCompleter |
jjg@900 | 65 | |
jjg@900 | 66 | // print("noSymbol", syms.noSymbol); |
jjg@900 | 67 | // print("errSymbol", syms.errSymbol); |
jjg@900 | 68 | // print("unknownSymbol", syms.unknownSymbol); |
jjg@900 | 69 | // print("botType", syms.botType); |
jjg@900 | 70 | // print("errType", syms.errType); |
jjg@900 | 71 | // print("unknownType", syms.unknownType); |
jjg@900 | 72 | |
jjg@900 | 73 | for (Field f: Symtab.class.getDeclaredFields()) { |
jjg@900 | 74 | // System.err.println(f.getType() + " " + f.getName()); |
jjg@900 | 75 | |
jjg@900 | 76 | // Temporarily ignore methodHandle and transientMethodHandle |
jjg@900 | 77 | // during API evolution |
jjg@900 | 78 | if (f.getName().toLowerCase().contains("methodhandle")) |
jjg@900 | 79 | continue; |
jjg@900 | 80 | |
jjg@900 | 81 | Class<?> ft = f.getType(); |
jjg@900 | 82 | if (TypeMirror.class.isAssignableFrom(ft)) |
jjg@900 | 83 | print(f.getName(), (TypeMirror) f.get(syms), types); |
jjg@900 | 84 | else if(Element.class.isAssignableFrom(ft)) |
jjg@900 | 85 | print(f.getName(), (Element) f.get(syms)); |
jjg@900 | 86 | } |
jjg@900 | 87 | |
jjg@900 | 88 | if (errors > 0) |
jjg@900 | 89 | throw new Exception(errors + " errors occurred"); |
jjg@900 | 90 | } |
jjg@900 | 91 | |
jjg@900 | 92 | void print(String label, Element e) { |
jjg@900 | 93 | ElemPrinter ep = new ElemPrinter(); |
jjg@900 | 94 | System.err.println("Test " + label); |
jjg@900 | 95 | ep.visit(e); |
jjg@900 | 96 | System.err.println(); |
jjg@900 | 97 | } |
jjg@900 | 98 | |
jjg@900 | 99 | void print(String label, TypeMirror t, Types types) { |
jjg@900 | 100 | TypePrinter tp = new TypePrinter(); |
jjg@900 | 101 | System.err.println("Test " + label); |
jjg@900 | 102 | tp.visit(t, types); |
jjg@900 | 103 | System.err.println(); |
jjg@900 | 104 | } |
jjg@900 | 105 | |
jjg@900 | 106 | void error(String msg) { |
jjg@900 | 107 | System.err.println("Error: " + msg); |
jjg@900 | 108 | errors++; |
jjg@900 | 109 | } |
jjg@900 | 110 | |
jjg@900 | 111 | int errors; |
jjg@900 | 112 | |
darcy@1054 | 113 | class ElemPrinter extends ElementScanner8<Void, Void> { |
jjg@900 | 114 | @Override |
jjg@900 | 115 | public Void visitPackage(PackageElement e, Void p) { |
jjg@900 | 116 | show("package", e); |
jjg@900 | 117 | indent(+1); |
jjg@900 | 118 | super.visitPackage(e, p); |
jjg@900 | 119 | indent(-1); |
jjg@900 | 120 | return null; |
jjg@900 | 121 | } |
jjg@900 | 122 | |
jjg@900 | 123 | @Override |
jjg@900 | 124 | public Void visitType(TypeElement e, Void p) { |
jjg@900 | 125 | show("type", e); |
jjg@900 | 126 | indent(+1); |
jjg@900 | 127 | super.visitType(e, p); |
jjg@900 | 128 | indent(-1); |
jjg@900 | 129 | return null; |
jjg@900 | 130 | } |
jjg@900 | 131 | |
jjg@900 | 132 | @Override |
jjg@900 | 133 | public Void visitVariable(VariableElement e, Void p) { |
jjg@900 | 134 | show("variable", e); |
jjg@900 | 135 | indent(+1); |
jjg@900 | 136 | super.visitVariable(e, p); |
jjg@900 | 137 | indent(-1); |
jjg@900 | 138 | return null; |
jjg@900 | 139 | } |
jjg@900 | 140 | |
jjg@900 | 141 | @Override |
jjg@900 | 142 | public Void visitExecutable(ExecutableElement e, Void p) { |
jjg@900 | 143 | show("executable", e); |
jjg@900 | 144 | indent(+1); |
jjg@900 | 145 | super.visitExecutable(e, p); |
jjg@900 | 146 | indent(-1); |
jjg@900 | 147 | return null; |
jjg@900 | 148 | } |
jjg@900 | 149 | |
jjg@900 | 150 | @Override |
jjg@900 | 151 | public Void visitTypeParameter(TypeParameterElement e, Void p) { |
jjg@900 | 152 | show("type parameter", e); |
jjg@900 | 153 | indent(+1); |
jjg@900 | 154 | super.visitTypeParameter(e, p); |
jjg@900 | 155 | indent(-1); |
jjg@900 | 156 | return null; |
jjg@900 | 157 | } |
jjg@900 | 158 | |
jjg@900 | 159 | @Override |
jjg@900 | 160 | public Void visitUnknown(Element e, Void p) { |
jjg@900 | 161 | show("unknown", e); |
jjg@900 | 162 | indent(+1); |
jjg@900 | 163 | try { |
jjg@900 | 164 | super.visitUnknown(e, p); |
jjg@900 | 165 | } catch (UnknownElementException ex) { |
jjg@900 | 166 | System.err.println("caught " + ex); |
jjg@900 | 167 | } |
jjg@900 | 168 | indent(-1); |
jjg@900 | 169 | return null; |
jjg@900 | 170 | } |
jjg@900 | 171 | |
jjg@900 | 172 | void indent(int i) { |
jjg@900 | 173 | indent += i; |
jjg@900 | 174 | } |
jjg@900 | 175 | |
jjg@900 | 176 | String sp(int w) { |
jjg@900 | 177 | StringBuilder sb = new StringBuilder(); |
jjg@900 | 178 | for (int i = 0; i < w; i++) |
jjg@900 | 179 | sb.append(" "); |
jjg@900 | 180 | return sb.toString(); |
jjg@900 | 181 | } |
jjg@900 | 182 | |
jjg@900 | 183 | void show(String label, Element e) { |
jjg@900 | 184 | System.err.println(sp(indent) + label |
jjg@900 | 185 | + ": mods:" + e.getModifiers() |
jjg@900 | 186 | + " " + e.getSimpleName() |
jjg@900 | 187 | + ", kind: " + e.getKind() |
jjg@900 | 188 | + ", type: " + e.asType() |
jjg@900 | 189 | + ", encl: " + e.getEnclosingElement()); |
jjg@900 | 190 | |
jjg@900 | 191 | // The following checks help establish why NPE might occur when trying to scan children |
jjg@900 | 192 | if (e instanceof ClassSymbol) { |
jjg@900 | 193 | ClassSymbol csym = (ClassSymbol) e; |
jjg@900 | 194 | if (csym.members_field == null) |
jjg@900 | 195 | error("members_field is null"); |
jjg@900 | 196 | if (csym.type == null) |
jjg@900 | 197 | System.err.println("type is null"); |
jjg@900 | 198 | } |
jjg@900 | 199 | } |
jjg@900 | 200 | |
jjg@900 | 201 | int indent; |
jjg@900 | 202 | }; |
jjg@900 | 203 | |
jjg@900 | 204 | class TypePrinter extends SimpleTypeVisitor7<Void, Types> { |
jjg@900 | 205 | @Override |
jjg@900 | 206 | public Void defaultAction(TypeMirror m, Types types) { |
jjg@900 | 207 | System.err.println(m.getKind() + " " + m + " " + types.asElement(m)); |
jjg@900 | 208 | return null; |
jjg@900 | 209 | } |
jjg@900 | 210 | |
jjg@900 | 211 | @Override |
jjg@900 | 212 | public Void visitUnknown(TypeMirror t, Types types) { |
jjg@900 | 213 | try { |
jjg@900 | 214 | return super.visitUnknown(t, types); |
jjg@900 | 215 | } catch (UnknownTypeException ex) { |
jjg@900 | 216 | System.err.println("caught " + ex); |
jjg@900 | 217 | return null; |
jjg@900 | 218 | } |
jjg@900 | 219 | } |
jjg@900 | 220 | }; |
jjg@900 | 221 | } |