langtools/test/tools/javac/api/TestResolveError.java
author ohair
Tue, 25 May 2010 15:54:51 -0700
changeset 5520 86e4b9a9da40
parent 5016 ff5e6791d0bb
child 26264 a09fedde76be
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
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
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5016
diff changeset
     2
 * Copyright (c) 2010, 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
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    29
 * @build ToolTester
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    30
 * @run main TestResolveError
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    31
 */
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    32
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    33
import java.io.*;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    34
import javax.lang.model.element.Element;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    35
import javax.lang.model.element.TypeElement;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    36
import javax.lang.model.type.DeclaredType;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    37
import javax.lang.model.type.TypeMirror;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    38
import javax.lang.model.util.Elements;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    39
import javax.lang.model.util.Types;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    40
import javax.tools.*;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    41
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    42
import com.sun.tools.javac.api.JavacTaskImpl;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    43
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
 * 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
    46
 * caused an IllegalArgumentException in AbstractDiagnosticFormatter as a result
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    47
 * 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
    48
 * of the bootclasspath.
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    49
 * 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
    50
 * 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
    51
 */
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    52
public class TestResolveError extends ToolTester {
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    53
    public static void main(String... args) throws Exception {
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    54
        new TestResolveError().run();
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    55
    }
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    56
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    57
    void run() throws Exception {
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    58
        StringWriter sw = new StringWriter();
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    59
        PrintWriter pw = new PrintWriter(sw);
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    60
        File file = new File(test_src, "TestResolveError.java");
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    61
        final Iterable<? extends JavaFileObject> compilationUnits =
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    62
            fm.getJavaFileObjects(new File[] {file});
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    63
        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
    64
        elements = task.getElements();
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    65
        types = task.getTypes();
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    66
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    67
        Iterable<? extends TypeElement> toplevels;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    68
        try {
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    69
            toplevels = task.enter(task.parse());
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    70
        } catch (IOException ex) {
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    71
            throw new AssertionError(ex);
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    72
        }
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    73
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    74
        for (TypeElement clazz : toplevels) {
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    75
            System.out.format("Testing %s:%n%n", clazz.getSimpleName());
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    76
            // this should not cause any exception from the compiler,
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    77
            // such as IllegalArgumentException
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    78
            testParseType(clazz);
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    79
        }
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
        pw.close();
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    82
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    83
        String out = sw.toString();
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    84
        System.out.println(out);
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    85
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    86
        if (out.contains("com.sun.tools.javac.util"))
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    87
            throw new Exception("Unexpected output from compiler");
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    88
    }
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
    void testParseType(TypeElement clazz) {
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    91
        DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz);
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    92
        for (Element member : elements.getAllMembers((TypeElement)type.asElement())) {
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    93
            TypeMirror mt = types.asMemberOf(type, member);
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    94
            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
    95
        }
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
    JavacTaskImpl task;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
    99
    Elements elements;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
   100
    Types types;
ff5e6791d0bb 6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
jjg
parents:
diff changeset
   101
}