aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8009227 aoqi@0: * @summary Certain diagnostics should not be deferred aoqi@0: */ aoqi@0: aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import java.io.BufferedWriter; aoqi@0: import java.io.File; aoqi@0: import java.io.FileWriter; aoqi@0: import java.io.IOException; aoqi@0: import java.util.Arrays; aoqi@0: import javax.tools.Diagnostic; aoqi@0: import javax.tools.JavaCompiler; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.SimpleJavaFileObject; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: public class CompletionFailure { aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: aoqi@0: String SCRATCH_DIR = System.getProperty("user.dir"); aoqi@0: JavaCompiler javacTool = ToolProvider.getSystemJavaCompiler(); aoqi@0: File scratchDir = new File(SCRATCH_DIR); aoqi@0: aoqi@0: sourceA.dumpTo(scratchDir); aoqi@0: sourceB.dumpTo(scratchDir); aoqi@0: aoqi@0: JavacTask ct = (JavacTask)javacTool.getTask(null, null, null, aoqi@0: null, null, Arrays.asList(sourceA.asJFO(scratchDir), sourceB.asJFO(scratchDir))); aoqi@0: aoqi@0: ct.generate(); aoqi@0: aoqi@0: remove(scratchDir, "A.java"); aoqi@0: remove(scratchDir, "B.java"); aoqi@0: remove(scratchDir, "A.class"); aoqi@0: aoqi@0: sourceC.dumpTo(scratchDir); aoqi@0: sourceD.dumpTo(scratchDir); aoqi@0: aoqi@0: DiagnosticChecker diagChecker = new DiagnosticChecker(); aoqi@0: ct = (JavacTask)javacTool.getTask(null, null, diagChecker, aoqi@0: Arrays.asList("-XDrawDiagnostics", "-cp", scratchDir.getAbsolutePath()), aoqi@0: null, Arrays.asList(sourceC.asJFO(scratchDir), sourceD.asJFO(scratchDir))); aoqi@0: try { aoqi@0: ct.analyze(); aoqi@0: } catch (Throwable ex) { aoqi@0: //ignore abort exception thrown by javac aoqi@0: } aoqi@0: aoqi@0: if (!diagChecker.errorFound) { aoqi@0: throw new AssertionError("Missing diagnostic"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static void remove(File dir, String fileName) { aoqi@0: File fileToRemove = new File(dir, fileName); aoqi@0: fileToRemove.delete(); aoqi@0: } aoqi@0: aoqi@0: static class JavaSource { aoqi@0: String contents; aoqi@0: String filename; aoqi@0: aoqi@0: public JavaSource(String filename, String contents) { aoqi@0: this.filename = filename; aoqi@0: this.contents = contents; aoqi@0: } aoqi@0: aoqi@0: void dumpTo(java.io.File loc) throws Exception { aoqi@0: File file = new File(loc, filename); aoqi@0: BufferedWriter bw = new java.io.BufferedWriter(new FileWriter(file)); aoqi@0: bw.append(contents); aoqi@0: bw.close(); aoqi@0: } aoqi@0: aoqi@0: SimpleJavaFileObject asJFO(java.io.File dir) { aoqi@0: return new SimpleJavaFileObject(new File(dir, filename).toURI(), JavaFileObject.Kind.SOURCE) { aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { aoqi@0: return contents; aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static JavaSource sourceA = new JavaSource("A.java", "public interface A { }\n"); aoqi@0: aoqi@0: static JavaSource sourceB = new JavaSource("B.java", "public class B implements A {\n" + aoqi@0: " public static Object n() { return null; }\n" + aoqi@0: "}\n"); aoqi@0: aoqi@0: static JavaSource sourceC = new JavaSource("C.java", "public class C {\n" + aoqi@0: " void test(B b) {\n" + aoqi@0: " D.m(B.n());\n" + aoqi@0: "} }\n"); aoqi@0: aoqi@0: static JavaSource sourceD = new JavaSource("D.java", "public class D {\n" + aoqi@0: " static void m(Object o) { }\n" + aoqi@0: "}\n"); aoqi@0: aoqi@0: static class DiagnosticChecker implements javax.tools.DiagnosticListener { aoqi@0: aoqi@0: boolean errorFound; aoqi@0: aoqi@0: public void report(Diagnostic diagnostic) { aoqi@0: if (diagnostic.getKind() == Diagnostic.Kind.ERROR && aoqi@0: diagnostic.getCode().contains("compiler.err.cant.access")) { aoqi@0: errorFound = true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: }