6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav
authorjjg
Fri, 05 Mar 2010 16:12:33 -0800
changeset 5016 ff5e6791d0bb
parent 5014 3651144e76f0
child 5017 8394425cd279
6930108: IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.jav Reviewed-by: darcy
langtools/src/share/classes/com/sun/tools/javac/util/BasicDiagnosticFormatter.java
langtools/test/tools/javac/api/TestJavacTaskScanner.java
langtools/test/tools/javac/api/TestResolveError.java
--- a/langtools/src/share/classes/com/sun/tools/javac/util/BasicDiagnosticFormatter.java	Wed Mar 03 19:34:34 2010 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/util/BasicDiagnosticFormatter.java	Fri Mar 05 16:12:33 2010 -0800
@@ -201,7 +201,7 @@
     private String selectFormat(JCDiagnostic d) {
         DiagnosticSource source = d.getDiagnosticSource();
         String format = getConfiguration().getFormat(BasicFormatKind.DEFAULT_NO_POS_FORMAT);
-        if (source != null) {
+        if (source != null && source != DiagnosticSource.NO_SOURCE) {
             if (d.getIntPosition() != Position.NOPOS) {
                 format = getConfiguration().getFormat(BasicFormatKind.DEFAULT_POS_FORMAT);
             } else if (source.getFile() != null &&
--- a/langtools/test/tools/javac/api/TestJavacTaskScanner.java	Wed Mar 03 19:34:34 2010 -0800
+++ b/langtools/test/tools/javac/api/TestJavacTaskScanner.java	Fri Mar 05 16:12:33 2010 -0800
@@ -34,7 +34,10 @@
 import com.sun.tools.javac.parser.*; // XXX
 import com.sun.tools.javac.util.*; // XXX
 import java.io.*;
+import java.net.*;
 import java.nio.*;
+import java.nio.charset.Charset;
+import java.util.Arrays;
 import javax.lang.model.element.Element;
 import javax.lang.model.element.TypeElement;
 import javax.lang.model.type.DeclaredType;
@@ -43,6 +46,10 @@
 import javax.lang.model.util.Types;
 import javax.tools.*;
 
+import static javax.tools.StandardLocation.CLASS_PATH;
+import static javax.tools.StandardLocation.SOURCE_PATH;
+import static javax.tools.StandardLocation.CLASS_OUTPUT;
+
 public class TestJavacTaskScanner extends ToolTester {
 
     final JavacTaskImpl task;
@@ -56,6 +63,7 @@
     TestJavacTaskScanner(File file) {
         final Iterable<? extends JavaFileObject> compilationUnits =
             fm.getJavaFileObjects(new File[] {file});
+        StandardJavaFileManager fm = getLocalFileManager(tool, null, null);
         task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, compilationUnits);
         task.getContext().put(Scanner.Factory.scannerFactoryKey,
                 new MyScanner.Factory(task.getContext(), this));
@@ -83,7 +91,7 @@
         System.out.println("#parseTypeElements: " + numParseTypeElements);
         System.out.println("#allMembers: " + numAllMembers);
 
-        check(numTokens, "#Tokens", 891);
+        check(numTokens, "#Tokens", 1222);
         check(numParseTypeElements, "#parseTypeElements", 136);
         check(numAllMembers, "#allMembers", 67);
     }
@@ -117,6 +125,47 @@
             numAllMembers++;
         }
     }
+
+    /* Similar to ToolTester.getFileManager, except that this version also ensures
+     * javac classes will be available on the classpath.  The javac classes are assumed
+     * to be on the classpath used to run this test (this is true when using jtreg).
+     * The classes are found by obtaining the URL for a sample javac class, using
+     * getClassLoader().getResource(), and then deconstructing the URL to find the
+     * underlying directory or jar file to place on the classpath.
+     */
+    public StandardJavaFileManager getLocalFileManager(JavaCompiler tool,
+                                                        DiagnosticListener<JavaFileObject> dl,
+                                                        Charset encoding) {
+        File javac_classes;
+        try {
+            final String javacMainClass = "com/sun/tools/javac/Main.class";
+            URL url = getClass().getClassLoader().getResource(javacMainClass);
+            if (url == null)
+                throw new Error("can't locate javac classes");
+            URI uri = url.toURI();
+            String scheme = uri.getScheme();
+            String ssp = uri.getSchemeSpecificPart();
+            if (scheme.equals("jar")) {
+                javac_classes = new File(new URI(ssp.substring(0, ssp.indexOf("!/"))));
+            } else if (scheme.equals("file")) {
+                javac_classes = new File(ssp.substring(0, ssp.indexOf(javacMainClass)));
+            } else
+                throw new Error("unknown URL: " + url);
+        } catch (URISyntaxException e) {
+            throw new Error(e);
+        }
+        System.err.println("javac_classes: " + javac_classes);
+
+        StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, encoding);
+        try {
+            fm.setLocation(SOURCE_PATH,  Arrays.asList(test_src));
+            fm.setLocation(CLASS_PATH,   Arrays.asList(test_classes, javac_classes));
+            fm.setLocation(CLASS_OUTPUT, Arrays.asList(test_classes));
+        } catch (IOException e) {
+            throw new AssertionError(e);
+        }
+        return fm;
+    }
 }
 
 class MyScanner extends Scanner {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/api/TestResolveError.java	Fri Mar 05 16:12:33 2010 -0800
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/**
+ * @test
+ * @bug 6930108
+ * @summary IllegalArgumentException in AbstractDiagnosticFormatter for tools/javac/api/TestJavacTaskScanner.java
+ * @library ./lib
+ * @build ToolTester
+ * @run main TestResolveError
+ */
+
+import java.io.*;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.type.DeclaredType;
+import javax.lang.model.type.TypeMirror;
+import javax.lang.model.util.Elements;
+import javax.lang.model.util.Types;
+import javax.tools.*;
+
+import com.sun.tools.javac.api.JavacTaskImpl;
+
+/*
+ * This is a cut down version of TestJavacTaskScanner, which as originally written
+ * caused an IllegalArgumentException in AbstractDiagnosticFormatter as a result
+ * of calling task.parseType with a name whose resolution depended on the setting
+ * of the bootclasspath.
+ * This test has the same call, task.parseType("List<String>", clazz), but checks
+ * that the error is handled in a reasonable way by javac.
+ */
+public class TestResolveError extends ToolTester {
+    public static void main(String... args) throws Exception {
+        new TestResolveError().run();
+    }
+
+    void run() throws Exception {
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        File file = new File(test_src, "TestResolveError.java");
+        final Iterable<? extends JavaFileObject> compilationUnits =
+            fm.getJavaFileObjects(new File[] {file});
+        task = (JavacTaskImpl)tool.getTask(pw, fm, null, null, null, compilationUnits);
+        elements = task.getElements();
+        types = task.getTypes();
+
+        Iterable<? extends TypeElement> toplevels;
+        try {
+            toplevels = task.enter(task.parse());
+        } catch (IOException ex) {
+            throw new AssertionError(ex);
+        }
+
+        for (TypeElement clazz : toplevels) {
+            System.out.format("Testing %s:%n%n", clazz.getSimpleName());
+            // this should not cause any exception from the compiler,
+            // such as IllegalArgumentException
+            testParseType(clazz);
+        }
+
+        pw.close();
+
+        String out = sw.toString();
+        System.out.println(out);
+
+        if (out.contains("com.sun.tools.javac.util"))
+            throw new Exception("Unexpected output from compiler");
+    }
+
+    void testParseType(TypeElement clazz) {
+        DeclaredType type = (DeclaredType)task.parseType("List<String>", clazz);
+        for (Element member : elements.getAllMembers((TypeElement)type.asElement())) {
+            TypeMirror mt = types.asMemberOf(type, member);
+            System.out.format("%s : %s -> %s%n", member.getSimpleName(), member.asType(), mt);
+        }
+    }
+
+    JavacTaskImpl task;
+    Elements elements;
+    Types types;
+}