test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,205 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +
    1.28 +/*
    1.29 + * @test
    1.30 + * @bug 6639645 7026414 7025809
    1.31 + * @summary Modeling type implementing missing interfaces
    1.32 + * @library /tools/javac/lib
    1.33 + * @build JavacTestingAbstractProcessor TestMissingElement
    1.34 + * @compile/fail/ref=TestMissingElement.ref -proc:only -XprintRounds -XDrawDiagnostics -processor TestMissingElement InvalidSource.java
    1.35 + */
    1.36 +
    1.37 +import java.io.PrintWriter;
    1.38 +import java.util.*;
    1.39 +import javax.annotation.processing.*;
    1.40 +import javax.lang.model.element.*;
    1.41 +import javax.lang.model.type.*;
    1.42 +import javax.lang.model.util.*;
    1.43 +import static javax.tools.Diagnostic.Kind.*;
    1.44 +
    1.45 +import com.sun.tools.javac.processing.JavacProcessingEnvironment;
    1.46 +import com.sun.tools.javac.util.Log;
    1.47 +
    1.48 +public class TestMissingElement extends JavacTestingAbstractProcessor {
    1.49 +    private PrintWriter out;
    1.50 +
    1.51 +    @Override
    1.52 +    public void init(ProcessingEnvironment env) {
    1.53 +        super.init(env);
    1.54 +        out = ((JavacProcessingEnvironment) env).getContext().get(Log.outKey);
    1.55 +    }
    1.56 +
    1.57 +    @Override
    1.58 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    1.59 +        for (TypeElement te: ElementFilter.typesIn(roundEnv.getRootElements())) {
    1.60 +            if (isSimpleName(te, "InvalidSource")) {
    1.61 +                for (Element c: te.getEnclosedElements()) {
    1.62 +                    for (AnnotationMirror am: c.getAnnotationMirrors()) {
    1.63 +                        Element ate = am.getAnnotationType().asElement();
    1.64 +                        if (isSimpleName(ate, "ExpectInterfaces")) {
    1.65 +                            checkInterfaces((TypeElement) c, getValue(am));
    1.66 +                        } else if (isSimpleName(ate, "ExpectSupertype")) {
    1.67 +                            checkSupertype((TypeElement) c, getValue(am));
    1.68 +                        }
    1.69 +                    }
    1.70 +                }
    1.71 +            }
    1.72 +        }
    1.73 +        return true;
    1.74 +    }
    1.75 +
    1.76 +    private boolean isSimpleName(Element e, String name) {
    1.77 +        return e.getSimpleName().contentEquals(name);
    1.78 +    }
    1.79 +
    1.80 +    private String getValue(AnnotationMirror am) {
    1.81 +        Map<? extends ExecutableElement, ? extends AnnotationValue> map = am.getElementValues();
    1.82 +        if (map.size() != 1) throw new IllegalArgumentException();
    1.83 +        AnnotationValue v = map.values().iterator().next();
    1.84 +        return (String) v.getValue();
    1.85 +    }
    1.86 +
    1.87 +    private void checkInterfaces(TypeElement te, String expect) {
    1.88 +        out.println("check interfaces: " + te + " -- " + expect);
    1.89 +        String found = asString(te.getInterfaces(), ", ");
    1.90 +        checkEqual("interfaces", te, found, expect);
    1.91 +    }
    1.92 +
    1.93 +    private void checkSupertype(TypeElement te, String expect) {
    1.94 +        out.println("check supertype: " + te + " -- " + expect);
    1.95 +        String found = asString(te.getSuperclass());
    1.96 +        checkEqual("supertype", te, found, expect);
    1.97 +    }
    1.98 +
    1.99 +    private void checkEqual(String label, TypeElement te, String found, String expect) {
   1.100 +        if (found.equals(expect)) {
   1.101 +//            messager.printMessage(NOTE, "expected " + label + " found: " + expect, te);
   1.102 +        } else {
   1.103 +            out.println("unexpected " + label + ": " + te + "\n"
   1.104 +                    + " found: " + found + "\n"
   1.105 +                    + "expect: " + expect);
   1.106 +            messager.printMessage(ERROR, "unexpected " + label + " found: " + found + "; expected: " + expect, te);
   1.107 +        }
   1.108 +    }
   1.109 +
   1.110 +    private String asString(List<? extends TypeMirror> ts, String sep) {
   1.111 +        StringBuilder sb = new StringBuilder();
   1.112 +        for (TypeMirror t: ts) {
   1.113 +            if (sb.length() != 0) sb.append(sep);
   1.114 +            sb.append(asString(t));
   1.115 +        }
   1.116 +        return sb.toString();
   1.117 +    }
   1.118 +
   1.119 +    private String asString(TypeMirror t) {
   1.120 +        if (t == null)
   1.121 +            return "[typ:null]";
   1.122 +        return t.accept(new SimpleTypeVisitor<String, Void>() {
   1.123 +            @Override
   1.124 +            public String defaultAction(TypeMirror t, Void ignore) {
   1.125 +                return "[typ:" + t.toString() + "]";
   1.126 +            }
   1.127 +
   1.128 +            @Override
   1.129 +            public String visitDeclared(DeclaredType t, Void ignore) {
   1.130 +                checkEqual(t.asElement(), types.asElement(t));
   1.131 +                String s = asString(t.asElement());
   1.132 +                List<? extends TypeMirror> args = t.getTypeArguments();
   1.133 +                if (!args.isEmpty())
   1.134 +                    s += "<" + asString(args, ",") + ">";
   1.135 +                return s;
   1.136 +            }
   1.137 +
   1.138 +            @Override
   1.139 +            public String visitTypeVariable(TypeVariable t, Void ignore) {
   1.140 +                return "tvar " + t;
   1.141 +            }
   1.142 +
   1.143 +            @Override
   1.144 +            public String visitError(ErrorType t, Void ignore) {
   1.145 +                return "!:" + visitDeclared(t, ignore);
   1.146 +            }
   1.147 +        }, null);
   1.148 +    }
   1.149 +
   1.150 +    private String asString(Element e) {
   1.151 +        if (e == null)
   1.152 +            return "[elt:null]";
   1.153 +        return e.accept(new SimpleElementVisitor<String, Void>() {
   1.154 +            @Override
   1.155 +            public String defaultAction(Element e, Void ignore) {
   1.156 +                return "[elt:" + e.getKind() + " " + e.toString() + "]";
   1.157 +            }
   1.158 +            @Override
   1.159 +            public String visitPackage(PackageElement e, Void ignore) {
   1.160 +                return "pkg " + e.getQualifiedName();
   1.161 +            }
   1.162 +            @Override
   1.163 +            public String visitType(TypeElement e, Void ignore) {
   1.164 +                StringBuilder sb = new StringBuilder();
   1.165 +                if (e.getEnclosedElements().isEmpty())
   1.166 +                    sb.append("empty ");
   1.167 +                ElementKind ek = e.getKind();
   1.168 +                switch (ek) {
   1.169 +                    case CLASS:
   1.170 +                        sb.append("clss");
   1.171 +                        break;
   1.172 +                    case INTERFACE:
   1.173 +                        sb.append("intf");
   1.174 +                        break;
   1.175 +                    default:
   1.176 +                        sb.append(ek);
   1.177 +                        break;
   1.178 +                }
   1.179 +                sb.append(" ");
   1.180 +                Element encl = e.getEnclosingElement();
   1.181 +                if (!isUnnamedPackage(encl) && encl.asType().getKind() != TypeKind.NONE) {
   1.182 +                    sb.append("(");
   1.183 +                    sb.append(asString(encl));
   1.184 +                    sb.append(")");
   1.185 +                    sb.append(".");
   1.186 +                }
   1.187 +                sb.append(e.getSimpleName());
   1.188 +                if (e.asType().getKind() == TypeKind.ERROR) sb.append("!");
   1.189 +                return sb.toString();
   1.190 +            }
   1.191 +        }, null);
   1.192 +    }
   1.193 +
   1.194 +    boolean isUnnamedPackage(Element e) {
   1.195 +        return (e != null && e.getKind() == ElementKind.PACKAGE
   1.196 +                && ((PackageElement) e).isUnnamed());
   1.197 +    }
   1.198 +
   1.199 +    void checkEqual(Element e1, Element e2) {
   1.200 +        if (e1 != e2) {
   1.201 +            throw new AssertionError("elements not equal as expected: "
   1.202 +                + e1 + ", " + e2);
   1.203 +        }
   1.204 +    }
   1.205 +}
   1.206 +
   1.207 +
   1.208 +

mercurial