test/tools/javac/api/TestResolveError.java

Mon, 06 Sep 2010 12:55:09 -0700

author
jjg
date
Mon, 06 Sep 2010 12:55:09 -0700
changeset 672
ea54372637a5
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

6930507: Symbols for anonymous and local classes made too late for use by java tree API
Reviewed-by: mcimadamore

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

mercurial