46 }
47
48 /* Class containing a local class definition;
49 * compiled class file will have an EnclosedMethod attribute.
50 */
51 static final JavaSource pC =
52 new JavaSource("p/C.java",
53 "package p;\n"
54 + "class C {\n"
55 + " void m() {\n"
56 + " new Runnable() {\n"
57 + " public void run() {\n"
58 + " new Runnable() {\n"
59 + " public void run() { }\n"
60 + " };\n"
61 + " }\n"
62 + " };\n"
63 + " }\n"
64 + "}");
65
66 /* Dummy source file to compile while running anno processor. */
67 static final JavaSource dummy =
68 new JavaSource("Dummy.java",
69 "class Dummy { }");
70
71 void run() throws Exception {
72 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
73 try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
74
75 // step 1: compile test classes
76 File cwd = new File(".");
77 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(cwd));
78 compile(comp, fm, null, null, pC);
79
80 // step 2: verify functioning of processor
81 fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
82 fm.getLocation(StandardLocation.CLASS_PATH));
83 fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(cwd));
84 compile(comp, fm, null, getClass().getName(), dummy);
85
86 File pC_class = new File(new File("p"), "C.class");
87 pC_class.delete();
88
89 DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();
90 compile(comp, fm, dc, getClass().getName(), dummy);
91 List<Diagnostic<? extends JavaFileObject>> diags =dc.getDiagnostics();
92
93 System.err.println(diags);
94 switch (diags.size()) {
95 case 0:
96 throw new Exception("no diagnostics received");
97 case 1:
98 String code = diags.get(0).getCode();
99 String expect = "compiler.err.proc.cant.access.1";
100 if (!expect.equals(code))
101 throw new Exception("unexpected diag code: " + code
102 + ", expected: " + expect);
103 break;
104 default:
105 throw new Exception("unexpected diags received");
106 }
107 }
108 }
109
110 void compile(JavaCompiler comp, JavaFileManager fm,
111 DiagnosticListener<JavaFileObject> dl,
112 String processor, JavaFileObject... files) throws Exception {
113 System.err.println("compile processor:" + processor + ", files:" + Arrays.asList(files));
114 List<String> opts = new ArrayList<String>();
115 if (processor != null) {
116 // opts.add("-verbose");
117 opts.addAll(Arrays.asList("-processor", processor));
118 }
119 CompilationTask task = comp.getTask(null, fm, dl, opts, null, Arrays.asList(files));
120 boolean ok = task.call();
121 if (dl == null && !ok)
122 throw new Exception("compilation failed");
126 JavaSource(String name, String text) {
127 super(URI.create("js://" + name), JavaFileObject.Kind.SOURCE);
128 this.text = text;
129 }
130 @Override
131 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
132 return text;
133 }
134 final String text;
135 }
136
137 // annotation processor method
138
139 @Override
140 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
141 if (!roundEnv.processingOver()) {
142 PackageElement p = elements.getPackageElement("p");
143 List<? extends Element> elems = p.getEnclosedElements();
144 System.err.println("contents of package p: " + elems);
145 if (elems.size() != 1 || !elems.get(0).getSimpleName().contentEquals("C")) {
146 messager.printMessage(Diagnostic.Kind.ERROR, "unexpected package contents");
147 }
148 }
149 return true;
150 }
151 }
152
|
46 }
47
48 /* Class containing a local class definition;
49 * compiled class file will have an EnclosedMethod attribute.
50 */
51 static final JavaSource pC =
52 new JavaSource("p/C.java",
53 "package p;\n"
54 + "class C {\n"
55 + " void m() {\n"
56 + " new Runnable() {\n"
57 + " public void run() {\n"
58 + " new Runnable() {\n"
59 + " public void run() { }\n"
60 + " };\n"
61 + " }\n"
62 + " };\n"
63 + " }\n"
64 + "}");
65
66 private static final String PACKAGE_CONTENT_ERROR = "package does not contain C";
67
68 /* Dummy source file to compile while running anno processor. */
69 static final JavaSource dummy =
70 new JavaSource("Dummy.java",
71 "class Dummy { }");
72
73 void run() throws Exception {
74 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
75 try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
76
77 // step 1: compile test classes
78 File cwd = new File(".");
79 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(cwd));
80 compile(comp, fm, null, null, pC);
81
82 // step 2: verify functioning of processor
83 fm.setLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH,
84 fm.getLocation(StandardLocation.CLASS_PATH));
85 fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(cwd));
86 compile(comp, fm, null, getClass().getName(), dummy);
87
88 File pC_class = new File(new File("p"), "C.class");
89 pC_class.delete();
90
91 DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<JavaFileObject>();
92 compile(comp, fm, dc, getClass().getName(), dummy);
93 List<Diagnostic<? extends JavaFileObject>> diags =dc.getDiagnostics();
94
95 System.err.println(diags);
96 switch (diags.size()) {
97 case 0:
98 throw new Exception("no diagnostics received");
99 case 1:
100 String code = diags.get(0).getCode();
101 String expect = "compiler.err.proc.messager";
102 if (!expect.equals(code))
103 throw new Exception("unexpected diag code: " + code
104 + ", expected: " + expect);
105 String message = diags.get(0).getMessage(null);
106 if (!PACKAGE_CONTENT_ERROR.equals(message)) {
107 throw new Exception("unexpected diag message: " + code
108 + ", expected: " + PACKAGE_CONTENT_ERROR);
109 }
110 break;
111 default:
112 throw new Exception("unexpected diags received");
113 }
114 }
115 }
116
117 void compile(JavaCompiler comp, JavaFileManager fm,
118 DiagnosticListener<JavaFileObject> dl,
119 String processor, JavaFileObject... files) throws Exception {
120 System.err.println("compile processor:" + processor + ", files:" + Arrays.asList(files));
121 List<String> opts = new ArrayList<String>();
122 if (processor != null) {
123 // opts.add("-verbose");
124 opts.addAll(Arrays.asList("-processor", processor));
125 }
126 CompilationTask task = comp.getTask(null, fm, dl, opts, null, Arrays.asList(files));
127 boolean ok = task.call();
128 if (dl == null && !ok)
129 throw new Exception("compilation failed");
133 JavaSource(String name, String text) {
134 super(URI.create("js://" + name), JavaFileObject.Kind.SOURCE);
135 this.text = text;
136 }
137 @Override
138 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
139 return text;
140 }
141 final String text;
142 }
143
144 // annotation processor method
145
146 @Override
147 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
148 if (!roundEnv.processingOver()) {
149 PackageElement p = elements.getPackageElement("p");
150 List<? extends Element> elems = p.getEnclosedElements();
151 System.err.println("contents of package p: " + elems);
152 if (elems.size() != 1 || !elems.get(0).getSimpleName().contentEquals("C")) {
153 messager.printMessage(Diagnostic.Kind.ERROR, PACKAGE_CONTENT_ERROR);
154 }
155 }
156 return true;
157 }
158 }
159
|