Wed, 03 Mar 2010 16:05:34 -0800
6449781: TypeElement.getQualifiedName for anonymous classes returns null instead of an empty name
Reviewed-by: jjg
darcy@515 | 1 | /* |
darcy@515 | 2 | * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. |
darcy@515 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
darcy@515 | 4 | * |
darcy@515 | 5 | * This code is free software; you can redistribute it and/or modify it |
darcy@515 | 6 | * under the terms of the GNU General Public License version 2 only, as |
darcy@515 | 7 | * published by the Free Software Foundation. |
darcy@515 | 8 | * |
darcy@515 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
darcy@515 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
darcy@515 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
darcy@515 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
darcy@515 | 13 | * accompanied this code). |
darcy@515 | 14 | * |
darcy@515 | 15 | * You should have received a copy of the GNU General Public License version |
darcy@515 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
darcy@515 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
darcy@515 | 18 | * |
darcy@515 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
darcy@515 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
darcy@515 | 21 | * have any questions. |
darcy@515 | 22 | */ |
darcy@515 | 23 | |
darcy@515 | 24 | /* |
darcy@515 | 25 | * @test |
darcy@515 | 26 | * @bug 6449781 |
darcy@515 | 27 | * @summary Test that reported names of anonymous classes are non-null. |
darcy@515 | 28 | * @author Joseph D. Darcy |
darcy@515 | 29 | * @build TestAnonSourceNames |
darcy@515 | 30 | * @compile/fail -processor TestAnonSourceNames TestAnonClassNames.java |
darcy@515 | 31 | * @build TestAnonClassNames |
darcy@515 | 32 | * @run main TestAnonClassNames |
darcy@515 | 33 | */ |
darcy@515 | 34 | |
darcy@515 | 35 | /* |
darcy@515 | 36 | * This test operates in phases to test retrieving the qualified name |
darcy@515 | 37 | * of anonymous classes from type elements modeling the anonymous |
darcy@515 | 38 | * class. The type elements are generated using both source files and |
darcy@515 | 39 | * class files as the basis of constructing the elements. |
darcy@515 | 40 | * |
darcy@515 | 41 | * Source files will be tested by the @compile line which runs |
darcy@515 | 42 | * TestAnonSourceNames as an annotation processor over this file. |
darcy@515 | 43 | * This compile line is expected to fail until 6930507 is fixed. Once |
darcy@515 | 44 | * bug 6930507 is fixed, the "@compile/fail -processor ..." and |
darcy@515 | 45 | * following "@build..." steps can be replaced with a single "@compile |
darcy@515 | 46 | * -processor ..." directive. |
darcy@515 | 47 | * |
darcy@515 | 48 | * Class files are tested by the @run command on this type. This |
darcy@515 | 49 | * class gets the names of classes with different nesting kinds, |
darcy@515 | 50 | * including anonymous classes, and then invokes the compiler with an |
darcy@515 | 51 | * annotation processor having the class files names as inputs. The |
darcy@515 | 52 | * compiler is invoked via the javax.tools mechanism. |
darcy@515 | 53 | */ |
darcy@515 | 54 | |
darcy@515 | 55 | import java.lang.annotation.*; |
darcy@515 | 56 | import javax.lang.model.element.*; |
darcy@515 | 57 | import javax.annotation.processing.*; |
darcy@515 | 58 | import javax.lang.model.SourceVersion; |
darcy@515 | 59 | import javax.lang.model.element.*; |
darcy@515 | 60 | import javax.lang.model.util.*; |
darcy@515 | 61 | import javax.tools.*; |
darcy@515 | 62 | import java.util.*; |
darcy@515 | 63 | |
darcy@515 | 64 | import static java.lang.annotation.RetentionPolicy.*; |
darcy@515 | 65 | import static javax.lang.model.element.NestingKind.*; |
darcy@515 | 66 | import static javax.lang.model.util.ElementFilter.*; |
darcy@515 | 67 | import static javax.tools.Diagnostic.Kind.*; |
darcy@515 | 68 | import static javax.tools.StandardLocation.*; |
darcy@515 | 69 | |
darcy@515 | 70 | @Nesting(TOP_LEVEL) |
darcy@515 | 71 | public class TestAnonClassNames { |
darcy@515 | 72 | @Nesting(MEMBER) |
darcy@515 | 73 | static class MemberClass1{} |
darcy@515 | 74 | |
darcy@515 | 75 | @Nesting(MEMBER) |
darcy@515 | 76 | class MemberClass2{} |
darcy@515 | 77 | |
darcy@515 | 78 | @Nesting(MEMBER) |
darcy@515 | 79 | class Win$$AtVegas { } // Class with funny name. |
darcy@515 | 80 | |
darcy@515 | 81 | public static void main(String... argv) { |
darcy@515 | 82 | @Nesting(LOCAL) |
darcy@515 | 83 | class LocalClass{}; |
darcy@515 | 84 | |
darcy@515 | 85 | Object o = new @Nesting(ANONYMOUS) Object() { // An anonymous annotated class |
darcy@515 | 86 | public String toString() { |
darcy@515 | 87 | return "I have no name!"; |
darcy@515 | 88 | } |
darcy@515 | 89 | }; |
darcy@515 | 90 | |
darcy@515 | 91 | Class<?>[] classes = { |
darcy@515 | 92 | MemberClass1.class, |
darcy@515 | 93 | MemberClass2.class, |
darcy@515 | 94 | LocalClass.class, |
darcy@515 | 95 | Win$$AtVegas.class, |
darcy@515 | 96 | o.getClass(), |
darcy@515 | 97 | TestAnonClassNames.class, |
darcy@515 | 98 | }; |
darcy@515 | 99 | |
darcy@515 | 100 | for(Class<?> clazz : classes) { |
darcy@515 | 101 | String name = clazz.getName(); |
darcy@515 | 102 | System.out.format("%s is %s%n", |
darcy@515 | 103 | clazz.getName(), |
darcy@515 | 104 | clazz.getAnnotation(Nesting.class).value()); |
darcy@515 | 105 | testClassName(name); |
darcy@515 | 106 | } |
darcy@515 | 107 | } |
darcy@515 | 108 | |
darcy@515 | 109 | /** |
darcy@515 | 110 | * Perform annotation processing on the class file name and verify |
darcy@515 | 111 | * the existence of different flavors of class names when the |
darcy@515 | 112 | * input classes are modeled as elements. |
darcy@515 | 113 | */ |
darcy@515 | 114 | static void testClassName(String className) { |
darcy@515 | 115 | JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); |
darcy@515 | 116 | List<String> classNames = new ArrayList<>(); |
darcy@515 | 117 | classNames.add(className); |
darcy@515 | 118 | |
darcy@515 | 119 | List<String> options = new ArrayList<>(); |
darcy@515 | 120 | options.add("-proc:only"); |
darcy@515 | 121 | options.add("-classpath"); |
darcy@515 | 122 | options.add(System.getProperty("test.classes")); |
darcy@515 | 123 | |
darcy@515 | 124 | JavaCompiler.CompilationTask compileTask = |
darcy@515 | 125 | javaCompiler.getTask(null, // Output |
darcy@515 | 126 | null, // File manager |
darcy@515 | 127 | null, // Diagnostics |
darcy@515 | 128 | options, |
darcy@515 | 129 | classNames, |
darcy@515 | 130 | null); // Sources |
darcy@515 | 131 | List<Processor> processors = new ArrayList<>(); |
darcy@515 | 132 | processors.add(new ClassNameProber()); |
darcy@515 | 133 | compileTask.setProcessors(processors); |
darcy@515 | 134 | Boolean goodResult = compileTask.call(); |
darcy@515 | 135 | if (!goodResult) { |
darcy@515 | 136 | throw new RuntimeException("Errors found during compile."); |
darcy@515 | 137 | } |
darcy@515 | 138 | } |
darcy@515 | 139 | } |
darcy@515 | 140 | |
darcy@515 | 141 | @Retention(RUNTIME) |
darcy@515 | 142 | @interface Nesting { |
darcy@515 | 143 | NestingKind value(); |
darcy@515 | 144 | } |
darcy@515 | 145 | |
darcy@515 | 146 | /** |
darcy@515 | 147 | * Probe at the various kinds of names of a type element. |
darcy@515 | 148 | */ |
darcy@515 | 149 | @SupportedAnnotationTypes("*") |
darcy@515 | 150 | class ClassNameProber extends AbstractProcessor { |
darcy@515 | 151 | public ClassNameProber(){super();} |
darcy@515 | 152 | |
darcy@515 | 153 | private boolean classesFound=false; |
darcy@515 | 154 | |
darcy@515 | 155 | public boolean process(Set<? extends TypeElement> annotations, |
darcy@515 | 156 | RoundEnvironment roundEnv) { |
darcy@515 | 157 | if (!roundEnv.processingOver()) { |
darcy@515 | 158 | for(TypeElement typeElt : typesIn(roundEnv.getRootElements())) { |
darcy@515 | 159 | classesFound = true; |
darcy@515 | 160 | |
darcy@515 | 161 | // Verify different names are non-null; an NPE will |
darcy@515 | 162 | // result in failed compile status being reported. |
darcy@515 | 163 | NestingKind nestingKind = typeElt.getNestingKind(); |
darcy@515 | 164 | System.out.printf("\tSimple name: ''%s''\tQualified Name: ''%s''\tKind ''%s''\tNesting ''%s''%n", |
darcy@515 | 165 | typeElt.getSimpleName().toString(), |
darcy@515 | 166 | typeElt.getQualifiedName().toString(), |
darcy@515 | 167 | typeElt.getKind().toString(), |
darcy@515 | 168 | nestingKind.toString()); |
darcy@515 | 169 | |
darcy@515 | 170 | if (typeElt.getAnnotation(Nesting.class).value() != nestingKind) { |
darcy@515 | 171 | throw new RuntimeException("Mismatch of expected and reported nesting kind."); |
darcy@515 | 172 | } |
darcy@515 | 173 | } |
darcy@515 | 174 | |
darcy@515 | 175 | } |
darcy@515 | 176 | |
darcy@515 | 177 | if (!classesFound) { |
darcy@515 | 178 | throw new RuntimeException("Error: no classes processed."); |
darcy@515 | 179 | } |
darcy@515 | 180 | return true; |
darcy@515 | 181 | } |
darcy@515 | 182 | |
darcy@515 | 183 | public SourceVersion getSupportedSourceVersion() { |
darcy@515 | 184 | return SourceVersion.latest(); |
darcy@515 | 185 | } |
darcy@515 | 186 | } |