1 /*
2  * Copyright (c) 2015, 2019, 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  */
23 
24 /*
25  * @test
26  * @bug 8230105
27  * @summary Do not run Analyzers when Attr crashes with an exception.
28  * @modules jdk.compiler/com.sun.tools.javac.api
29  *          jdk.compiler/com.sun.tools.javac.comp
30  *          jdk.compiler/com.sun.tools.javac.tree
31  *          jdk.compiler/com.sun.tools.javac.util
32  */
33 
34 import java.io.IOException;
35 import java.net.URI;
36 import java.util.Arrays;
37 import java.util.List;
38 import javax.tools.JavaFileObject;
39 import javax.tools.SimpleJavaFileObject;
40 
41 import com.sun.tools.javac.api.JavacTaskImpl;
42 import com.sun.tools.javac.api.JavacTool;
43 import com.sun.tools.javac.comp.Attr;
44 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
45 import com.sun.tools.javac.util.Context;
46 import com.sun.tools.javac.util.Context.Factory;
47 
48 public class DoNoRunAnalyzersWhenException {
49 
main(String... args)50     public static void main(String... args) throws Exception {
51         new DoNoRunAnalyzersWhenException().run();
52     }
53 
run()54     void run() throws IOException {
55         JavacTool tool = JavacTool.create();
56         JavaSource source = new JavaSource("class Test {" +
57                                            "    { String STOP = \"\"; }" +
58                                            "}");
59         Context context = new Context();
60         CrashingAttr.preRegister(context);
61         List<JavaSource> inputs = Arrays.asList(source);
62         JavacTaskImpl task =
63                 (JavacTaskImpl) tool.getTask(null, null, null, List.of("-XDfind=all"), null, inputs, context);
64         try {
65             task.analyze(null);
66             throw new AssertionError("Expected exception not seen.");
67         } catch (StopException ex) {
68             //ok
69         }
70     }
71 
72     static class CrashingAttr extends Attr {
73 
preRegister(Context context)74         static void preRegister(Context context) {
75             context.put(attrKey, (Factory<Attr>) c -> new CrashingAttr(c));
76         }
77 
CrashingAttr(Context context)78         CrashingAttr(Context context) {
79             super(context);
80         }
81 
visitVarDef(JCVariableDecl tree)82         @Override public void visitVarDef(JCVariableDecl tree) {
83             if (tree.name.contentEquals("STOP"))
84                 throw new StopException();
85             super.visitVarDef(tree);
86         }
87     }
88 
89     static class StopException extends NullPointerException {}
90 
91     class JavaSource extends SimpleJavaFileObject {
92 
93         String source;
94 
JavaSource(String source)95         JavaSource(String source) {
96             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
97             this.source = source;
98         }
99 
100         @Override
getCharContent(boolean ignoreEncodingErrors)101         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
102             return source;
103         }
104 
105     }
106 
107 }
108