langtools/test/tools/javac/api/TestResolveError.java
changeset 5016 ff5e6791d0bb
child 5520 86e4b9a9da40
equal deleted inserted replaced
5014:3651144e76f0 5016:ff5e6791d0bb
       
     1 /*
       
     2  * Copyright 2010 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    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  */
       
    32 
       
    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.*;
       
    41 
       
    42 import com.sun.tools.javac.api.JavacTaskImpl;
       
    43 
       
    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     }
       
    56 
       
    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();
       
    66 
       
    67         Iterable<? extends TypeElement> toplevels;
       
    68         try {
       
    69             toplevels = task.enter(task.parse());
       
    70         } catch (IOException ex) {
       
    71             throw new AssertionError(ex);
       
    72         }
       
    73 
       
    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         }
       
    80 
       
    81         pw.close();
       
    82 
       
    83         String out = sw.toString();
       
    84         System.out.println(out);
       
    85 
       
    86         if (out.contains("com.sun.tools.javac.util"))
       
    87             throw new Exception("Unexpected output from compiler");
       
    88     }
       
    89 
       
    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     }
       
    97 
       
    98     JavacTaskImpl task;
       
    99     Elements elements;
       
   100     Types types;
       
   101 }