1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/processing/model/element/TestAnonClassNames.java Wed Mar 03 16:05:34 2010 -0800 1.3 @@ -0,0 +1,186 @@ 1.4 +/* 1.5 + * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6449781 1.30 + * @summary Test that reported names of anonymous classes are non-null. 1.31 + * @author Joseph D. Darcy 1.32 + * @build TestAnonSourceNames 1.33 + * @compile/fail -processor TestAnonSourceNames TestAnonClassNames.java 1.34 + * @build TestAnonClassNames 1.35 + * @run main TestAnonClassNames 1.36 + */ 1.37 + 1.38 +/* 1.39 + * This test operates in phases to test retrieving the qualified name 1.40 + * of anonymous classes from type elements modeling the anonymous 1.41 + * class. The type elements are generated using both source files and 1.42 + * class files as the basis of constructing the elements. 1.43 + * 1.44 + * Source files will be tested by the @compile line which runs 1.45 + * TestAnonSourceNames as an annotation processor over this file. 1.46 + * This compile line is expected to fail until 6930507 is fixed. Once 1.47 + * bug 6930507 is fixed, the "@compile/fail -processor ..." and 1.48 + * following "@build..." steps can be replaced with a single "@compile 1.49 + * -processor ..." directive. 1.50 + * 1.51 + * Class files are tested by the @run command on this type. This 1.52 + * class gets the names of classes with different nesting kinds, 1.53 + * including anonymous classes, and then invokes the compiler with an 1.54 + * annotation processor having the class files names as inputs. The 1.55 + * compiler is invoked via the javax.tools mechanism. 1.56 + */ 1.57 + 1.58 +import java.lang.annotation.*; 1.59 +import javax.lang.model.element.*; 1.60 +import javax.annotation.processing.*; 1.61 +import javax.lang.model.SourceVersion; 1.62 +import javax.lang.model.element.*; 1.63 +import javax.lang.model.util.*; 1.64 +import javax.tools.*; 1.65 +import java.util.*; 1.66 + 1.67 +import static java.lang.annotation.RetentionPolicy.*; 1.68 +import static javax.lang.model.element.NestingKind.*; 1.69 +import static javax.lang.model.util.ElementFilter.*; 1.70 +import static javax.tools.Diagnostic.Kind.*; 1.71 +import static javax.tools.StandardLocation.*; 1.72 + 1.73 +@Nesting(TOP_LEVEL) 1.74 +public class TestAnonClassNames { 1.75 + @Nesting(MEMBER) 1.76 + static class MemberClass1{} 1.77 + 1.78 + @Nesting(MEMBER) 1.79 + class MemberClass2{} 1.80 + 1.81 + @Nesting(MEMBER) 1.82 + class Win$$AtVegas { } // Class with funny name. 1.83 + 1.84 + public static void main(String... argv) { 1.85 + @Nesting(LOCAL) 1.86 + class LocalClass{}; 1.87 + 1.88 + Object o = new @Nesting(ANONYMOUS) Object() { // An anonymous annotated class 1.89 + public String toString() { 1.90 + return "I have no name!"; 1.91 + } 1.92 + }; 1.93 + 1.94 + Class<?>[] classes = { 1.95 + MemberClass1.class, 1.96 + MemberClass2.class, 1.97 + LocalClass.class, 1.98 + Win$$AtVegas.class, 1.99 + o.getClass(), 1.100 + TestAnonClassNames.class, 1.101 + }; 1.102 + 1.103 + for(Class<?> clazz : classes) { 1.104 + String name = clazz.getName(); 1.105 + System.out.format("%s is %s%n", 1.106 + clazz.getName(), 1.107 + clazz.getAnnotation(Nesting.class).value()); 1.108 + testClassName(name); 1.109 + } 1.110 + } 1.111 + 1.112 + /** 1.113 + * Perform annotation processing on the class file name and verify 1.114 + * the existence of different flavors of class names when the 1.115 + * input classes are modeled as elements. 1.116 + */ 1.117 + static void testClassName(String className) { 1.118 + JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); 1.119 + List<String> classNames = new ArrayList<>(); 1.120 + classNames.add(className); 1.121 + 1.122 + List<String> options = new ArrayList<>(); 1.123 + options.add("-proc:only"); 1.124 + options.add("-classpath"); 1.125 + options.add(System.getProperty("test.classes")); 1.126 + 1.127 + JavaCompiler.CompilationTask compileTask = 1.128 + javaCompiler.getTask(null, // Output 1.129 + null, // File manager 1.130 + null, // Diagnostics 1.131 + options, 1.132 + classNames, 1.133 + null); // Sources 1.134 + List<Processor> processors = new ArrayList<>(); 1.135 + processors.add(new ClassNameProber()); 1.136 + compileTask.setProcessors(processors); 1.137 + Boolean goodResult = compileTask.call(); 1.138 + if (!goodResult) { 1.139 + throw new RuntimeException("Errors found during compile."); 1.140 + } 1.141 + } 1.142 +} 1.143 + 1.144 +@Retention(RUNTIME) 1.145 +@interface Nesting { 1.146 + NestingKind value(); 1.147 +} 1.148 + 1.149 +/** 1.150 + * Probe at the various kinds of names of a type element. 1.151 + */ 1.152 +@SupportedAnnotationTypes("*") 1.153 +class ClassNameProber extends AbstractProcessor { 1.154 + public ClassNameProber(){super();} 1.155 + 1.156 + private boolean classesFound=false; 1.157 + 1.158 + public boolean process(Set<? extends TypeElement> annotations, 1.159 + RoundEnvironment roundEnv) { 1.160 + if (!roundEnv.processingOver()) { 1.161 + for(TypeElement typeElt : typesIn(roundEnv.getRootElements())) { 1.162 + classesFound = true; 1.163 + 1.164 + // Verify different names are non-null; an NPE will 1.165 + // result in failed compile status being reported. 1.166 + NestingKind nestingKind = typeElt.getNestingKind(); 1.167 + System.out.printf("\tSimple name: ''%s''\tQualified Name: ''%s''\tKind ''%s''\tNesting ''%s''%n", 1.168 + typeElt.getSimpleName().toString(), 1.169 + typeElt.getQualifiedName().toString(), 1.170 + typeElt.getKind().toString(), 1.171 + nestingKind.toString()); 1.172 + 1.173 + if (typeElt.getAnnotation(Nesting.class).value() != nestingKind) { 1.174 + throw new RuntimeException("Mismatch of expected and reported nesting kind."); 1.175 + } 1.176 + } 1.177 + 1.178 + } 1.179 + 1.180 + if (!classesFound) { 1.181 + throw new RuntimeException("Error: no classes processed."); 1.182 + } 1.183 + return true; 1.184 + } 1.185 + 1.186 + public SourceVersion getSupportedSourceVersion() { 1.187 + return SourceVersion.latest(); 1.188 + } 1.189 +}