|
1 /* |
|
2 * Copyright (c) 2018, Google LLC. 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 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 8204630 |
|
27 * @summary generating an anonymous class with Filer#createClassFile causes an NPE in |
|
28 * JavacProcessingEnvironment |
|
29 * @library /tools/lib /tools/javac/lib/ |
|
30 * @modules jdk.compiler/com.sun.tools.javac.api |
|
31 * jdk.compiler/com.sun.tools.javac.main |
|
32 * jdk.compiler/com.sun.tools.javac.processing |
|
33 * jdk.compiler/com.sun.tools.javac.util |
|
34 * jdk.jdeps/com.sun.tools.javap |
|
35 * @clean * |
|
36 * @build toolbox.ToolBox toolbox.JavacTask |
|
37 * @build GenerateAnonymousClass JavacTestingAbstractProcessor |
|
38 * @compile/ref=GenerateAnonymousClass.out -XDaccessInternalAPI -processor GenerateAnonymousClass -XDrawDiagnostics GenerateAnonymousClass.java |
|
39 */ |
|
40 |
|
41 import com.sun.tools.javac.processing.JavacProcessingEnvironment; |
|
42 import com.sun.tools.javac.processing.PrintingProcessor.PrintingElementVisitor; |
|
43 import com.sun.tools.javac.util.Log; |
|
44 import com.sun.tools.javac.util.Log.WriterKind; |
|
45 import java.io.*; |
|
46 import java.util.*; |
|
47 import javax.annotation.processing.*; |
|
48 import javax.lang.model.element.*; |
|
49 import javax.tools.*; |
|
50 import toolbox.JavacTask; |
|
51 import toolbox.ToolBox; |
|
52 |
|
53 public class GenerateAnonymousClass extends JavacTestingAbstractProcessor { |
|
54 int round = 1; |
|
55 |
|
56 @Override |
|
57 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
|
58 Log log = Log.instance(((JavacProcessingEnvironment) processingEnv).getContext()); |
|
59 PrintWriter pw = log.getWriter(WriterKind.NOTICE); |
|
60 |
|
61 pw.println("round: " + round); |
|
62 |
|
63 TypeElement generatedClass = processingEnv.getElementUtils().getTypeElement("T"); |
|
64 if (generatedClass != null) { |
|
65 new PrintingElementVisitor(pw, processingEnv.getElementUtils()).visit(generatedClass); |
|
66 pw.flush(); |
|
67 } |
|
68 |
|
69 if (round++ == 1) { |
|
70 ToolBox tb = new ToolBox(); |
|
71 ToolBox.MemoryFileManager mfm = new ToolBox.MemoryFileManager(); |
|
72 new JavacTask(tb).fileManager(mfm).sources(GENERATED).run(); |
|
73 |
|
74 try (OutputStream out = filer.createClassFile("T").openOutputStream()) { |
|
75 out.write(mfm.getFileBytes(StandardLocation.CLASS_OUTPUT, "T")); |
|
76 } catch (IOException e) { |
|
77 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.toString()); |
|
78 } |
|
79 try (OutputStream out = filer.createClassFile("T$1").openOutputStream()) { |
|
80 out.write(mfm.getFileBytes(StandardLocation.CLASS_OUTPUT, "T$1")); |
|
81 } catch (IOException e) { |
|
82 processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.toString()); |
|
83 } |
|
84 } |
|
85 |
|
86 return false; |
|
87 } |
|
88 |
|
89 private static final String GENERATED = |
|
90 "public class T {\n" |
|
91 + " public void test() {\n" |
|
92 + " new Object() {};\n" |
|
93 + " }\n" |
|
94 + "}"; |
|
95 } |