1/*
2 * Copyright (c) 2006, 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     6427274 6347778 6469079
27 * @summary Various bugs fixed while writing Compiler API Guide
28 * @author  Peter von der Ah\u0081
29 * @library ../lib
30 * @modules java.compiler
31 *          jdk.compiler
32 * @build ToolTester
33 * @compile Test.java
34 * @run main Test
35 */
36
37import javax.tools.*;
38import java.io.File;
39import java.util.Collections;
40
41public class Test extends ToolTester {
42
43    public boolean success = false;
44
45    class DiagnosticTester implements DiagnosticListener<JavaFileObject> {
46        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
47            if (diagnostic.getKind() == Diagnostic.Kind.NOTE) {
48                try {
49                    // 6427274: FileObject.openReader throws exception
50                    // 6347778: getSource() returns null for notes
51                    diagnostic.getSource().openReader(true).getClass();
52                } catch (Exception ex) {
53                    throw new AssertionError(ex);
54                }
55                success = true;
56            }
57        }
58    }
59
60    void test(String... args) throws Exception {
61        final Iterable<? extends JavaFileObject> compilationUnits =
62            fm.getJavaFileObjects(new File(test_src, "TestMe.java"));
63        task = tool.getTask(null, fm, new DiagnosticTester(), null, null, compilationUnits);
64        if (!task.call())
65            throw new AssertionError("Compilation failed");
66        if (!success)
67            throw new AssertionError("Did not see a NOTE");
68        // 6427274: openReader throws exception
69        fm.getFileForInput(StandardLocation.PLATFORM_CLASS_PATH,
70                           "java.lang",
71                           "Object.class").openReader(true).getClass();
72        DiagnosticCollector<JavaFileObject> diags = new DiagnosticCollector<JavaFileObject>();
73        task = tool.getTask(null, fm, diags, Collections.singleton("-Xlint:all"),
74                            null, compilationUnits);
75        if (!task.call())
76            throw new AssertionError("Compilation failed");
77        String msg = diags.getDiagnostics().get(0).getMessage(null);
78        long lineno = diags.getDiagnostics().get(0).getLineNumber();
79        if (msg.contains(":"+lineno+":"))
80            // 6469079: Diagnostic.getMessage(Locale) includes line numbers
81            throw new AssertionError(msg);
82    }
83
84    public static void main(String... args) throws Exception {
85        try (Test t = new Test()) {
86            t.test(args);
87        }
88    }
89
90}
91