7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
authorjjh
Tue, 24 Jan 2012 15:51:44 -0800
changeset 11708 f83264d62af2
parent 11707 532f41763bc9
child 11709 0d56d3fc22e6
7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java
langtools/src/share/classes/com/sun/tools/javac/main/Main.java
langtools/test/tools/javah/T7126832/T7126832.java
langtools/test/tools/javah/T7126832/java.java
--- a/langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Tue Jan 24 17:52:02 2012 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Tue Jan 24 15:51:44 2012 -0800
@@ -70,6 +70,7 @@
     private JavaCompiler compiler;
     private Locale locale;
     private String[] args;
+    private String[] classNames;
     private Context context;
     private List<JavaFileObject> fileObjects;
     private Map<JavaFileObject, JCCompilationUnit> notYetEntered;
@@ -82,11 +83,13 @@
 
     JavacTaskImpl(Main compilerMain,
                 String[] args,
+                String[] classNames,
                 Context context,
                 List<JavaFileObject> fileObjects) {
         this.ccw = ClientCodeWrapper.instance(context);
         this.compilerMain = compilerMain;
         this.args = args;
+        this.classNames = classNames;
         this.context = context;
         this.fileObjects = fileObjects;
         setLocale(Locale.getDefault());
@@ -101,17 +104,14 @@
                 Context context,
                 Iterable<String> classes,
                 Iterable<? extends JavaFileObject> fileObjects) {
-        this(compilerMain, toArray(flags, classes), context, toList(fileObjects));
+        this(compilerMain, toArray(flags), toArray(classes), context, toList(fileObjects));
     }
 
-    static private String[] toArray(Iterable<String> flags, Iterable<String> classes) {
+    static private String[] toArray(Iterable<String> iter) {
         ListBuffer<String> result = new ListBuffer<String>();
-        if (flags != null)
-            for (String flag : flags)
-                result.append(flag);
-        if (classes != null)
-            for (String cls : classes)
-                result.append(cls);
+        if (iter != null)
+            for (String s : iter)
+                result.append(s);
         return result.toArray(new String[result.length()]);
     }
 
@@ -129,7 +129,7 @@
             initContext();
             notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>();
             compilerMain.setAPIMode(true);
-            result = compilerMain.compile(args, context, fileObjects, processors);
+            result = compilerMain.compile(args, classNames, context, fileObjects, processors);
             cleanup();
             return result.isOK();
         } else {
@@ -159,7 +159,7 @@
             initContext();
             compilerMain.setOptions(Options.instance(context));
             compilerMain.filenames = new LinkedHashSet<File>();
-            Collection<File> filenames = compilerMain.processArgs(CommandLine.parse(args));
+            Collection<File> filenames = compilerMain.processArgs(CommandLine.parse(args), classNames);
             if (!filenames.isEmpty())
                 throw new IllegalArgumentException("Malformed arguments " + toString(filenames, " "));
             compiler = JavaCompiler.instance(context);
@@ -174,6 +174,7 @@
             // endContext will be called when all classes have been generated
             // TODO: should handle the case after each phase if errors have occurred
             args = null;
+            classNames = null;
         }
     }
 
@@ -204,6 +205,7 @@
         compiler = null;
         compilerMain = null;
         args = null;
+        classNames = null;
         context = null;
         fileObjects = null;
         notYetEntered = null;
--- a/langtools/src/share/classes/com/sun/tools/javac/main/Main.java	Tue Jan 24 17:52:02 2012 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/main/Main.java	Tue Jan 24 15:51:44 2012 -0800
@@ -31,6 +31,7 @@
 import java.net.URL;
 import java.security.DigestInputStream;
 import java.security.MessageDigest;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.LinkedHashSet;
 import java.util.Set;
@@ -208,6 +209,10 @@
      *  @param flags    The array of command line arguments.
      */
     public Collection<File> processArgs(String[] flags) { // XXX sb protected
+        return processArgs(flags, null);
+    }
+
+    public Collection<File> processArgs(String[] flags, String[] classNames) { // XXX sb protected
         int ac = 0;
         while (ac < flags.length) {
             String flag = flags[ac];
@@ -248,6 +253,10 @@
             }
         }
 
+        if (this.classnames != null && classNames != null) {
+            this.classnames.addAll(Arrays.asList(classNames));
+        }
+
         if (!checkDirectory(D))
             return null;
         if (!checkDirectory(S))
@@ -346,6 +355,15 @@
                        List<JavaFileObject> fileObjects,
                        Iterable<? extends Processor> processors)
     {
+        return compile(args,  null, context, fileObjects, processors);
+    }
+
+    public Result compile(String[] args,
+                       String[] classNames,
+                       Context context,
+                       List<JavaFileObject> fileObjects,
+                       Iterable<? extends Processor> processors)
+    {
         context.put(Log.outKey, out);
         log = Log.instance(context);
 
@@ -361,14 +379,16 @@
          * into account.
          */
         try {
-            if (args.length == 0 && fileObjects.isEmpty()) {
+            if (args.length == 0
+                    && (classNames == null || classNames.length == 0)
+                    && fileObjects.isEmpty()) {
                 Option.HELP.process(optionHelper, "-help");
                 return Result.CMDERR;
             }
 
             Collection<File> files;
             try {
-                files = processArgs(CommandLine.parse(args));
+                files = processArgs(CommandLine.parse(args), classNames);
                 if (files == null) {
                     // null signals an error in options, abort
                     return Result.CMDERR;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javah/T7126832/T7126832.java	Tue Jan 24 15:51:44 2012 -0800
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 7126832
+ * @compile java.java
+ * @summary com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
+ * @run main T7126832
+ */
+
+import java.io.*;
+import java.util.*;
+
+public class T7126832 {
+    public static void main(String... args) throws Exception {
+        new T7126832().run();
+    }
+
+    void run() throws Exception {
+        Locale prev = Locale.getDefault();
+        Locale.setDefault(Locale.ENGLISH);
+        try {
+            // Verify that a .java file is correctly diagnosed
+            File ff = writeFile(new File("JavahTest.java"), "class JavahTest {}");
+            test(Arrays.asList(ff.getPath()), 1, "Could not find class file for 'JavahTest.java'.");
+
+            // Verify that a class named 'xx.java' is accepted.
+            // Note that ./xx/java.class exists, so this should work ok
+            test(Arrays.asList("xx.java"), 0, null);
+
+            if (errors > 0) {
+                throw new Exception(errors + " errors occurred");
+            }
+        } finally {
+           Locale.setDefault(prev);
+        }
+    }
+
+    void test(List<String> args, int expectRC, String expectOut) {
+        System.err.println("Test: " + args
+                + " rc:" + expectRC
+                + ((expectOut != null) ? " out:" + expectOut : ""));
+
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        int rc = 0;
+        String out = null;
+        try {
+            rc = com.sun.tools.javah.Main.run(args.toArray(new String[args.size()]), pw);
+            out = sw.toString();
+        } catch(Exception ee) {
+            rc = 1;
+            out = ee.toString();;
+        }
+        pw.close();
+        if (!out.isEmpty()) {
+            System.err.println(out);
+        }
+        if (rc != expectRC) {
+            error("Unexpected exit code: " + rc + "; expected: " + expectRC);
+        }
+        if (expectOut != null && !out.contains(expectOut)) {
+            error("Expected string not found: " + expectOut);
+        }
+
+        System.err.println();
+    }
+
+    File writeFile(File ff, String ss) throws IOException {
+        if (ff.getParentFile() != null)
+            ff.getParentFile().mkdirs();
+
+        try (FileWriter out = new FileWriter(ff)) {
+            out.write(ss);
+        }
+        return ff;
+    }
+
+    void error(String msg) {
+        System.err.println(msg);
+        errors++;
+    }
+
+    int errors;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javah/T7126832/java.java	Tue Jan 24 15:51:44 2012 -0800
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package xx;
+class java {
+    int fred;
+}