Wed, 03 Mar 2010 16:05:34 -0800
6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java Tue Mar 02 16:43:29 2010 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java Wed Mar 03 16:05:34 2010 -0800 1.3 @@ -1135,7 +1135,7 @@ 1.4 self.name = simpleBinaryName(self.flatname, c.flatname) ; 1.5 self.owner = m != null ? m : c; 1.6 if (self.name.isEmpty()) 1.7 - self.fullname = null; 1.8 + self.fullname = names.empty; 1.9 else 1.10 self.fullname = ClassSymbol.formFullName(self.name, self.owner); 1.11
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/processing/model/element/TestAnonClassNames.java Wed Mar 03 16:05:34 2010 -0800 2.3 @@ -0,0 +1,186 @@ 2.4 +/* 2.5 + * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 2.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 2.24 + * have any questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 6449781 2.30 + * @summary Test that reported names of anonymous classes are non-null. 2.31 + * @author Joseph D. Darcy 2.32 + * @build TestAnonSourceNames 2.33 + * @compile/fail -processor TestAnonSourceNames TestAnonClassNames.java 2.34 + * @build TestAnonClassNames 2.35 + * @run main TestAnonClassNames 2.36 + */ 2.37 + 2.38 +/* 2.39 + * This test operates in phases to test retrieving the qualified name 2.40 + * of anonymous classes from type elements modeling the anonymous 2.41 + * class. The type elements are generated using both source files and 2.42 + * class files as the basis of constructing the elements. 2.43 + * 2.44 + * Source files will be tested by the @compile line which runs 2.45 + * TestAnonSourceNames as an annotation processor over this file. 2.46 + * This compile line is expected to fail until 6930507 is fixed. Once 2.47 + * bug 6930507 is fixed, the "@compile/fail -processor ..." and 2.48 + * following "@build..." steps can be replaced with a single "@compile 2.49 + * -processor ..." directive. 2.50 + * 2.51 + * Class files are tested by the @run command on this type. This 2.52 + * class gets the names of classes with different nesting kinds, 2.53 + * including anonymous classes, and then invokes the compiler with an 2.54 + * annotation processor having the class files names as inputs. The 2.55 + * compiler is invoked via the javax.tools mechanism. 2.56 + */ 2.57 + 2.58 +import java.lang.annotation.*; 2.59 +import javax.lang.model.element.*; 2.60 +import javax.annotation.processing.*; 2.61 +import javax.lang.model.SourceVersion; 2.62 +import javax.lang.model.element.*; 2.63 +import javax.lang.model.util.*; 2.64 +import javax.tools.*; 2.65 +import java.util.*; 2.66 + 2.67 +import static java.lang.annotation.RetentionPolicy.*; 2.68 +import static javax.lang.model.element.NestingKind.*; 2.69 +import static javax.lang.model.util.ElementFilter.*; 2.70 +import static javax.tools.Diagnostic.Kind.*; 2.71 +import static javax.tools.StandardLocation.*; 2.72 + 2.73 +@Nesting(TOP_LEVEL) 2.74 +public class TestAnonClassNames { 2.75 + @Nesting(MEMBER) 2.76 + static class MemberClass1{} 2.77 + 2.78 + @Nesting(MEMBER) 2.79 + class MemberClass2{} 2.80 + 2.81 + @Nesting(MEMBER) 2.82 + class Win$$AtVegas { } // Class with funny name. 2.83 + 2.84 + public static void main(String... argv) { 2.85 + @Nesting(LOCAL) 2.86 + class LocalClass{}; 2.87 + 2.88 + Object o = new @Nesting(ANONYMOUS) Object() { // An anonymous annotated class 2.89 + public String toString() { 2.90 + return "I have no name!"; 2.91 + } 2.92 + }; 2.93 + 2.94 + Class<?>[] classes = { 2.95 + MemberClass1.class, 2.96 + MemberClass2.class, 2.97 + LocalClass.class, 2.98 + Win$$AtVegas.class, 2.99 + o.getClass(), 2.100 + TestAnonClassNames.class, 2.101 + }; 2.102 + 2.103 + for(Class<?> clazz : classes) { 2.104 + String name = clazz.getName(); 2.105 + System.out.format("%s is %s%n", 2.106 + clazz.getName(), 2.107 + clazz.getAnnotation(Nesting.class).value()); 2.108 + testClassName(name); 2.109 + } 2.110 + } 2.111 + 2.112 + /** 2.113 + * Perform annotation processing on the class file name and verify 2.114 + * the existence of different flavors of class names when the 2.115 + * input classes are modeled as elements. 2.116 + */ 2.117 + static void testClassName(String className) { 2.118 + JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); 2.119 + List<String> classNames = new ArrayList<>(); 2.120 + classNames.add(className); 2.121 + 2.122 + List<String> options = new ArrayList<>(); 2.123 + options.add("-proc:only"); 2.124 + options.add("-classpath"); 2.125 + options.add(System.getProperty("test.classes")); 2.126 + 2.127 + JavaCompiler.CompilationTask compileTask = 2.128 + javaCompiler.getTask(null, // Output 2.129 + null, // File manager 2.130 + null, // Diagnostics 2.131 + options, 2.132 + classNames, 2.133 + null); // Sources 2.134 + List<Processor> processors = new ArrayList<>(); 2.135 + processors.add(new ClassNameProber()); 2.136 + compileTask.setProcessors(processors); 2.137 + Boolean goodResult = compileTask.call(); 2.138 + if (!goodResult) { 2.139 + throw new RuntimeException("Errors found during compile."); 2.140 + } 2.141 + } 2.142 +} 2.143 + 2.144 +@Retention(RUNTIME) 2.145 +@interface Nesting { 2.146 + NestingKind value(); 2.147 +} 2.148 + 2.149 +/** 2.150 + * Probe at the various kinds of names of a type element. 2.151 + */ 2.152 +@SupportedAnnotationTypes("*") 2.153 +class ClassNameProber extends AbstractProcessor { 2.154 + public ClassNameProber(){super();} 2.155 + 2.156 + private boolean classesFound=false; 2.157 + 2.158 + public boolean process(Set<? extends TypeElement> annotations, 2.159 + RoundEnvironment roundEnv) { 2.160 + if (!roundEnv.processingOver()) { 2.161 + for(TypeElement typeElt : typesIn(roundEnv.getRootElements())) { 2.162 + classesFound = true; 2.163 + 2.164 + // Verify different names are non-null; an NPE will 2.165 + // result in failed compile status being reported. 2.166 + NestingKind nestingKind = typeElt.getNestingKind(); 2.167 + System.out.printf("\tSimple name: ''%s''\tQualified Name: ''%s''\tKind ''%s''\tNesting ''%s''%n", 2.168 + typeElt.getSimpleName().toString(), 2.169 + typeElt.getQualifiedName().toString(), 2.170 + typeElt.getKind().toString(), 2.171 + nestingKind.toString()); 2.172 + 2.173 + if (typeElt.getAnnotation(Nesting.class).value() != nestingKind) { 2.174 + throw new RuntimeException("Mismatch of expected and reported nesting kind."); 2.175 + } 2.176 + } 2.177 + 2.178 + } 2.179 + 2.180 + if (!classesFound) { 2.181 + throw new RuntimeException("Error: no classes processed."); 2.182 + } 2.183 + return true; 2.184 + } 2.185 + 2.186 + public SourceVersion getSupportedSourceVersion() { 2.187 + return SourceVersion.latest(); 2.188 + } 2.189 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/processing/model/element/TestAnonSourceNames.java Wed Mar 03 16:05:34 2010 -0800 3.3 @@ -0,0 +1,92 @@ 3.4 +/* 3.5 + * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 3.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 3.24 + * have any questions. 3.25 + */ 3.26 + 3.27 +import java.io.*; 3.28 +import javax.annotation.processing.*; 3.29 +import javax.lang.model.*; 3.30 +import javax.lang.model.element.*; 3.31 +import javax.lang.model.type.*; 3.32 +import javax.lang.model.util.*; 3.33 +import java.util.*; 3.34 +import com.sun.source.tree.*; 3.35 +import com.sun.source.util.*; 3.36 +import static javax.tools.Diagnostic.Kind.*; 3.37 + 3.38 +/** 3.39 + * Using the tree API, retrieve element representations of anonymous 3.40 + * classes and verify their names are as specified. 3.41 + */ 3.42 +@SupportedAnnotationTypes("*") 3.43 +public class TestAnonSourceNames extends AbstractProcessor { 3.44 + 3.45 + public boolean process(Set<? extends TypeElement> annotations, 3.46 + RoundEnvironment roundEnv) { 3.47 + if (!roundEnv.processingOver()) { 3.48 + Trees trees = Trees.instance(processingEnv); 3.49 + 3.50 + for(Element rootElement : roundEnv.getRootElements()) { 3.51 + TreePath treePath = trees.getPath(rootElement); 3.52 + 3.53 + (new ClassTreeScanner(trees)). 3.54 + scan(trees.getTree(rootElement), 3.55 + treePath.getCompilationUnit()); 3.56 + } 3.57 + } 3.58 + return true; 3.59 + } 3.60 + 3.61 + class ClassTreeScanner extends TreeScanner<Void, CompilationUnitTree> { 3.62 + private Trees trees; 3.63 + 3.64 + public ClassTreeScanner(Trees trees) { 3.65 + super(); 3.66 + this.trees = trees; 3.67 + } 3.68 + @Override 3.69 + public Void visitClass(ClassTree node, CompilationUnitTree cu) { 3.70 + Element element = trees.getElement(trees.getPath(cu, node)); 3.71 + if (element == null) { 3.72 + processingEnv.getMessager().printMessage(ERROR, 3.73 + "No element retreived for node named ''" + 3.74 + node.getSimpleName() + "''."); 3.75 + } else { 3.76 + 3.77 + System.out.println("\nVisiting class ``" + element.getSimpleName() + 3.78 + "'' of kind " + element.getKind()); 3.79 + if (element instanceof TypeElement) { 3.80 + TypeElement typeElement = (TypeElement) element; 3.81 + String s = typeElement.getQualifiedName().toString(); 3.82 + System.out.println("\tqualified name:" + s); 3.83 + } else { 3.84 + throw new RuntimeException("TypeElement not gotten from ClassTree."); 3.85 + } 3.86 + } 3.87 + return super.visitClass(node, cu); 3.88 + } 3.89 + } 3.90 + 3.91 + @Override 3.92 + public SourceVersion getSupportedSourceVersion() { 3.93 + return SourceVersion.latest(); 3.94 + } 3.95 +}