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