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

Wed, 29 Sep 2010 23:27:57 -0700

author
darcy
date
Wed, 29 Sep 2010 23:27:57 -0700
changeset 699
d2aaaec153e8
parent 672
ea54372637a5
child 722
4851ff2ffc10
permissions
-rw-r--r--

6983738: Use a JavacTestingAbstractProcessor
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2010, 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
    27  * @summary Test that reported names of anonymous classes are non-null.
    28  * @author  Joseph D. Darcy
    29  * @library ../../../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         for(Class<?> clazz : classes) {
    97             String name = clazz.getName();
    98             System.out.format("%s is %s%n",
    99                               clazz.getName(),
   100                               clazz.getAnnotation(Nesting.class).value());
   101             testClassName(name);
   102         }
   103     }
   105     /**
   106      * Perform annotation processing on the class file name and verify
   107      * the existence of different flavors of class names when the
   108      * input classes are modeled as elements.
   109      */
   110     static void testClassName(String className) {
   111         JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
   112         List<String> classNames = new ArrayList<String>();
   113         classNames.add(className);
   115         List<String> options = new ArrayList<String>();
   116         options.add("-proc:only");
   117         options.add("-classpath");
   118         options.add(System.getProperty("test.classes"));
   120         JavaCompiler.CompilationTask compileTask =
   121             javaCompiler.getTask(null, // Output
   122                                  null, // File manager
   123                                  null, // Diagnostics
   124                                  options,
   125                                  classNames,
   126                                  null); // Sources
   127         List<Processor> processors = new ArrayList<Processor>();
   128         processors.add(new ClassNameProber());
   129         compileTask.setProcessors(processors);
   130         Boolean goodResult = compileTask.call();
   131         if (!goodResult) {
   132             throw new RuntimeException("Errors found during compile.");
   133         }
   134     }
   135 }
   137 @Retention(RUNTIME)
   138 @interface Nesting {
   139     NestingKind value();
   140 }
   142 /**
   143  * Probe at the various kinds of names of a type element.
   144  */
   145 class ClassNameProber extends JavacTestingAbstractProcessor {
   146     public ClassNameProber(){super();}
   148     private boolean classesFound=false;
   150     public boolean process(Set<? extends TypeElement> annotations,
   151                            RoundEnvironment roundEnv) {
   152         if (!roundEnv.processingOver()) {
   153             for(TypeElement typeElt : typesIn(roundEnv.getRootElements())) {
   154                 classesFound = true;
   156                 // Verify different names are non-null; an NPE will
   157                 // result in failed compile status being reported.
   158                 NestingKind nestingKind = typeElt.getNestingKind();
   159                 System.out.printf("\tSimple name: ''%s''\tQualified Name: ''%s''\tKind ''%s''\tNesting ''%s''%n",
   160                                   typeElt.getSimpleName().toString(),
   161                                   typeElt.getQualifiedName().toString(),
   162                                   typeElt.getKind().toString(),
   163                                   nestingKind.toString());
   165                 if (typeElt.getAnnotation(Nesting.class).value() != nestingKind) {
   166                     throw new RuntimeException("Mismatch of expected and reported nesting kind.");
   167                 }
   168             }
   170         }
   172         if (!classesFound) {
   173             throw new RuntimeException("Error: no classes processed.");
   174         }
   175         return true;
   176     }
   177 }

mercurial