1 /*
2  * Copyright (c) 2011, 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 7086261
27  * @summary javac doesn't report error as expected, it only reports ClientCodeWrapper$DiagnosticSourceUnwrapper
28  * @modules jdk.compiler/com.sun.tools.javac.api
29  *          jdk.compiler/com.sun.tools.javac.util
30  */
31 
32 import javax.tools.*;
33 
34 import com.sun.tools.javac.api.ClientCodeWrapper.DiagnosticSourceUnwrapper;
35 import com.sun.tools.javac.util.JCDiagnostic;
36 
37 import java.net.URI;
38 import java.util.Arrays;
39 
40 import static javax.tools.StandardLocation.*;
41 import static javax.tools.JavaFileObject.Kind.*;
42 
43 
44 public class T7086261 {
45 
46     static class ErroneousSource extends SimpleJavaFileObject {
ErroneousSource()47         public ErroneousSource() {
48             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
49         }
getCharContent(boolean ignoreEncodingErrors)50         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
51             return "class Test { NonexistentClass c = null; }";
52         }
53     }
54 
55     static class DiagnosticChecker implements DiagnosticListener<javax.tools.JavaFileObject> {
report(Diagnostic message)56         public void report(Diagnostic message) {
57             if (!(message instanceof DiagnosticSourceUnwrapper)) {
58                 throw new AssertionError("Wrapped diagnostic expected!");
59             }
60             String actual = message.toString();
61             JCDiagnostic jd = (JCDiagnostic)((DiagnosticSourceUnwrapper)message).d;
62             String expected = jd.toString();
63             if (!actual.equals(expected)) {
64                 throw new AssertionError("expected = " + expected + "\nfound = " + actual);
65             }
66         }
67     };
68 
test()69     void test() throws Throwable {
70         JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
71         try (JavaFileManager jfm = javac.getStandardFileManager(null, null, null)) {
72             JavaCompiler.CompilationTask task =
73                     javac.getTask(null, jfm, new DiagnosticChecker(), null, null, Arrays.asList(new ErroneousSource()));
74             task.call();
75         }
76     }
77 
main(String[] args)78     public static void main(String[] args) throws Throwable {
79         new T7086261().test();
80     }
81 }
82