Wed, 16 Mar 2011 17:21:52 -0700
6930508: Passing nested class names on javac command line interfere with subsequent name -> class lookup
Reviewed-by: darcy
darcy@515 | 1 | /* |
jjg@937 | 2 | * Copyright (c) 2010, 2011, Oracle and/or its affiliates. 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 | * |
ohair@554 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 20 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 21 | * questions. |
darcy@515 | 22 | */ |
darcy@515 | 23 | |
darcy@515 | 24 | /* |
darcy@515 | 25 | * @test |
jjg@937 | 26 | * @bug 6449781 6930508 |
darcy@515 | 27 | * @summary Test that reported names of anonymous classes are non-null. |
darcy@515 | 28 | * @author Joseph D. Darcy |
darcy@699 | 29 | * @library ../../../lib |
darcy@699 | 30 | * @build JavacTestingAbstractProcessor TestAnonSourceNames |
jjg@672 | 31 | * @compile -processor TestAnonSourceNames TestAnonClassNames.java |
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 | * |
darcy@515 | 44 | * Class files are tested by the @run command on this type. This |
darcy@515 | 45 | * class gets the names of classes with different nesting kinds, |
darcy@515 | 46 | * including anonymous classes, and then invokes the compiler with an |
darcy@515 | 47 | * annotation processor having the class files names as inputs. The |
darcy@515 | 48 | * compiler is invoked via the javax.tools mechanism. |
darcy@515 | 49 | */ |
darcy@515 | 50 | |
darcy@515 | 51 | import java.lang.annotation.*; |
darcy@515 | 52 | import javax.lang.model.element.*; |
darcy@515 | 53 | import javax.annotation.processing.*; |
darcy@515 | 54 | import javax.lang.model.SourceVersion; |
darcy@515 | 55 | import javax.lang.model.element.*; |
darcy@515 | 56 | import javax.lang.model.util.*; |
darcy@515 | 57 | import javax.tools.*; |
darcy@515 | 58 | import java.util.*; |
darcy@515 | 59 | |
darcy@515 | 60 | import static java.lang.annotation.RetentionPolicy.*; |
darcy@515 | 61 | import static javax.lang.model.element.NestingKind.*; |
darcy@515 | 62 | import static javax.lang.model.util.ElementFilter.*; |
darcy@515 | 63 | import static javax.tools.Diagnostic.Kind.*; |
darcy@515 | 64 | import static javax.tools.StandardLocation.*; |
darcy@515 | 65 | |
darcy@515 | 66 | @Nesting(TOP_LEVEL) |
darcy@515 | 67 | public class TestAnonClassNames { |
darcy@515 | 68 | @Nesting(MEMBER) |
darcy@515 | 69 | static class MemberClass1{} |
darcy@515 | 70 | |
darcy@515 | 71 | @Nesting(MEMBER) |
darcy@515 | 72 | class MemberClass2{} |
darcy@515 | 73 | |
darcy@515 | 74 | @Nesting(MEMBER) |
darcy@515 | 75 | class Win$$AtVegas { } // Class with funny name. |
darcy@515 | 76 | |
darcy@515 | 77 | public static void main(String... argv) { |
darcy@515 | 78 | @Nesting(LOCAL) |
darcy@515 | 79 | class LocalClass{}; |
darcy@515 | 80 | |
jjg@722 | 81 | Object o = new /*@Nesting(ANONYMOUS)*/ Object() { // An anonymous annotated class |
darcy@515 | 82 | public String toString() { |
darcy@515 | 83 | return "I have no name!"; |
darcy@515 | 84 | } |
darcy@515 | 85 | }; |
darcy@515 | 86 | |
darcy@515 | 87 | Class<?>[] classes = { |
darcy@515 | 88 | MemberClass1.class, |
darcy@515 | 89 | MemberClass2.class, |
darcy@515 | 90 | LocalClass.class, |
darcy@515 | 91 | Win$$AtVegas.class, |
darcy@515 | 92 | o.getClass(), |
darcy@515 | 93 | TestAnonClassNames.class, |
darcy@515 | 94 | }; |
darcy@515 | 95 | |
jjg@937 | 96 | List<String> names = new ArrayList<String>(); |
darcy@515 | 97 | for(Class<?> clazz : classes) { |
darcy@515 | 98 | String name = clazz.getName(); |
jjg@722 | 99 | Nesting anno = clazz.getAnnotation(Nesting.class); |
darcy@515 | 100 | System.out.format("%s is %s%n", |
darcy@515 | 101 | clazz.getName(), |
jjg@722 | 102 | anno == null ? "(unset/ANONYMOUS)" : anno.value()); |
darcy@515 | 103 | testClassName(name); |
jjg@937 | 104 | names.add(name); |
darcy@515 | 105 | } |
jjg@937 | 106 | |
jjg@937 | 107 | // test all names together |
jjg@937 | 108 | testClassNames(names); |
jjg@937 | 109 | |
jjg@937 | 110 | if (errors > 0) |
jjg@937 | 111 | throw new RuntimeException(errors + " errors occurred"); |
darcy@515 | 112 | } |
darcy@515 | 113 | |
darcy@515 | 114 | /** |
darcy@515 | 115 | * Perform annotation processing on the class file name and verify |
darcy@515 | 116 | * the existence of different flavors of class names when the |
darcy@515 | 117 | * input classes are modeled as elements. |
darcy@515 | 118 | */ |
darcy@515 | 119 | static void testClassName(String className) { |
jjg@937 | 120 | testClassNames(Arrays.asList(className)); |
jjg@937 | 121 | } |
jjg@937 | 122 | |
jjg@937 | 123 | /** |
jjg@937 | 124 | * Perform annotation processing on a list of class file names and verify |
jjg@937 | 125 | * the existence of different flavors of class names when the |
jjg@937 | 126 | * input classes are modeled as elements. |
jjg@937 | 127 | */ |
jjg@937 | 128 | static void testClassNames(List<String> classNames) { |
jjg@937 | 129 | System.out.println("test: " + classNames); |
darcy@515 | 130 | |
mcimadamore@536 | 131 | List<String> options = new ArrayList<String>(); |
darcy@515 | 132 | options.add("-proc:only"); |
darcy@515 | 133 | options.add("-classpath"); |
darcy@515 | 134 | options.add(System.getProperty("test.classes")); |
darcy@515 | 135 | |
jjg@937 | 136 | JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); |
darcy@515 | 137 | JavaCompiler.CompilationTask compileTask = |
darcy@515 | 138 | javaCompiler.getTask(null, // Output |
darcy@515 | 139 | null, // File manager |
darcy@515 | 140 | null, // Diagnostics |
darcy@515 | 141 | options, |
darcy@515 | 142 | classNames, |
darcy@515 | 143 | null); // Sources |
mcimadamore@536 | 144 | List<Processor> processors = new ArrayList<Processor>(); |
darcy@515 | 145 | processors.add(new ClassNameProber()); |
darcy@515 | 146 | compileTask.setProcessors(processors); |
darcy@515 | 147 | Boolean goodResult = compileTask.call(); |
darcy@515 | 148 | if (!goodResult) { |
jjg@937 | 149 | error("Errors found during compile."); |
darcy@515 | 150 | } |
darcy@515 | 151 | } |
jjg@937 | 152 | |
jjg@937 | 153 | static int errors = 0; |
jjg@937 | 154 | |
jjg@937 | 155 | static void error(String msg) { |
jjg@937 | 156 | System.out.println("Error: " + msg); |
jjg@937 | 157 | errors++; |
jjg@937 | 158 | } |
darcy@515 | 159 | } |
darcy@515 | 160 | |
darcy@515 | 161 | @Retention(RUNTIME) |
darcy@515 | 162 | @interface Nesting { |
darcy@515 | 163 | NestingKind value(); |
darcy@515 | 164 | } |
darcy@515 | 165 | |
darcy@515 | 166 | /** |
darcy@515 | 167 | * Probe at the various kinds of names of a type element. |
darcy@515 | 168 | */ |
darcy@699 | 169 | class ClassNameProber extends JavacTestingAbstractProcessor { |
darcy@515 | 170 | public ClassNameProber(){super();} |
darcy@515 | 171 | |
darcy@515 | 172 | private boolean classesFound=false; |
darcy@515 | 173 | |
darcy@515 | 174 | public boolean process(Set<? extends TypeElement> annotations, |
darcy@515 | 175 | RoundEnvironment roundEnv) { |
darcy@515 | 176 | if (!roundEnv.processingOver()) { |
darcy@515 | 177 | for(TypeElement typeElt : typesIn(roundEnv.getRootElements())) { |
darcy@515 | 178 | classesFound = true; |
darcy@515 | 179 | |
darcy@515 | 180 | // Verify different names are non-null; an NPE will |
darcy@515 | 181 | // result in failed compile status being reported. |
darcy@515 | 182 | NestingKind nestingKind = typeElt.getNestingKind(); |
darcy@515 | 183 | System.out.printf("\tSimple name: ''%s''\tQualified Name: ''%s''\tKind ''%s''\tNesting ''%s''%n", |
darcy@515 | 184 | typeElt.getSimpleName().toString(), |
darcy@515 | 185 | typeElt.getQualifiedName().toString(), |
darcy@515 | 186 | typeElt.getKind().toString(), |
darcy@515 | 187 | nestingKind.toString()); |
jjg@722 | 188 | Nesting anno = typeElt.getAnnotation(Nesting.class); |
jjg@722 | 189 | if ((anno == null ? NestingKind.ANONYMOUS : anno.value()) != nestingKind) { |
darcy@515 | 190 | throw new RuntimeException("Mismatch of expected and reported nesting kind."); |
darcy@515 | 191 | } |
darcy@515 | 192 | } |
darcy@515 | 193 | |
darcy@515 | 194 | } |
darcy@515 | 195 | |
darcy@515 | 196 | if (!classesFound) { |
darcy@515 | 197 | throw new RuntimeException("Error: no classes processed."); |
darcy@515 | 198 | } |
darcy@515 | 199 | return true; |
darcy@515 | 200 | } |
darcy@515 | 201 | } |