1/* 2 * Copyright (c) 2012, 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 8003280 27 * @summary Add lambda tests 28 * check that all diagnostics are dumped to output when compiler exits abruptly 29 * @modules jdk.compiler 30 */ 31 32import com.sun.source.util.JavacTask; 33import java.io.BufferedWriter; 34import java.io.IOException; 35import java.io.StringWriter; 36import java.net.URI; 37import java.net.URL; 38import java.util.Arrays; 39import javax.tools.Diagnostic; 40import javax.tools.JavaCompiler; 41import javax.tools.JavaFileObject; 42import javax.tools.SimpleJavaFileObject; 43import javax.tools.StandardJavaFileManager; 44import javax.tools.ToolProvider; 45 46public class Abort { 47 48 public static void main(String... args) throws Exception { 49 50 String SCRATCH_DIR = System.getProperty("user.dir"); 51 JavaCompiler javacTool = ToolProvider.getSystemJavaCompiler(); 52 java.io.File testDir = new java.io.File(SCRATCH_DIR); 53 54 sourceA.dumpTo(testDir); 55 sourceB.dumpTo(testDir); 56 57 DiagnosticChecker diagChecker = new DiagnosticChecker(); 58 JavacTask ct = (JavacTask)javacTool.getTask(null, null, diagChecker, 59 Arrays.asList("-XDrawDiagnostics", "-cp", testDir.getAbsolutePath()), 60 null, Arrays.asList(sourceA.asJFO(testDir))); 61 try { 62 ct.analyze(); 63 } catch (Throwable ex) { 64 //ignore abort exception thrown by javac 65 } 66 67 if (!diagChecker.errorFound) { 68 throw new AssertionError("Missing diagnostic"); 69 } 70 } 71 72 static class JavaSource { 73 String contents; 74 String filename; 75 76 public JavaSource(String filename, String contents) { 77 this.filename = filename; 78 this.contents = contents; 79 } 80 81 void dumpTo(java.io.File loc) throws Exception { 82 java.io.File file = new java.io.File(loc, filename); 83 java.io.BufferedWriter bw = new java.io.BufferedWriter(new java.io.FileWriter(file)); 84 bw.append(contents); 85 bw.close(); 86 } 87 88 SimpleJavaFileObject asJFO(java.io.File dir) { 89 return new SimpleJavaFileObject(new java.io.File(dir, filename).toURI(), JavaFileObject.Kind.SOURCE) { 90 @Override 91 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { 92 return contents; 93 } 94 }; 95 } 96 } 97 98 static JavaSource sourceA = new JavaSource("Abort.java", "public class Abort {\n" + 99 " public static void main(String[] args) {\n" + 100 " System.out.println(C.m());\n" + 101 " }\n" + 102 "}"); 103 104 static JavaSource sourceB = new JavaSource("C.java", "package com.example;\n" + 105 "public class C {\n" + 106 " public static String m() { return null; }\n" + 107 "}"); 108 109 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { 110 111 boolean errorFound; 112 113 public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 114 if (diagnostic.getKind() == Diagnostic.Kind.ERROR && 115 diagnostic.getCode().contains("compiler.err.cant.access")) { 116 errorFound = true; 117 } 118 } 119 } 120} 121