langtools/test/tools/javac/api/TestResolveError.java
author alanb
Thu, 17 Mar 2016 19:04:28 +0000
changeset 36526 3b41f1c69604
parent 30730 d3ce7619db2c
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: jjg, jlahoda, vromero, mcimadamore, bpatel, ksrini, darcy, anazarov, dfuchs Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, jan.lahoda@oracle.com, vicente.romero@oracle.com, andreas.lundblad@oracle.com, andrey.x.nazarov@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, kumar.x.srinivasan@oracle.com, sundararajan.athijegannathan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5016
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27321
diff changeset
     2
 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
5016
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
     4
 *
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
     8
 *
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    13
 * accompanied this code).
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    14
 *
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5016
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5016
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5016
diff changeset
    21
 * questions.
5016
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    22
 */
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    23
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    24
/**
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    25
 * @test
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    26
 * @bug 6930108
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    27
 * @summary IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.java
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    28
 * @library ./lib
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27321
diff changeset
    29
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27321
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.code
5016
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    31
 * @build ToolTester
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    32
 * @run main TestResolveError
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    33
 */
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    34
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    35
import java.io.*;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    36
import javax.lang.model.element.Element;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    37
import javax.lang.model.element.TypeElement;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    38
import javax.lang.model.type.DeclaredType;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    39
import javax.lang.model.type.TypeMirror;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    40
import javax.lang.model.util.ElementFilter;
5016
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    41
import javax.lang.model.util.Elements;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    42
import javax.lang.model.util.Types;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    43
import javax.tools.*;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    44
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    45
import com.sun.tools.javac.api.JavacTaskImpl;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    46
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    47
/*
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    48
 * This is a cut down version of TestJavacTaskScanner, which as originally written
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    49
 * caused an IllegalArgumentException in AbstractDiagnosticFormatter as a result
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    50
 * of calling task.parseType with a name whose resolution depended on the setting
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    51
 * of the bootclasspath.
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    52
 * This test has the same call, task.parseType("List<String>", clazz), but checks
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    53
 * that the error is handled in a reasonable way by javac.
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    54
 */
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    55
public class TestResolveError extends ToolTester {
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    56
    public static void main(String... args) throws Exception {
27321
c8eceba990bf 8062514: Update ToolTester tests to close file manager
jjg
parents: 26264
diff changeset
    57
        try (TestResolveError t = new TestResolveError()) {
c8eceba990bf 8062514: Update ToolTester tests to close file manager
jjg
parents: 26264
diff changeset
    58
            t.run();
c8eceba990bf 8062514: Update ToolTester tests to close file manager
jjg
parents: 26264
diff changeset
    59
        }
5016
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    60
    }
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    61
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    62
    void run() throws Exception {
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    63
        StringWriter sw = new StringWriter();
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    64
        PrintWriter pw = new PrintWriter(sw);
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    65
        File file = new File(test_src, "TestResolveError.java");
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    66
        final Iterable<? extends JavaFileObject> compilationUnits =
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    67
            fm.getJavaFileObjects(new File[] {file});
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    68
        task = (JavacTaskImpl)tool.getTask(pw, fm, null, null, null, compilationUnits);
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    69
        elements = task.getElements();
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    70
        types = task.getTypes();
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    71
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    72
        Iterable<? extends TypeElement> toplevels;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    73
        toplevels = ElementFilter.typesIn(task.enter(task.parse()));
5016
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    74
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    75
        for (TypeElement clazz : toplevels) {
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    76
            System.out.format("Testing %s:%n%n", clazz.getSimpleName());
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    77
            // this should not cause any exception from the compiler,
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    78
            // such as IllegalArgumentException
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    79
            testParseType(clazz);
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    80
        }
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    81
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    82
        pw.close();
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    83
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    84
        String out = sw.toString();
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    85
        System.out.println(out);
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    86
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    87
        if (out.contains("com.sun.tools.javac.util"))
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    88
            throw new Exception("Unexpected output from compiler");
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    89
    }
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    90
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    91
    void testParseType(TypeElement clazz) {
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    92
        DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz);
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    93
        for (Element member : elements.getAllMembers((TypeElement)type.asElement())) {
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    94
            TypeMirror mt = types.asMemberOf(type, member);
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    95
            System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt);
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    96
        }
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    97
    }
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    98
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    99
    JavacTaskImpl task;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
   100
    Elements elements;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
   101
    Types types;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
   102
}