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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial