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


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


 126                                  classNames,
 127                                  null); // Sources
 128         List<Processor> processors = new ArrayList<Processor>();
 129         processors.add(new ClassNameProber());
 130         compileTask.setProcessors(processors);
 131         Boolean goodResult = compileTask.call();
 132         if (!goodResult) {
 133             throw new RuntimeException("Errors found during compile.");
 134         }
 135     }
 136 }
 137 
 138 @Retention(RUNTIME)
 139 @interface Nesting {
 140     NestingKind value();
 141 }
 142 
 143 /**
 144  * Probe at the various kinds of names of a type element.
 145  */
 146 class ClassNameProber extends JavacTestingAbstractProcessor {

 147     public ClassNameProber(){super();}
 148 
 149     private boolean classesFound=false;
 150 
 151     public boolean process(Set<? extends TypeElement> annotations,
 152                            RoundEnvironment roundEnv) {
 153         if (!roundEnv.processingOver()) {
 154             for(TypeElement typeElt : typesIn(roundEnv.getRootElements())) {
 155                 classesFound = true;
 156 
 157                 // Verify different names are non-null; an NPE will
 158                 // result in failed compile status being reported.
 159                 NestingKind nestingKind = typeElt.getNestingKind();
 160                 System.out.printf("\tSimple name: ''%s''\tQualified Name: ''%s''\tKind ''%s''\tNesting ''%s''%n",
 161                                   typeElt.getSimpleName().toString(),
 162                                   typeElt.getQualifiedName().toString(),
 163                                   typeElt.getKind().toString(),
 164                                   nestingKind.toString());
 165 
 166                 if (typeElt.getAnnotation(Nesting.class).value() != nestingKind) {
 167                     throw new RuntimeException("Mismatch of expected and reported nesting kind.");
 168                 }
 169             }
 170 
 171         }
 172 
 173         if (!classesFound) {
 174             throw new RuntimeException("Error: no classes processed.");
 175         }
 176         return true;
 177     }




 178 }