1/*
2 * Copyright (c) 2011, 2016, 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 * @test
26 * @bug 6639645 7026414 7025809
27 * @summary Modeling type implementing missing interfaces
28 * @library /tools/javac/lib
29 * @modules jdk.compiler/com.sun.tools.javac.processing
30 *          jdk.compiler/com.sun.tools.javac.util
31 * @build JavacTestingAbstractProcessor TestMissingElement
32 * @compile/fail/ref=TestMissingElement.ref -XDaccessInternalAPI -proc:only -XprintRounds -XDrawDiagnostics -processor TestMissingElement InvalidSource.java
33 */
34
35import java.io.PrintWriter;
36import java.util.*;
37import javax.annotation.processing.*;
38import javax.lang.model.element.*;
39import javax.lang.model.type.*;
40import javax.lang.model.util.*;
41import static javax.tools.Diagnostic.Kind.*;
42
43import com.sun.tools.javac.processing.JavacProcessingEnvironment;
44import com.sun.tools.javac.util.Log;
45
46public class TestMissingElement extends JavacTestingAbstractProcessor {
47    private PrintWriter out;
48
49    @Override
50    public void init(ProcessingEnvironment env) {
51        super.init(env);
52        out = ((JavacProcessingEnvironment) env).getContext().get(Log.logKey).getWriter(Log.WriterKind.STDERR);
53    }
54
55    @Override
56    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
57        for (TypeElement te: ElementFilter.typesIn(roundEnv.getRootElements())) {
58            if (isSimpleName(te, "InvalidSource")) {
59                for (Element c: te.getEnclosedElements()) {
60                    for (AnnotationMirror am: c.getAnnotationMirrors()) {
61                        Element ate = am.getAnnotationType().asElement();
62                        if (isSimpleName(ate, "ExpectInterfaces")) {
63                            checkInterfaces((TypeElement) c, getValue(am));
64                        } else if (isSimpleName(ate, "ExpectSupertype")) {
65                            checkSupertype((TypeElement) c, getValue(am));
66                        }
67                    }
68                }
69            }
70        }
71        return true;
72    }
73
74    private boolean isSimpleName(Element e, String name) {
75        return e.getSimpleName().contentEquals(name);
76    }
77
78    private String getValue(AnnotationMirror am) {
79        Map<? extends ExecutableElement, ? extends AnnotationValue> map = am.getElementValues();
80        if (map.size() != 1) throw new IllegalArgumentException();
81        AnnotationValue v = map.values().iterator().next();
82        return (String) v.getValue();
83    }
84
85    private void checkInterfaces(TypeElement te, String expect) {
86        out.println("check interfaces: " + te + " -- " + expect);
87        String found = asString(te.getInterfaces(), ", ");
88        checkEqual("interfaces", te, found, expect);
89    }
90
91    private void checkSupertype(TypeElement te, String expect) {
92        out.println("check supertype: " + te + " -- " + expect);
93        String found = asString(te.getSuperclass());
94        checkEqual("supertype", te, found, expect);
95    }
96
97    private void checkEqual(String label, TypeElement te, String found, String expect) {
98        if (found.equals(expect)) {
99//            messager.printMessage(NOTE, "expected " + label + " found: " + expect, te);
100        } else {
101            out.println("unexpected " + label + ": " + te + "\n"
102                    + " found: " + found + "\n"
103                    + "expect: " + expect);
104            messager.printMessage(ERROR, "unexpected " + label + " found: " + found + "; expected: " + expect, te);
105        }
106    }
107
108    private String asString(List<? extends TypeMirror> ts, String sep) {
109        StringBuilder sb = new StringBuilder();
110        for (TypeMirror t: ts) {
111            if (sb.length() != 0) sb.append(sep);
112            sb.append(asString(t));
113        }
114        return sb.toString();
115    }
116
117    private String asString(TypeMirror t) {
118        if (t == null)
119            return "[typ:null]";
120        return t.accept(new SimpleTypeVisitor<String, Void>() {
121            @Override
122            public String defaultAction(TypeMirror t, Void ignore) {
123                return "[typ:" + t.toString() + "]";
124            }
125
126            @Override
127            public String visitDeclared(DeclaredType t, Void ignore) {
128                checkEqual(t.asElement(), types.asElement(t));
129                String s = asString(t.asElement());
130                List<? extends TypeMirror> args = t.getTypeArguments();
131                if (!args.isEmpty())
132                    s += "<" + asString(args, ",") + ">";
133                return s;
134            }
135
136            @Override
137            public String visitTypeVariable(TypeVariable t, Void ignore) {
138                return "tvar " + t;
139            }
140
141            @Override
142            public String visitError(ErrorType t, Void ignore) {
143                return "!:" + visitDeclared(t, ignore);
144            }
145        }, null);
146    }
147
148    private String asString(Element e) {
149        if (e == null)
150            return "[elt:null]";
151        return e.accept(new SimpleElementVisitor<String, Void>() {
152            @Override
153            public String defaultAction(Element e, Void ignore) {
154                return "[elt:" + e.getKind() + " " + e.toString() + "]";
155            }
156            @Override
157            public String visitPackage(PackageElement e, Void ignore) {
158                return "pkg " + e.getQualifiedName();
159            }
160            @Override
161            public String visitType(TypeElement e, Void ignore) {
162                StringBuilder sb = new StringBuilder();
163                if (e.getEnclosedElements().isEmpty())
164                    sb.append("empty ");
165                ElementKind ek = e.getKind();
166                switch (ek) {
167                    case CLASS:
168                        sb.append("clss");
169                        break;
170                    case INTERFACE:
171                        sb.append("intf");
172                        break;
173                    default:
174                        sb.append(ek);
175                        break;
176                }
177                sb.append(" ");
178                Element encl = e.getEnclosingElement();
179                if (!isUnnamedPackage(encl) && encl.asType().getKind() != TypeKind.NONE) {
180                    sb.append("(");
181                    sb.append(asString(encl));
182                    sb.append(")");
183                    sb.append(".");
184                }
185                sb.append(e.getSimpleName());
186                if (e.asType().getKind() == TypeKind.ERROR) sb.append("!");
187                return sb.toString();
188            }
189        }, null);
190    }
191
192    boolean isUnnamedPackage(Element e) {
193        return (e != null && e.getKind() == ElementKind.PACKAGE
194                && ((PackageElement) e).isUnnamed());
195    }
196
197    void checkEqual(Element e1, Element e2) {
198        if (e1 != e2) {
199            throw new AssertionError("elements not equal as expected: "
200                + e1 + ", " + e2);
201        }
202    }
203}
204
205
206
207