test/tools/javac/api/TestResolveError.java

changeset 519
a23282f17d0b
child 554
9d9f26857129
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/api/TestResolveError.java	Fri Mar 05 16:12:33 2010 -0800
     1.3 @@ -0,0 +1,101 @@
     1.4 +/*
     1.5 + * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +/**
    1.28 + * @test
    1.29 + * @bug 6930108
    1.30 + * @summary IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.java
    1.31 + * @library ./lib
    1.32 + * @build ToolTester
    1.33 + * @run main TestResolveError
    1.34 + */
    1.35 +
    1.36 +import java.io.*;
    1.37 +import javax.lang.model.element.Element;
    1.38 +import javax.lang.model.element.TypeElement;
    1.39 +import javax.lang.model.type.DeclaredType;
    1.40 +import javax.lang.model.type.TypeMirror;
    1.41 +import javax.lang.model.util.Elements;
    1.42 +import javax.lang.model.util.Types;
    1.43 +import javax.tools.*;
    1.44 +
    1.45 +import com.sun.tools.javac.api.JavacTaskImpl;
    1.46 +
    1.47 +/*
    1.48 + * This is a cut down version of TestJavacTaskScanner, which as originally written
    1.49 + * caused an IllegalArgumentException in AbstractDiagnosticFormatter as a result
    1.50 + * of calling task.parseType with a name whose resolution depended on the setting
    1.51 + * of the bootclasspath.
    1.52 + * This test has the same call, task.parseType("List<String>", clazz), but checks
    1.53 + * that the error is handled in a reasonable way by javac.
    1.54 + */
    1.55 +public class TestResolveError extends ToolTester {
    1.56 +    public static void main(String... args) throws Exception {
    1.57 +        new TestResolveError().run();
    1.58 +    }
    1.59 +
    1.60 +    void run() throws Exception {
    1.61 +        StringWriter sw = new StringWriter();
    1.62 +        PrintWriter pw = new PrintWriter(sw);
    1.63 +        File file = new File(test_src, "TestResolveError.java");
    1.64 +        final Iterable<? extends JavaFileObject> compilationUnits =
    1.65 +            fm.getJavaFileObjects(new File[] {file});
    1.66 +        task = (JavacTaskImpl)tool.getTask(pw, fm, null, null, null, compilationUnits);
    1.67 +        elements = task.getElements();
    1.68 +        types = task.getTypes();
    1.69 +
    1.70 +        Iterable<? extends TypeElement> toplevels;
    1.71 +        try {
    1.72 +            toplevels = task.enter(task.parse());
    1.73 +        } catch (IOException ex) {
    1.74 +            throw new AssertionError(ex);
    1.75 +        }
    1.76 +
    1.77 +        for (TypeElement clazz : toplevels) {
    1.78 +            System.out.format("Testing %s:%n%n", clazz.getSimpleName());
    1.79 +            // this should not cause any exception from the compiler,
    1.80 +            // such as IllegalArgumentException
    1.81 +            testParseType(clazz);
    1.82 +        }
    1.83 +
    1.84 +        pw.close();
    1.85 +
    1.86 +        String out = sw.toString();
    1.87 +        System.out.println(out);
    1.88 +
    1.89 +        if (out.contains("com.sun.tools.javac.util"))
    1.90 +            throw new Exception("Unexpected output from compiler");
    1.91 +    }
    1.92 +
    1.93 +    void testParseType(TypeElement clazz) {
    1.94 +        DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz);
    1.95 +        for (Element member : elements.getAllMembers((TypeElement)type.asElement())) {
    1.96 +            TypeMirror mt = types.asMemberOf(type, member);
    1.97 +            System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt);
    1.98 +        }
    1.99 +    }
   1.100 +
   1.101 +    JavacTaskImpl task;
   1.102 +    Elements elements;
   1.103 +    Types types;
   1.104 +}

mercurial