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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1521
71f35e4b93a5
parent 0
959103a6100f
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2010, 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  */
    24 /*
    25  * @test
    26  * @bug 6449781 6930508
    27  * @summary Test that reported names of anonymous classes are non-null.
    28  * @author  Joseph D. Darcy
    29  * @library /tools/javac/lib
    30  * @build   JavacTestingAbstractProcessor TestAnonSourceNames
    31  * @compile -processor TestAnonSourceNames TestAnonClassNames.java
    32  * @run main TestAnonClassNames
    33  */
    35 /*
    36  * This test operates in phases to test retrieving the qualified name
    37  * of anonymous classes from type elements modeling the anonymous
    38  * class.  The type elements are generated using both source files and
    39  * class files as the basis of constructing the elements.
    40  *
    41  * Source files will be tested by the @compile line which runs
    42  * TestAnonSourceNames as an annotation processor over this file.
    43  *
    44  * Class files are tested by the @run command on this type.  This
    45  * class gets the names of classes with different nesting kinds,
    46  * including anonymous classes, and then invokes the compiler with an
    47  * annotation processor having the class files names as inputs.  The
    48  * compiler is invoked via the javax.tools mechanism.
    49  */
    51 import java.lang.annotation.*;
    52 import javax.lang.model.element.*;
    53 import javax.annotation.processing.*;
    54 import javax.lang.model.SourceVersion;
    55 import javax.lang.model.element.*;
    56 import javax.lang.model.util.*;
    57 import javax.tools.*;
    58 import java.util.*;
    60 import static java.lang.annotation.RetentionPolicy.*;
    61 import static javax.lang.model.element.NestingKind.*;
    62 import static javax.lang.model.util.ElementFilter.*;
    63 import static javax.tools.Diagnostic.Kind.*;
    64 import static javax.tools.StandardLocation.*;
    66 @Nesting(TOP_LEVEL)
    67 public class TestAnonClassNames {
    68     @Nesting(MEMBER)
    69     static class MemberClass1{}
    71     @Nesting(MEMBER)
    72     class MemberClass2{}
    74     @Nesting(MEMBER)
    75     class Win$$AtVegas { } // Class with funny name.
    77     public static void main(String... argv) {
    78         @Nesting(LOCAL)
    79         class LocalClass{};
    81         Object o =  new @Nesting(ANONYMOUS) Object() { // An anonymous annotated class
    82                 public String toString() {
    83                     return "I have no name!";
    84                 }
    85             };
    87         Class<?>[] classes = {
    88             MemberClass1.class,
    89             MemberClass2.class,
    90             LocalClass.class,
    91             Win$$AtVegas.class,
    92             o.getClass(),
    93             TestAnonClassNames.class,
    94         };
    96         List<String> names = new ArrayList<String>();
    97         for(Class<?> clazz : classes) {
    98             String name = clazz.getName();
    99             System.out.format("%s is %s%n",
   100                               clazz.getName(),
   101                               clazz.getAnnotation(Nesting.class).value());
   102             testClassName(name);
   103             names.add(name);
   104         }
   106         // test all names together
   107         testClassNames(names);
   109         if (errors > 0)
   110             throw new RuntimeException(errors + " errors occurred");
   111     }
   113     /**
   114      * Perform annotation processing on the class file name and verify
   115      * the existence of different flavors of class names when the
   116      * input classes are modeled as elements.
   117      */
   118     static void testClassName(String className) {
   119         testClassNames(Arrays.asList(className));
   120     }
   122     /**
   123      * Perform annotation processing on a list of class file names and verify
   124      * the existence of different flavors of class names when the
   125      * input classes are modeled as elements.
   126      */
   127     static void testClassNames(List<String> classNames) {
   128         System.out.println("test: " + classNames);
   130         List<String> options = new ArrayList<String>();
   131         options.add("-proc:only");
   132         options.add("-classpath");
   133         options.add(System.getProperty("test.classes"));
   135         JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
   136         JavaCompiler.CompilationTask compileTask =
   137             javaCompiler.getTask(null, // Output
   138                                  null, // File manager
   139                                  null, // Diagnostics
   140                                  options,
   141                                  classNames,
   142                                  null); // Sources
   143         List<Processor> processors = new ArrayList<Processor>();
   144         processors.add(new ClassNameProber());
   145         compileTask.setProcessors(processors);
   146         Boolean goodResult = compileTask.call();
   147         if (!goodResult) {
   148             error("Errors found during compile.");
   149         }
   150     }
   152     static int errors = 0;
   154     static void error(String msg) {
   155         System.out.println("Error: " + msg);
   156         errors++;
   157     }
   158 }
   160 @Target({ElementType.TYPE, ElementType.TYPE_USE})
   161 @Retention(RUNTIME)
   162 @interface Nesting {
   163     NestingKind value();
   164 }
   166 /**
   167  * Probe at the various kinds of names of a type element.
   168  */
   169 class ClassNameProber extends JavacTestingAbstractProcessor {
   170     public ClassNameProber(){super();}
   172     private boolean classesFound=false;
   174     public boolean process(Set<? extends TypeElement> annotations,
   175                            RoundEnvironment roundEnv) {
   176         if (!roundEnv.processingOver()) {
   177             for(TypeElement typeElt : typesIn(roundEnv.getRootElements())) {
   178                 classesFound = true;
   180                 // Verify different names are non-null; an NPE will
   181                 // result in failed compile status being reported.
   182                 NestingKind nestingKind = typeElt.getNestingKind();
   183                 System.out.printf("\tSimple name: ''%s''\tQualified Name: ''%s''\tKind ''%s''\tNesting ''%s''%n",
   184                                   typeElt.getSimpleName().toString(),
   185                                   typeElt.getQualifiedName().toString(),
   186                                   typeElt.getKind().toString(),
   187                                   nestingKind.toString());
   189                 if (typeElt.getAnnotation(Nesting.class).value() != nestingKind) {
   190                     throw new RuntimeException("Mismatch of expected and reported nesting kind.");
   191                 }
   192             }
   194         }
   196         if (!classesFound) {
   197             throw new RuntimeException("Error: no classes processed.");
   198         }
   199         return true;
   200     }
   201 }

mercurial