# HG changeset patch # User jjh # Date 1327541636 28800 # Node ID 7ca6d8cb4cc7628cbe9b463c7358873b9c525b90 # Parent 83d5084beaa109a3cc9ade57659c7af66a681280 7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast Reviewed-by: jjg diff -r 83d5084beaa1 -r 7ca6d8cb4cc7 src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java --- a/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java Wed Jan 25 17:26:35 2012 -0800 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java Wed Jan 25 17:33:56 2012 -0800 @@ -70,6 +70,7 @@ private JavaCompiler compiler; private Locale locale; private String[] args; + private String[] classNames; private Context context; private List fileObjects; private Map notYetEntered; @@ -82,11 +83,13 @@ JavacTaskImpl(Main compilerMain, String[] args, + String[] classNames, Context context, List 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 classes, Iterable fileObjects) { - this(compilerMain, toArray(flags, classes), context, toList(fileObjects)); + this(compilerMain, toArray(flags), toArray(classes), context, toList(fileObjects)); } - static private String[] toArray(Iterable flags, Iterable classes) { + static private String[] toArray(Iterable iter) { ListBuffer result = new ListBuffer(); - 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(); compilerMain.setAPIMode(true); - result = compilerMain.compile(args, context, fileObjects, processors); + result = compilerMain.compile(args, classNames, context, fileObjects, processors); cleanup(); return result == 0; } else { @@ -159,7 +159,7 @@ initContext(); compilerMain.setOptions(Options.instance(context)); compilerMain.filenames = new LinkedHashSet(); - Collection filenames = compilerMain.processArgs(CommandLine.parse(args)); + Collection 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; diff -r 83d5084beaa1 -r 7ca6d8cb4cc7 src/share/classes/com/sun/tools/javac/main/Main.java --- a/src/share/classes/com/sun/tools/javac/main/Main.java Wed Jan 25 17:26:35 2012 -0800 +++ b/src/share/classes/com/sun/tools/javac/main/Main.java Wed Jan 25 17:33:56 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.MissingResourceException; @@ -205,6 +206,10 @@ * @param flags The array of command line arguments. */ public Collection processArgs(String[] flags) { // XXX sb protected + return processArgs(flags, null); + } + + public Collection processArgs(String[] flags, String[] classNames) { // XXX sb protected int ac = 0; while (ac < flags.length) { String flag = flags[ac]; @@ -245,6 +250,10 @@ } } + if (this.classnames != null && classNames != null) { + this.classnames.addAll(Arrays.asList(classNames)); + } + if (!checkDirectory(D)) return null; if (!checkDirectory(S)) @@ -341,6 +350,15 @@ List fileObjects, Iterable processors) { + return compile(args, null, context, fileObjects, processors); + } + + public int compile(String[] args, + String[] classNames, + Context context, + List fileObjects, + Iterable processors) + { if (options == null) options = Options.instance(context); // creates a new one @@ -353,14 +371,16 @@ * into account. */ try { - if (args.length == 0 && fileObjects.isEmpty()) { + if (args.length == 0 + && (classNames == null || classNames.length == 0) + && fileObjects.isEmpty()) { help(); return EXIT_CMDERR; } Collection files; try { - files = processArgs(CommandLine.parse(args)); + files = processArgs(CommandLine.parse(args), classNames); if (files == null) { // null signals an error in options, abort return EXIT_CMDERR; diff -r 83d5084beaa1 -r 7ca6d8cb4cc7 test/tools/javah/T7126832/T7126832.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javah/T7126832/T7126832.java Wed Jan 25 17:33:56 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 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; +} + diff -r 83d5084beaa1 -r 7ca6d8cb4cc7 test/tools/javah/T7126832/java.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javah/T7126832/java.java Wed Jan 25 17:33:56 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; +}