Fri, 05 Mar 2010 16:12:33 -0800
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
Reviewed-by: darcy
1.1 --- a/src/share/classes/com/sun/tools/javac/util/BasicDiagnosticFormatter.java Wed Mar 03 19:34:34 2010 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/util/BasicDiagnosticFormatter.java Fri Mar 05 16:12:33 2010 -0800 1.3 @@ -201,7 +201,7 @@ 1.4 private String selectFormat(JCDiagnostic d) { 1.5 DiagnosticSource source = d.getDiagnosticSource(); 1.6 String format = getConfiguration().getFormat(BasicFormatKind.DEFAULT_NO_POS_FORMAT); 1.7 - if (source != null) { 1.8 + if (source != null && source != DiagnosticSource.NO_SOURCE) { 1.9 if (d.getIntPosition() != Position.NOPOS) { 1.10 format = getConfiguration().getFormat(BasicFormatKind.DEFAULT_POS_FORMAT); 1.11 } else if (source.getFile() != null &&
2.1 --- a/test/tools/javac/api/TestJavacTaskScanner.java Wed Mar 03 19:34:34 2010 -0800 2.2 +++ b/test/tools/javac/api/TestJavacTaskScanner.java Fri Mar 05 16:12:33 2010 -0800 2.3 @@ -34,7 +34,10 @@ 2.4 import com.sun.tools.javac.parser.*; // XXX 2.5 import com.sun.tools.javac.util.*; // XXX 2.6 import java.io.*; 2.7 +import java.net.*; 2.8 import java.nio.*; 2.9 +import java.nio.charset.Charset; 2.10 +import java.util.Arrays; 2.11 import javax.lang.model.element.Element; 2.12 import javax.lang.model.element.TypeElement; 2.13 import javax.lang.model.type.DeclaredType; 2.14 @@ -43,6 +46,10 @@ 2.15 import javax.lang.model.util.Types; 2.16 import javax.tools.*; 2.17 2.18 +import static javax.tools.StandardLocation.CLASS_PATH; 2.19 +import static javax.tools.StandardLocation.SOURCE_PATH; 2.20 +import static javax.tools.StandardLocation.CLASS_OUTPUT; 2.21 + 2.22 public class TestJavacTaskScanner extends ToolTester { 2.23 2.24 final JavacTaskImpl task; 2.25 @@ -56,6 +63,7 @@ 2.26 TestJavacTaskScanner(File file) { 2.27 final Iterable<? extends JavaFileObject> compilationUnits = 2.28 fm.getJavaFileObjects(new File[] {file}); 2.29 + StandardJavaFileManager fm = getLocalFileManager(tool, null, null); 2.30 task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, compilationUnits); 2.31 task.getContext().put(Scanner.Factory.scannerFactoryKey, 2.32 new MyScanner.Factory(task.getContext(), this)); 2.33 @@ -83,7 +91,7 @@ 2.34 System.out.println("#parseTypeElements: " + numParseTypeElements); 2.35 System.out.println("#allMembers: " + numAllMembers); 2.36 2.37 - check(numTokens, "#Tokens", 891); 2.38 + check(numTokens, "#Tokens", 1222); 2.39 check(numParseTypeElements, "#parseTypeElements", 136); 2.40 check(numAllMembers, "#allMembers", 67); 2.41 } 2.42 @@ -117,6 +125,47 @@ 2.43 numAllMembers++; 2.44 } 2.45 } 2.46 + 2.47 + /* Similar to ToolTester.getFileManager, except that this version also ensures 2.48 + * javac classes will be available on the classpath. The javac classes are assumed 2.49 + * to be on the classpath used to run this test (this is true when using jtreg). 2.50 + * The classes are found by obtaining the URL for a sample javac class, using 2.51 + * getClassLoader().getResource(), and then deconstructing the URL to find the 2.52 + * underlying directory or jar file to place on the classpath. 2.53 + */ 2.54 + public StandardJavaFileManager getLocalFileManager(JavaCompiler tool, 2.55 + DiagnosticListener<JavaFileObject> dl, 2.56 + Charset encoding) { 2.57 + File javac_classes; 2.58 + try { 2.59 + final String javacMainClass = "com/sun/tools/javac/Main.class"; 2.60 + URL url = getClass().getClassLoader().getResource(javacMainClass); 2.61 + if (url == null) 2.62 + throw new Error("can't locate javac classes"); 2.63 + URI uri = url.toURI(); 2.64 + String scheme = uri.getScheme(); 2.65 + String ssp = uri.getSchemeSpecificPart(); 2.66 + if (scheme.equals("jar")) { 2.67 + javac_classes = new File(new URI(ssp.substring(0, ssp.indexOf("!/")))); 2.68 + } else if (scheme.equals("file")) { 2.69 + javac_classes = new File(ssp.substring(0, ssp.indexOf(javacMainClass))); 2.70 + } else 2.71 + throw new Error("unknown URL: " + url); 2.72 + } catch (URISyntaxException e) { 2.73 + throw new Error(e); 2.74 + } 2.75 + System.err.println("javac_classes: " + javac_classes); 2.76 + 2.77 + StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, encoding); 2.78 + try { 2.79 + fm.setLocation(SOURCE_PATH, Arrays.asList(test_src)); 2.80 + fm.setLocation(CLASS_PATH, Arrays.asList(test_classes, javac_classes)); 2.81 + fm.setLocation(CLASS_OUTPUT, Arrays.asList(test_classes)); 2.82 + } catch (IOException e) { 2.83 + throw new AssertionError(e); 2.84 + } 2.85 + return fm; 2.86 + } 2.87 } 2.88 2.89 class MyScanner extends Scanner {
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/api/TestResolveError.java Fri Mar 05 16:12:33 2010 -0800 3.3 @@ -0,0 +1,101 @@ 3.4 +/* 3.5 + * Copyright 2010 Sun Microsystems, Inc. All Rights Reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 3.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 3.24 + * have any questions. 3.25 + */ 3.26 + 3.27 +/** 3.28 + * @test 3.29 + * @bug 6930108 3.30 + * @summary IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.java 3.31 + * @library ./lib 3.32 + * @build ToolTester 3.33 + * @run main TestResolveError 3.34 + */ 3.35 + 3.36 +import java.io.*; 3.37 +import javax.lang.model.element.Element; 3.38 +import javax.lang.model.element.TypeElement; 3.39 +import javax.lang.model.type.DeclaredType; 3.40 +import javax.lang.model.type.TypeMirror; 3.41 +import javax.lang.model.util.Elements; 3.42 +import javax.lang.model.util.Types; 3.43 +import javax.tools.*; 3.44 + 3.45 +import com.sun.tools.javac.api.JavacTaskImpl; 3.46 + 3.47 +/* 3.48 + * This is a cut down version of TestJavacTaskScanner, which as originally written 3.49 + * caused an IllegalArgumentException in AbstractDiagnosticFormatter as a result 3.50 + * of calling task.parseType with a name whose resolution depended on the setting 3.51 + * of the bootclasspath. 3.52 + * This test has the same call, task.parseType("List<String>", clazz), but checks 3.53 + * that the error is handled in a reasonable way by javac. 3.54 + */ 3.55 +public class TestResolveError extends ToolTester { 3.56 + public static void main(String... args) throws Exception { 3.57 + new TestResolveError().run(); 3.58 + } 3.59 + 3.60 + void run() throws Exception { 3.61 + StringWriter sw = new StringWriter(); 3.62 + PrintWriter pw = new PrintWriter(sw); 3.63 + File file = new File(test_src, "TestResolveError.java"); 3.64 + final Iterable<? extends JavaFileObject> compilationUnits = 3.65 + fm.getJavaFileObjects(new File[] {file}); 3.66 + task = (JavacTaskImpl)tool.getTask(pw, fm, null, null, null, compilationUnits); 3.67 + elements = task.getElements(); 3.68 + types = task.getTypes(); 3.69 + 3.70 + Iterable<? extends TypeElement> toplevels; 3.71 + try { 3.72 + toplevels = task.enter(task.parse()); 3.73 + } catch (IOException ex) { 3.74 + throw new AssertionError(ex); 3.75 + } 3.76 + 3.77 + for (TypeElement clazz : toplevels) { 3.78 + System.out.format("Testing %s:%n%n", clazz.getSimpleName()); 3.79 + // this should not cause any exception from the compiler, 3.80 + // such as IllegalArgumentException 3.81 + testParseType(clazz); 3.82 + } 3.83 + 3.84 + pw.close(); 3.85 + 3.86 + String out = sw.toString(); 3.87 + System.out.println(out); 3.88 + 3.89 + if (out.contains("com.sun.tools.javac.util")) 3.90 + throw new Exception("Unexpected output from compiler"); 3.91 + } 3.92 + 3.93 + void testParseType(TypeElement clazz) { 3.94 + DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz); 3.95 + for (Element member : elements.getAllMembers((TypeElement)type.asElement())) { 3.96 + TypeMirror mt = types.asMemberOf(type, member); 3.97 + System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt); 3.98 + } 3.99 + } 3.100 + 3.101 + JavacTaskImpl task; 3.102 + Elements elements; 3.103 + Types types; 3.104 +}