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 */ 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 */ 51 52 import java.lang.annotation.*; 53 import javax.lang.model.element.*; 54 import javax.annotation.processing.*; 55 import javax.lang.model.SourceVersion; 56 import javax.lang.model.element.*; 57 import javax.lang.model.util.*; 58 import javax.tools.*; 59 import java.util.*; 60 61 import static java.lang.annotation.RetentionPolicy.*; 62 import static javax.lang.model.element.NestingKind.*; 63 import static javax.lang.model.util.ElementFilter.*; 64 import static javax.tools.Diagnostic.Kind.*; 65 import static javax.tools.StandardLocation.*; 66 67 @Nesting(TOP_LEVEL) 68 public class TestAnonClassNames { 69 @Nesting(MEMBER) 70 static class MemberClass1{} 71 72 @Nesting(MEMBER) 73 class MemberClass2{} 74 75 @Nesting(MEMBER) 76 class Win$$AtVegas { } // Class with funny name. 77 78 public static void main(String... argv) { 79 @Nesting(LOCAL) 80 class LocalClass{}; 81 82 Object o = new @Nesting(ANONYMOUS) Object() { // An anonymous annotated class 83 public String toString() { 84 return "I have no name!"; 85 } 86 }; 87 88 Class<?>[] classes = { 89 MemberClass1.class, 90 MemberClass2.class, 91 LocalClass.class, 92 Win$$AtVegas.class, 93 o.getClass(), 94 TestAnonClassNames.class, 95 }; 96 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 } 104 } 105 106 /** 107 * Perform annotation processing on the class file name and verify 108 * the existence of different flavors of class names when the 109 * input classes are modeled as elements. 110 */ 111 static void testClassName(String className) { 112 JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); 113 List<String> classNames = new ArrayList<String>(); 114 classNames.add(className); 115 116 List<String> options = new ArrayList<String>(); 117 options.add("-proc:only"); 118 options.add("-classpath"); 119 options.add(System.getProperty("test.classes")); 120 121 JavaCompiler.CompilationTask compileTask = 122 javaCompiler.getTask(null, // Output 123 null, // File manager 124 null, // Diagnostics 125 options, 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 }