|
1 /* |
|
2 * Copyright 2010 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 6449781 |
|
27 * @summary Test that reported names of anonymous classes are non-null. |
|
28 * @author Joseph D. Darcy |
|
29 * @build TestAnonSourceNames |
|
30 * @compile/fail -processor TestAnonSourceNames TestAnonClassNames.java |
|
31 * @build TestAnonClassNames |
|
32 * @run main TestAnonClassNames |
|
33 */ |
|
34 |
|
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 * This compile line is expected to fail until 6930507 is fixed. Once |
|
44 * bug 6930507 is fixed, the "@compile/fail -processor ..." and |
|
45 * following "@build..." steps can be replaced with a single "@compile |
|
46 * -processor ..." directive. |
|
47 * |
|
48 * Class files are tested by the @run command on this type. This |
|
49 * class gets the names of classes with different nesting kinds, |
|
50 * including anonymous classes, and then invokes the compiler with an |
|
51 * annotation processor having the class files names as inputs. The |
|
52 * compiler is invoked via the javax.tools mechanism. |
|
53 */ |
|
54 |
|
55 import java.lang.annotation.*; |
|
56 import javax.lang.model.element.*; |
|
57 import javax.annotation.processing.*; |
|
58 import javax.lang.model.SourceVersion; |
|
59 import javax.lang.model.element.*; |
|
60 import javax.lang.model.util.*; |
|
61 import javax.tools.*; |
|
62 import java.util.*; |
|
63 |
|
64 import static java.lang.annotation.RetentionPolicy.*; |
|
65 import static javax.lang.model.element.NestingKind.*; |
|
66 import static javax.lang.model.util.ElementFilter.*; |
|
67 import static javax.tools.Diagnostic.Kind.*; |
|
68 import static javax.tools.StandardLocation.*; |
|
69 |
|
70 @Nesting(TOP_LEVEL) |
|
71 public class TestAnonClassNames { |
|
72 @Nesting(MEMBER) |
|
73 static class MemberClass1{} |
|
74 |
|
75 @Nesting(MEMBER) |
|
76 class MemberClass2{} |
|
77 |
|
78 @Nesting(MEMBER) |
|
79 class Win$$AtVegas { } // Class with funny name. |
|
80 |
|
81 public static void main(String... argv) { |
|
82 @Nesting(LOCAL) |
|
83 class LocalClass{}; |
|
84 |
|
85 Object o = new @Nesting(ANONYMOUS) Object() { // An anonymous annotated class |
|
86 public String toString() { |
|
87 return "I have no name!"; |
|
88 } |
|
89 }; |
|
90 |
|
91 Class<?>[] classes = { |
|
92 MemberClass1.class, |
|
93 MemberClass2.class, |
|
94 LocalClass.class, |
|
95 Win$$AtVegas.class, |
|
96 o.getClass(), |
|
97 TestAnonClassNames.class, |
|
98 }; |
|
99 |
|
100 for(Class<?> clazz : classes) { |
|
101 String name = clazz.getName(); |
|
102 System.out.format("%s is %s%n", |
|
103 clazz.getName(), |
|
104 clazz.getAnnotation(Nesting.class).value()); |
|
105 testClassName(name); |
|
106 } |
|
107 } |
|
108 |
|
109 /** |
|
110 * Perform annotation processing on the class file name and verify |
|
111 * the existence of different flavors of class names when the |
|
112 * input classes are modeled as elements. |
|
113 */ |
|
114 static void testClassName(String className) { |
|
115 JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); |
|
116 List<String> classNames = new ArrayList<>(); |
|
117 classNames.add(className); |
|
118 |
|
119 List<String> options = new ArrayList<>(); |
|
120 options.add("-proc:only"); |
|
121 options.add("-classpath"); |
|
122 options.add(System.getProperty("test.classes")); |
|
123 |
|
124 JavaCompiler.CompilationTask compileTask = |
|
125 javaCompiler.getTask(null, // Output |
|
126 null, // File manager |
|
127 null, // Diagnostics |
|
128 options, |
|
129 classNames, |
|
130 null); // Sources |
|
131 List<Processor> processors = new ArrayList<>(); |
|
132 processors.add(new ClassNameProber()); |
|
133 compileTask.setProcessors(processors); |
|
134 Boolean goodResult = compileTask.call(); |
|
135 if (!goodResult) { |
|
136 throw new RuntimeException("Errors found during compile."); |
|
137 } |
|
138 } |
|
139 } |
|
140 |
|
141 @Retention(RUNTIME) |
|
142 @interface Nesting { |
|
143 NestingKind value(); |
|
144 } |
|
145 |
|
146 /** |
|
147 * Probe at the various kinds of names of a type element. |
|
148 */ |
|
149 @SupportedAnnotationTypes("*") |
|
150 class ClassNameProber extends AbstractProcessor { |
|
151 public ClassNameProber(){super();} |
|
152 |
|
153 private boolean classesFound=false; |
|
154 |
|
155 public boolean process(Set<? extends TypeElement> annotations, |
|
156 RoundEnvironment roundEnv) { |
|
157 if (!roundEnv.processingOver()) { |
|
158 for(TypeElement typeElt : typesIn(roundEnv.getRootElements())) { |
|
159 classesFound = true; |
|
160 |
|
161 // Verify different names are non-null; an NPE will |
|
162 // result in failed compile status being reported. |
|
163 NestingKind nestingKind = typeElt.getNestingKind(); |
|
164 System.out.printf("\tSimple name: ''%s''\tQualified Name: ''%s''\tKind ''%s''\tNesting ''%s''%n", |
|
165 typeElt.getSimpleName().toString(), |
|
166 typeElt.getQualifiedName().toString(), |
|
167 typeElt.getKind().toString(), |
|
168 nestingKind.toString()); |
|
169 |
|
170 if (typeElt.getAnnotation(Nesting.class).value() != nestingKind) { |
|
171 throw new RuntimeException("Mismatch of expected and reported nesting kind."); |
|
172 } |
|
173 } |
|
174 |
|
175 } |
|
176 |
|
177 if (!classesFound) { |
|
178 throw new RuntimeException("Error: no classes processed."); |
|
179 } |
|
180 return true; |
|
181 } |
|
182 |
|
183 public SourceVersion getSupportedSourceVersion() { |
|
184 return SourceVersion.latest(); |
|
185 } |
|
186 } |