test/tools/javac/processing/model/element/TestAnonClassNames.java

changeset 937
a2399c8db703
parent 722
4851ff2ffc10
child 1466
b52a38d4536c
equal deleted inserted replaced
936:f2f04935ef3f 937:a2399c8db703
1 /* 1 /*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 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. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
21 * questions. 21 * questions.
22 */ 22 */
23 23
24 /* 24 /*
25 * @test 25 * @test
26 * @bug 6449781 26 * @bug 6449781 6930508
27 * @summary Test that reported names of anonymous classes are non-null. 27 * @summary Test that reported names of anonymous classes are non-null.
28 * @author Joseph D. Darcy 28 * @author Joseph D. Darcy
29 * @library ../../../lib 29 * @library ../../../lib
30 * @build JavacTestingAbstractProcessor TestAnonSourceNames 30 * @build JavacTestingAbstractProcessor TestAnonSourceNames
31 * @compile -processor TestAnonSourceNames TestAnonClassNames.java 31 * @compile -processor TestAnonSourceNames TestAnonClassNames.java
91 Win$$AtVegas.class, 91 Win$$AtVegas.class,
92 o.getClass(), 92 o.getClass(),
93 TestAnonClassNames.class, 93 TestAnonClassNames.class,
94 }; 94 };
95 95
96 List<String> names = new ArrayList<String>();
96 for(Class<?> clazz : classes) { 97 for(Class<?> clazz : classes) {
97 String name = clazz.getName(); 98 String name = clazz.getName();
98 Nesting anno = clazz.getAnnotation(Nesting.class); 99 Nesting anno = clazz.getAnnotation(Nesting.class);
99 System.out.format("%s is %s%n", 100 System.out.format("%s is %s%n",
100 clazz.getName(), 101 clazz.getName(),
101 anno == null ? "(unset/ANONYMOUS)" : anno.value()); 102 anno == null ? "(unset/ANONYMOUS)" : anno.value());
102 testClassName(name); 103 testClassName(name);
103 } 104 names.add(name);
105 }
106
107 // test all names together
108 testClassNames(names);
109
110 if (errors > 0)
111 throw new RuntimeException(errors + " errors occurred");
104 } 112 }
105 113
106 /** 114 /**
107 * Perform annotation processing on the class file name and verify 115 * Perform annotation processing on the class file name and verify
108 * the existence of different flavors of class names when the 116 * the existence of different flavors of class names when the
109 * input classes are modeled as elements. 117 * input classes are modeled as elements.
110 */ 118 */
111 static void testClassName(String className) { 119 static void testClassName(String className) {
112 JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler(); 120 testClassNames(Arrays.asList(className));
113 List<String> classNames = new ArrayList<String>(); 121 }
114 classNames.add(className); 122
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);
115 130
116 List<String> options = new ArrayList<String>(); 131 List<String> options = new ArrayList<String>();
117 options.add("-proc:only"); 132 options.add("-proc:only");
118 options.add("-classpath"); 133 options.add("-classpath");
119 options.add(System.getProperty("test.classes")); 134 options.add(System.getProperty("test.classes"));
120 135
136 JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
121 JavaCompiler.CompilationTask compileTask = 137 JavaCompiler.CompilationTask compileTask =
122 javaCompiler.getTask(null, // Output 138 javaCompiler.getTask(null, // Output
123 null, // File manager 139 null, // File manager
124 null, // Diagnostics 140 null, // Diagnostics
125 options, 141 options,
128 List<Processor> processors = new ArrayList<Processor>(); 144 List<Processor> processors = new ArrayList<Processor>();
129 processors.add(new ClassNameProber()); 145 processors.add(new ClassNameProber());
130 compileTask.setProcessors(processors); 146 compileTask.setProcessors(processors);
131 Boolean goodResult = compileTask.call(); 147 Boolean goodResult = compileTask.call();
132 if (!goodResult) { 148 if (!goodResult) {
133 throw new RuntimeException("Errors found during compile."); 149 error("Errors found during compile.");
134 } 150 }
151 }
152
153 static int errors = 0;
154
155 static void error(String msg) {
156 System.out.println("Error: " + msg);
157 errors++;
135 } 158 }
136 } 159 }
137 160
138 @Retention(RUNTIME) 161 @Retention(RUNTIME)
139 @interface Nesting { 162 @interface Nesting {

mercurial