TestResolveError.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2010, 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 6930108
27 * @summary IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.java
28 * @library ./lib
29 * @modules jdk.compiler/com.sun.tools.javac.api
30 *          jdk.compiler/com.sun.tools.javac.code
31 * @build ToolTester
32 * @run main TestResolveError
33 */
34
35import java.io.*;
36import javax.lang.model.element.Element;
37import javax.lang.model.element.TypeElement;
38import javax.lang.model.type.DeclaredType;
39import javax.lang.model.type.TypeMirror;
40import javax.lang.model.util.ElementFilter;
41import javax.lang.model.util.Elements;
42import javax.lang.model.util.Types;
43import javax.tools.*;
44
45import com.sun.tools.javac.api.JavacTaskImpl;
46
47/*
48 * This is a cut down version of TestJavacTaskScanner, which as originally written
49 * caused an IllegalArgumentException in AbstractDiagnosticFormatter as a result
50 * of calling task.parseType with a name whose resolution depended on the setting
51 * of the bootclasspath.
52 * This test has the same call, task.parseType("List<String>", clazz), but checks
53 * that the error is handled in a reasonable way by javac.
54 */
55public class TestResolveError extends ToolTester {
56    public static void main(String... args) throws Exception {
57        try (TestResolveError t = new TestResolveError()) {
58            t.run();
59        }
60    }
61
62    void run() throws Exception {
63        StringWriter sw = new StringWriter();
64        PrintWriter pw = new PrintWriter(sw);
65        File file = new File(test_src, "TestResolveError.java");
66        final Iterable<? extends JavaFileObject> compilationUnits =
67            fm.getJavaFileObjects(new File[] {file});
68        task = (JavacTaskImpl)tool.getTask(pw, fm, null, null, null, compilationUnits);
69        elements = task.getElements();
70        types = task.getTypes();
71
72        Iterable<? extends TypeElement> toplevels;
73        toplevels = ElementFilter.typesIn(task.enter(task.parse()));
74
75        for (TypeElement clazz : toplevels) {
76            System.out.format("Testing %s:%n%n", clazz.getSimpleName());
77            // this should not cause any exception from the compiler,
78            // such as IllegalArgumentException
79            testParseType(clazz);
80        }
81
82        pw.close();
83
84        String out = sw.toString();
85        System.out.println(out);
86
87        if (out.contains("com.sun.tools.javac.util"))
88            throw new Exception("Unexpected output from compiler");
89    }
90
91    void testParseType(TypeElement clazz) {
92        DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz);
93        for (Element member : elements.getAllMembers((TypeElement)type.asElement())) {
94            TypeMirror mt = types.asMemberOf(type, member);
95            System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt);
96        }
97    }
98
99    JavacTaskImpl task;
100    Elements elements;
101    Types types;
102}
103