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