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

Print this page




   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 6449781
  27  * @summary Test that reported names of anonymous classes are non-null.
  28  * @author  Joseph D. Darcy
  29  * @build TestAnonSourceNames

  30  * @compile -processor TestAnonSourceNames TestAnonClassNames.java
  31  * @run main TestAnonClassNames
  32  */
  33 
  34 /*
  35  * This test operates in phases to test retrieving the qualified name
  36  * of anonymous classes from type elements modeling the anonymous
  37  * class.  The type elements are generated using both source files and
  38  * class files as the basis of constructing the elements.
  39  *
  40  * Source files will be tested by the @compile line which runs
  41  * TestAnonSourceNames as an annotation processor over this file.
  42  *
  43  * Class files are tested by the @run command on this type.  This
  44  * class gets the names of classes with different nesting kinds,
  45  * including anonymous classes, and then invokes the compiler with an
  46  * annotation processor having the class files names as inputs.  The
  47  * compiler is invoked via the javax.tools mechanism.
  48  */
  49 


 124                                  classNames,
 125                                  null); // Sources
 126         List<Processor> processors = new ArrayList<Processor>();
 127         processors.add(new ClassNameProber());
 128         compileTask.setProcessors(processors);
 129         Boolean goodResult = compileTask.call();
 130         if (!goodResult) {
 131             throw new RuntimeException("Errors found during compile.");
 132         }
 133     }
 134 }
 135 
 136 @Retention(RUNTIME)
 137 @interface Nesting {
 138     NestingKind value();
 139 }
 140 
 141 /**
 142  * Probe at the various kinds of names of a type element.
 143  */
 144 @SupportedAnnotationTypes("*")
 145 class ClassNameProber extends AbstractProcessor {
 146     public ClassNameProber(){super();}
 147 
 148     private boolean classesFound=false;
 149 
 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;
 155 
 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());
 164 
 165                 if (typeElt.getAnnotation(Nesting.class).value() != nestingKind) {
 166                     throw new RuntimeException("Mismatch of expected and reported nesting kind.");
 167                 }
 168             }
 169 
 170         }
 171 
 172         if (!classesFound) {
 173             throw new RuntimeException("Error: no classes processed.");
 174         }
 175         return true;
 176     }
 177 
 178     public SourceVersion getSupportedSourceVersion() {
 179         return SourceVersion.latest();
 180     }
 181 }


   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 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  */
  34 
  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  */
  50 


 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 }
 136 
 137 @Retention(RUNTIME)
 138 @interface Nesting {
 139     NestingKind value();
 140 }
 141 
 142 /**
 143  * Probe at the various kinds of names of a type element.
 144  */
 145 class ClassNameProber extends JavacTestingAbstractProcessor {

 146     public ClassNameProber(){super();}
 147 
 148     private boolean classesFound=false;
 149 
 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;
 155 
 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());
 164 
 165                 if (typeElt.getAnnotation(Nesting.class).value() != nestingKind) {
 166                     throw new RuntimeException("Mismatch of expected and reported nesting kind.");
 167                 }
 168             }
 169 
 170         }
 171 
 172         if (!classesFound) {
 173             throw new RuntimeException("Error: no classes processed.");
 174         }
 175         return true;
 176     }




 177 }