1 /*
2  * Copyright (c) 2013, 2015, 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 8009227
27  * @summary Certain diagnostics should not be deferred
28  * @modules jdk.compiler
29  */
30 
31 import com.sun.source.util.JavacTask;
32 import java.io.BufferedWriter;
33 import java.io.File;
34 import java.io.FileWriter;
35 import java.io.IOException;
36 import java.util.Arrays;
37 import javax.tools.Diagnostic;
38 import javax.tools.JavaCompiler;
39 import javax.tools.JavaFileObject;
40 import javax.tools.SimpleJavaFileObject;
41 import javax.tools.ToolProvider;
42 
43 public class CompletionFailure {
44 
main(String... args)45     public static void main(String... args) throws Exception {
46 
47         String SCRATCH_DIR = System.getProperty("user.dir");
48         JavaCompiler javacTool = ToolProvider.getSystemJavaCompiler();
49         File scratchDir = new File(SCRATCH_DIR);
50 
51         sourceA.dumpTo(scratchDir);
52         sourceB.dumpTo(scratchDir);
53 
54         JavacTask ct = (JavacTask)javacTool.getTask(null, null, null,
55                 null, null, Arrays.asList(sourceA.asJFO(scratchDir), sourceB.asJFO(scratchDir)));
56 
57         ct.generate();
58 
59         remove(scratchDir, "A.java");
60         remove(scratchDir, "B.java");
61         remove(scratchDir, "A.class");
62 
63         sourceC.dumpTo(scratchDir);
64         sourceD.dumpTo(scratchDir);
65 
66         DiagnosticChecker diagChecker = new DiagnosticChecker();
67         ct = (JavacTask)javacTool.getTask(null, null, diagChecker,
68                 Arrays.asList("-XDrawDiagnostics", "-cp", scratchDir.getAbsolutePath()),
69                 null, Arrays.asList(sourceC.asJFO(scratchDir), sourceD.asJFO(scratchDir)));
70         try {
71             ct.analyze();
72         } catch (Throwable ex) {
73             //ignore abort exception thrown by javac
74         }
75 
76         if (!diagChecker.errorFound) {
77             throw new AssertionError("Missing diagnostic");
78         }
79     }
80 
remove(File dir, String fileName)81     static void remove(File dir, String fileName) {
82         File fileToRemove = new File(dir, fileName);
83         fileToRemove.delete();
84     }
85 
86     static class JavaSource {
87         String contents;
88         String filename;
89 
JavaSource(String filename, String contents)90         public JavaSource(String filename, String contents) {
91             this.filename =  filename;
92             this.contents = contents;
93         }
94 
dumpTo(java.io.File loc)95         void dumpTo(java.io.File loc) throws Exception {
96             File file = new File(loc, filename);
97             BufferedWriter bw = new java.io.BufferedWriter(new FileWriter(file));
98             bw.append(contents);
99             bw.close();
100         }
101 
asJFO(java.io.File dir)102         SimpleJavaFileObject asJFO(java.io.File dir) {
103             return new SimpleJavaFileObject(new File(dir, filename).toURI(), JavaFileObject.Kind.SOURCE) {
104                 @Override
105                 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
106                     return contents;
107                 }
108             };
109         }
110     }
111 
112     static JavaSource sourceA = new JavaSource("A.java", "public interface A { }\n");
113 
114     static JavaSource sourceB = new JavaSource("B.java", "public class B implements A {\n" +
115                                                "   public static Object n() { return null; }\n" +
116                                                "}\n");
117 
118     static JavaSource sourceC = new JavaSource("C.java", "public class C {\n" +
119                                                "   void test(B b) {\n" +
120                                                "      D.m(B.n());\n" +
121                                                "}  }\n");
122 
123     static JavaSource sourceD = new JavaSource("D.java", "public class D {\n" +
124                                                "   static void m(Object o) { }\n" +
125                                                "}\n");
126 
127     static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
128 
129         boolean errorFound;
130 
131         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
132             if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&
133                     diagnostic.getCode().contains("compiler.err.cant.access")) {
134                 errorFound = true;
135             }
136         }
137     }
138 }
139