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