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