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