Mon, 14 Mar 2011 11:33:33 -0700
7026414: Types.asElement() returns null for ErrorType
Reviewed-by: darcy
jjg@904 | 1 | /* |
jjg@904 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
jjg@904 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@904 | 4 | * |
jjg@904 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@904 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@904 | 7 | * published by the Free Software Foundation. |
jjg@904 | 8 | * |
jjg@904 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@904 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@904 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@904 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@904 | 13 | * accompanied this code). |
jjg@904 | 14 | * |
jjg@904 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@904 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@904 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@904 | 18 | * |
jjg@904 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@904 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@904 | 21 | * questions. |
jjg@904 | 22 | */ |
jjg@904 | 23 | |
jjg@904 | 24 | |
jjg@904 | 25 | /* |
jjg@904 | 26 | * @test |
jjg@928 | 27 | * @bug 6639645 7026414 |
jjg@904 | 28 | * @summary Modeling type implementing missing interfaces |
jjg@904 | 29 | * @library ../../../../lib |
jjg@904 | 30 | * @build JavacTestingAbstractProcessor TestMissingElement |
jjg@904 | 31 | * @compile -proc:only -XprintRounds -processor TestMissingElement InvalidSource.java |
jjg@904 | 32 | */ |
jjg@904 | 33 | |
jjg@904 | 34 | import java.util.*; |
jjg@904 | 35 | import javax.annotation.processing.*; |
jjg@904 | 36 | import javax.lang.model.element.*; |
jjg@904 | 37 | import javax.lang.model.type.*; |
jjg@904 | 38 | import javax.lang.model.util.*; |
jjg@904 | 39 | import static javax.tools.Diagnostic.Kind.*; |
jjg@904 | 40 | |
jjg@904 | 41 | public class TestMissingElement extends JavacTestingAbstractProcessor { |
jjg@904 | 42 | @Override |
jjg@904 | 43 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
jjg@904 | 44 | for (TypeElement te: ElementFilter.typesIn(roundEnv.getRootElements())) { |
jjg@904 | 45 | if (isSimpleName(te, "InvalidSource")) { |
jjg@904 | 46 | for (Element c: te.getEnclosedElements()) { |
jjg@904 | 47 | for (AnnotationMirror am: c.getAnnotationMirrors()) { |
jjg@904 | 48 | Element ate = am.getAnnotationType().asElement(); |
jjg@904 | 49 | if (isSimpleName(ate, "ExpectInterfaces")) { |
jjg@904 | 50 | checkInterfaces((TypeElement) c, getValue(am)); |
jjg@904 | 51 | } else if (isSimpleName(ate, "ExpectSupertype")) { |
jjg@904 | 52 | checkSupertype((TypeElement) c, getValue(am)); |
jjg@904 | 53 | } |
jjg@904 | 54 | } |
jjg@904 | 55 | } |
jjg@904 | 56 | } |
jjg@904 | 57 | } |
jjg@904 | 58 | return true; |
jjg@904 | 59 | } |
jjg@904 | 60 | |
jjg@904 | 61 | private boolean isSimpleName(Element e, String name) { |
jjg@904 | 62 | return e.getSimpleName().contentEquals(name); |
jjg@904 | 63 | } |
jjg@904 | 64 | |
jjg@904 | 65 | private String getValue(AnnotationMirror am) { |
jjg@904 | 66 | Map<? extends ExecutableElement, ? extends AnnotationValue> map = am.getElementValues(); |
jjg@904 | 67 | if (map.size() != 1) throw new IllegalArgumentException(); |
jjg@904 | 68 | AnnotationValue v = map.values().iterator().next(); |
jjg@904 | 69 | return (String) v.getValue(); |
jjg@904 | 70 | } |
jjg@904 | 71 | |
jjg@904 | 72 | private void checkInterfaces(TypeElement te, String expect) { |
jjg@904 | 73 | System.err.println("check interfaces: " + te + " -- " + expect); |
jjg@904 | 74 | String found = asString(te.getInterfaces(), ", "); |
jjg@904 | 75 | checkEqual("interfaces", te, found, expect); |
jjg@904 | 76 | } |
jjg@904 | 77 | |
jjg@904 | 78 | private void checkSupertype(TypeElement te, String expect) { |
jjg@904 | 79 | System.err.println("check supertype: " + te + " -- " + expect); |
jjg@904 | 80 | String found = asString(te.getSuperclass()); |
jjg@904 | 81 | checkEqual("supertype", te, found, expect); |
jjg@904 | 82 | } |
jjg@904 | 83 | |
jjg@904 | 84 | private void checkEqual(String label, TypeElement te, String found, String expect) { |
jjg@904 | 85 | if (found.equals(expect)) { |
jjg@904 | 86 | // messager.printMessage(NOTE, "expected " + label + " found: " + expect, te); |
jjg@904 | 87 | } else { |
jjg@904 | 88 | System.err.println("unexpected " + label + ": " + te + "\n" |
jjg@904 | 89 | + " found: " + found + "\n" |
jjg@904 | 90 | + "expect: " + expect); |
jjg@904 | 91 | messager.printMessage(ERROR, "unexpected " + label + " found: " + found + "; expected: " + expect, te); |
jjg@904 | 92 | } |
jjg@904 | 93 | } |
jjg@904 | 94 | |
jjg@904 | 95 | private String asString(List<? extends TypeMirror> ts, String sep) { |
jjg@904 | 96 | StringBuilder sb = new StringBuilder(); |
jjg@904 | 97 | for (TypeMirror t: ts) { |
jjg@904 | 98 | if (sb.length() != 0) sb.append(sep); |
jjg@904 | 99 | sb.append(asString(t)); |
jjg@904 | 100 | } |
jjg@904 | 101 | return sb.toString(); |
jjg@904 | 102 | } |
jjg@904 | 103 | |
jjg@904 | 104 | private String asString(TypeMirror t) { |
jjg@904 | 105 | if (t == null) |
jjg@904 | 106 | return "[typ:null]"; |
jjg@904 | 107 | return t.accept(new SimpleTypeVisitor7<String, Void>() { |
jjg@904 | 108 | @Override |
jjg@904 | 109 | public String defaultAction(TypeMirror t, Void ignore) { |
jjg@904 | 110 | return "[typ:" + t.toString() + "]"; |
jjg@904 | 111 | } |
jjg@904 | 112 | |
jjg@904 | 113 | @Override |
jjg@904 | 114 | public String visitDeclared(DeclaredType t, Void ignore) { |
jjg@928 | 115 | checkEqual(t.asElement(), types.asElement(t)); |
jjg@904 | 116 | String s = asString(t.asElement()); |
jjg@904 | 117 | List<? extends TypeMirror> args = t.getTypeArguments(); |
jjg@904 | 118 | if (!args.isEmpty()) |
jjg@904 | 119 | s += "<" + asString(args, ",") + ">"; |
jjg@904 | 120 | return s; |
jjg@904 | 121 | } |
jjg@904 | 122 | |
jjg@904 | 123 | @Override |
jjg@904 | 124 | public String visitTypeVariable(TypeVariable t, Void ignore) { |
jjg@904 | 125 | return "tvar " + t; |
jjg@904 | 126 | } |
jjg@904 | 127 | |
jjg@904 | 128 | @Override |
jjg@904 | 129 | public String visitError(ErrorType t, Void ignore) { |
jjg@904 | 130 | return "!:" + visitDeclared(t, ignore); |
jjg@904 | 131 | } |
jjg@904 | 132 | }, null); |
jjg@904 | 133 | } |
jjg@904 | 134 | |
jjg@904 | 135 | private String asString(Element e) { |
jjg@904 | 136 | if (e == null) |
jjg@904 | 137 | return "[elt:null]"; |
jjg@904 | 138 | return e.accept(new SimpleElementVisitor7<String, Void>() { |
jjg@904 | 139 | @Override |
jjg@904 | 140 | public String defaultAction(Element e, Void ignore) { |
jjg@904 | 141 | return "[elt:" + e.getKind() + " " + e.toString() + "]"; |
jjg@904 | 142 | } |
jjg@904 | 143 | @Override |
jjg@904 | 144 | public String visitPackage(PackageElement e, Void ignore) { |
jjg@904 | 145 | return "pkg " + e.getQualifiedName(); |
jjg@904 | 146 | } |
jjg@904 | 147 | @Override |
jjg@904 | 148 | public String visitType(TypeElement e, Void ignore) { |
jjg@904 | 149 | StringBuilder sb = new StringBuilder(); |
jjg@904 | 150 | if (e.getEnclosedElements().isEmpty()) |
jjg@904 | 151 | sb.append("empty "); |
jjg@904 | 152 | ElementKind ek = e.getKind(); |
jjg@904 | 153 | switch (ek) { |
jjg@904 | 154 | case CLASS: |
jjg@904 | 155 | sb.append("clss"); |
jjg@904 | 156 | break; |
jjg@904 | 157 | case INTERFACE: |
jjg@904 | 158 | sb.append("intf"); |
jjg@904 | 159 | break; |
jjg@904 | 160 | default: |
jjg@904 | 161 | sb.append(ek); |
jjg@904 | 162 | break; |
jjg@904 | 163 | } |
jjg@904 | 164 | sb.append(" "); |
jjg@904 | 165 | Element encl = e.getEnclosingElement(); |
jjg@904 | 166 | if (!isUnnamedPackage(encl) && encl.asType().getKind() != TypeKind.NONE) { |
jjg@904 | 167 | sb.append("("); |
jjg@904 | 168 | sb.append(asString(encl)); |
jjg@904 | 169 | sb.append(")"); |
jjg@904 | 170 | sb.append("."); |
jjg@904 | 171 | } |
jjg@904 | 172 | sb.append(e.getSimpleName()); |
jjg@904 | 173 | if (e.asType().getKind() == TypeKind.ERROR) sb.append("!"); |
jjg@904 | 174 | return sb.toString(); |
jjg@904 | 175 | } |
jjg@904 | 176 | }, null); |
jjg@904 | 177 | } |
jjg@904 | 178 | |
jjg@904 | 179 | boolean isUnnamedPackage(Element e) { |
jjg@904 | 180 | return (e != null && e.getKind() == ElementKind.PACKAGE |
jjg@904 | 181 | && ((PackageElement) e).isUnnamed()); |
jjg@904 | 182 | } |
jjg@928 | 183 | |
jjg@928 | 184 | void checkEqual(Element e1, Element e2) { |
jjg@928 | 185 | if (e1 != e2) { |
jjg@928 | 186 | throw new AssertionError("elements not equal as expected: " |
jjg@928 | 187 | + e1 + ", " + e2); |
jjg@928 | 188 | } |
jjg@928 | 189 | } |
jjg@904 | 190 | } |
jjg@904 | 191 | |
jjg@904 | 192 | |
jjg@904 | 193 |