TestResolveError.java revision 2689:f839b50088bc
1/* 2 * Copyright (c) 2010, 2014, 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 */ 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 33import java.io.*; 34import javax.lang.model.element.Element; 35import javax.lang.model.element.TypeElement; 36import javax.lang.model.type.DeclaredType; 37import javax.lang.model.type.TypeMirror; 38import javax.lang.model.util.Elements; 39import javax.lang.model.util.Types; 40import javax.tools.*; 41 42import 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 */ 52public class TestResolveError extends ToolTester { 53 public static void main(String... args) throws Exception { 54 try (TestResolveError t = new TestResolveError()) { 55 t.run(); 56 } 57 } 58 59 void run() throws Exception { 60 StringWriter sw = new StringWriter(); 61 PrintWriter pw = new PrintWriter(sw); 62 File file = new File(test_src, "TestResolveError.java"); 63 final Iterable<? extends JavaFileObject> compilationUnits = 64 fm.getJavaFileObjects(new File[] {file}); 65 task = (JavacTaskImpl)tool.getTask(pw, fm, null, null, null, compilationUnits); 66 elements = task.getElements(); 67 types = task.getTypes(); 68 69 Iterable<? extends TypeElement> toplevels; 70 toplevels = task.enter(task.parse()); 71 72 for (TypeElement clazz : toplevels) { 73 System.out.format("Testing %s:%n%n", clazz.getSimpleName()); 74 // this should not cause any exception from the compiler, 75 // such as IllegalArgumentException 76 testParseType(clazz); 77 } 78 79 pw.close(); 80 81 String out = sw.toString(); 82 System.out.println(out); 83 84 if (out.contains("com.sun.tools.javac.util")) 85 throw new Exception("Unexpected output from compiler"); 86 } 87 88 void testParseType(TypeElement clazz) { 89 DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz); 90 for (Element member : elements.getAllMembers((TypeElement)type.asElement())) { 91 TypeMirror mt = types.asMemberOf(type, member); 92 System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt); 93 } 94 } 95 96 JavacTaskImpl task; 97 Elements elements; 98 Types types; 99} 100