1 /* 2 * Copyright 2010 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any 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/fail -processor TestAnonSourceNames TestAnonClassNames.java 31 * @build TestAnonClassNames 32 * @run main TestAnonClassNames 33 */ 34 35 // NOTE: Once bug 6930507 is fixed, the "@compile/fail -processor ..." 36 // and following "@build..." steps can be replaced with a single 37 // "@compile -processor ..." 38 39 import java.lang.annotation.*; 40 import javax.lang.model.element.*; 41 import javax.annotation.processing.*; 42 import javax.lang.model.SourceVersion; 43 import javax.lang.model.element.*; 44 import javax.lang.model.util.*; 45 import javax.tools.*; 46 import java.util.*; 47 48 import static java.lang.annotation.RetentionPolicy.*; 49 import static javax.lang.model.element.NestingKind.*; 50 import static javax.lang.model.util.ElementFilter.*; 51 import static javax.tools.Diagnostic.Kind.*; 52 import static javax.tools.StandardLocation.*; 53 54 55 @Nesting(TOP_LEVEL) 56 public class TestAnonClassNames { 57 @Nesting(MEMBER) 58 static class MemberClass1{} 59 60 @Nesting(MEMBER) 61 class MemberClass2{} 62 63 @Nesting(MEMBER) 64 class Win$$AtVegas { } // Class with funny name. 65 66 public static void main(String... argv) { 67 @Nesting(LOCAL) 68 class LocalClass{}; 69 70 Object o = new @Nesting(ANONYMOUS) Object() { // An anonymous class 71 public String toString() { 72 return "I have no name!"; 73 } 74 }; 75 76 Class<?>[] classes = { 77 MemberClass1.class, 78 MemberClass2.class, 79 LocalClass.class, 80 Win$$AtVegas.class, 81 o.getClass(), 82 TestAnonClassNames.class, 83 }; 84 85 for(Class<?> clazz : classes) { 86 String name = clazz.getName(); 87 System.out.format("%s is %s%n", 88 clazz.getName(), 89 clazz.getAnnotation(Nesting.class).value()); 90 testClassName(name); 91 } 92 } 93 94 /** 95 * Perform annotation processing on the class file name and verify 96 * the existence of different flavors of class names when the 97 * input classes are modeled as elements. 98 */ 99 static void testClassName(String className) { 100 JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); 101 List<String> classNames = new ArrayList<>(); 102 classNames.add(className); 103 104 List<String> options = new ArrayList<>(); 105 options.add("-proc:only"); 106 options.add("-classpath"); 107 options.add(System.getProperty("test.classes")); 108 109 JavaCompiler.CompilationTask compileTask = 110 javaCompiler.getTask(null, // Output 111 null, // File manager 112 null, // Diagnostics 113 options, 114 classNames, 115 null); // Sources 116 List<Processor> processors = new ArrayList<>(); 117 processors.add(new ClassNameProber()); 118 compileTask.setProcessors(processors); 119 Boolean goodResult = compileTask.call(); 120 if (!goodResult) { 121 throw new RuntimeException("Errors found during compile."); 122 } 123 } 124 } 125 126 @Retention(RUNTIME) 127 @interface Nesting { 128 NestingKind value(); 129 } 130 131 132 /** 133 * Probe at the various kinds of names of a type element. 134 */ 135 @SupportedAnnotationTypes("*") 136 class ClassNameProber extends AbstractProcessor { 137 public ClassNameProber(){super();} 138 139 private boolean classesFound=false; 140 141 public boolean process(Set<? extends TypeElement> annotations, 142 RoundEnvironment roundEnv) { 143 if (!roundEnv.processingOver()) { 144 for(TypeElement typeElt : typesIn(roundEnv.getRootElements())) { 145 classesFound = true; 146 147 // Verify different names are non-null; an NPE will 148 // result in failed compile status being reported. 149 NestingKind nestingKind = typeElt.getNestingKind(); 150 System.out.printf("\tSimple name: ''%s''\tQualified Name: ''%s''\tKind ''%s''\tNesting ''%s''%n", 151 typeElt.getSimpleName().toString(), 152 typeElt.getQualifiedName().toString(), 153 typeElt.getKind().toString(), 154 nestingKind.toString()); 155 156 if (typeElt.getAnnotation(Nesting.class).value() != nestingKind) { 157 throw new RuntimeException("Mismatch of expected and reported nesting kind."); 158 } 159 } 160 161 } 162 163 if (!classesFound) { 164 throw new RuntimeException("Error: no classes processed."); 165 } 166 return true; 167 } 168 169 public SourceVersion getSupportedSourceVersion() { 170 return SourceVersion.latest(); 171 } 172 }