7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast

Tue, 24 Jan 2012 15:51:44 -0800

author
jjh
date
Tue, 24 Jan 2012 15:51:44 -0800
changeset 1187
ac36176b7de0
parent 1186
51fb17abfc32
child 1188
d16b464e742c

7126832: com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/main/Main.java file | annotate | diff | comparison | revisions
test/tools/javah/T7126832/T7126832.java file | annotate | diff | comparison | revisions
test/tools/javah/T7126832/java.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Tue Jan 24 17:52:02 2012 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Tue Jan 24 15:51:44 2012 -0800
     1.3 @@ -70,6 +70,7 @@
     1.4      private JavaCompiler compiler;
     1.5      private Locale locale;
     1.6      private String[] args;
     1.7 +    private String[] classNames;
     1.8      private Context context;
     1.9      private List<JavaFileObject> fileObjects;
    1.10      private Map<JavaFileObject, JCCompilationUnit> notYetEntered;
    1.11 @@ -82,11 +83,13 @@
    1.12  
    1.13      JavacTaskImpl(Main compilerMain,
    1.14                  String[] args,
    1.15 +                String[] classNames,
    1.16                  Context context,
    1.17                  List<JavaFileObject> fileObjects) {
    1.18          this.ccw = ClientCodeWrapper.instance(context);
    1.19          this.compilerMain = compilerMain;
    1.20          this.args = args;
    1.21 +        this.classNames = classNames;
    1.22          this.context = context;
    1.23          this.fileObjects = fileObjects;
    1.24          setLocale(Locale.getDefault());
    1.25 @@ -101,17 +104,14 @@
    1.26                  Context context,
    1.27                  Iterable<String> classes,
    1.28                  Iterable<? extends JavaFileObject> fileObjects) {
    1.29 -        this(compilerMain, toArray(flags, classes), context, toList(fileObjects));
    1.30 +        this(compilerMain, toArray(flags), toArray(classes), context, toList(fileObjects));
    1.31      }
    1.32  
    1.33 -    static private String[] toArray(Iterable<String> flags, Iterable<String> classes) {
    1.34 +    static private String[] toArray(Iterable<String> iter) {
    1.35          ListBuffer<String> result = new ListBuffer<String>();
    1.36 -        if (flags != null)
    1.37 -            for (String flag : flags)
    1.38 -                result.append(flag);
    1.39 -        if (classes != null)
    1.40 -            for (String cls : classes)
    1.41 -                result.append(cls);
    1.42 +        if (iter != null)
    1.43 +            for (String s : iter)
    1.44 +                result.append(s);
    1.45          return result.toArray(new String[result.length()]);
    1.46      }
    1.47  
    1.48 @@ -129,7 +129,7 @@
    1.49              initContext();
    1.50              notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>();
    1.51              compilerMain.setAPIMode(true);
    1.52 -            result = compilerMain.compile(args, context, fileObjects, processors);
    1.53 +            result = compilerMain.compile(args, classNames, context, fileObjects, processors);
    1.54              cleanup();
    1.55              return result.isOK();
    1.56          } else {
    1.57 @@ -159,7 +159,7 @@
    1.58              initContext();
    1.59              compilerMain.setOptions(Options.instance(context));
    1.60              compilerMain.filenames = new LinkedHashSet<File>();
    1.61 -            Collection<File> filenames = compilerMain.processArgs(CommandLine.parse(args));
    1.62 +            Collection<File> filenames = compilerMain.processArgs(CommandLine.parse(args), classNames);
    1.63              if (!filenames.isEmpty())
    1.64                  throw new IllegalArgumentException("Malformed arguments " + toString(filenames, " "));
    1.65              compiler = JavaCompiler.instance(context);
    1.66 @@ -174,6 +174,7 @@
    1.67              // endContext will be called when all classes have been generated
    1.68              // TODO: should handle the case after each phase if errors have occurred
    1.69              args = null;
    1.70 +            classNames = null;
    1.71          }
    1.72      }
    1.73  
    1.74 @@ -204,6 +205,7 @@
    1.75          compiler = null;
    1.76          compilerMain = null;
    1.77          args = null;
    1.78 +        classNames = null;
    1.79          context = null;
    1.80          fileObjects = null;
    1.81          notYetEntered = null;
     2.1 --- a/src/share/classes/com/sun/tools/javac/main/Main.java	Tue Jan 24 17:52:02 2012 +0000
     2.2 +++ b/src/share/classes/com/sun/tools/javac/main/Main.java	Tue Jan 24 15:51:44 2012 -0800
     2.3 @@ -31,6 +31,7 @@
     2.4  import java.net.URL;
     2.5  import java.security.DigestInputStream;
     2.6  import java.security.MessageDigest;
     2.7 +import java.util.Arrays;
     2.8  import java.util.Collection;
     2.9  import java.util.LinkedHashSet;
    2.10  import java.util.Set;
    2.11 @@ -208,6 +209,10 @@
    2.12       *  @param flags    The array of command line arguments.
    2.13       */
    2.14      public Collection<File> processArgs(String[] flags) { // XXX sb protected
    2.15 +        return processArgs(flags, null);
    2.16 +    }
    2.17 +
    2.18 +    public Collection<File> processArgs(String[] flags, String[] classNames) { // XXX sb protected
    2.19          int ac = 0;
    2.20          while (ac < flags.length) {
    2.21              String flag = flags[ac];
    2.22 @@ -248,6 +253,10 @@
    2.23              }
    2.24          }
    2.25  
    2.26 +        if (this.classnames != null && classNames != null) {
    2.27 +            this.classnames.addAll(Arrays.asList(classNames));
    2.28 +        }
    2.29 +
    2.30          if (!checkDirectory(D))
    2.31              return null;
    2.32          if (!checkDirectory(S))
    2.33 @@ -346,6 +355,15 @@
    2.34                         List<JavaFileObject> fileObjects,
    2.35                         Iterable<? extends Processor> processors)
    2.36      {
    2.37 +        return compile(args,  null, context, fileObjects, processors);
    2.38 +    }
    2.39 +
    2.40 +    public Result compile(String[] args,
    2.41 +                       String[] classNames,
    2.42 +                       Context context,
    2.43 +                       List<JavaFileObject> fileObjects,
    2.44 +                       Iterable<? extends Processor> processors)
    2.45 +    {
    2.46          context.put(Log.outKey, out);
    2.47          log = Log.instance(context);
    2.48  
    2.49 @@ -361,14 +379,16 @@
    2.50           * into account.
    2.51           */
    2.52          try {
    2.53 -            if (args.length == 0 && fileObjects.isEmpty()) {
    2.54 +            if (args.length == 0
    2.55 +                    && (classNames == null || classNames.length == 0)
    2.56 +                    && fileObjects.isEmpty()) {
    2.57                  Option.HELP.process(optionHelper, "-help");
    2.58                  return Result.CMDERR;
    2.59              }
    2.60  
    2.61              Collection<File> files;
    2.62              try {
    2.63 -                files = processArgs(CommandLine.parse(args));
    2.64 +                files = processArgs(CommandLine.parse(args), classNames);
    2.65                  if (files == null) {
    2.66                      // null signals an error in options, abort
    2.67                      return Result.CMDERR;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javah/T7126832/T7126832.java	Tue Jan 24 15:51:44 2012 -0800
     3.3 @@ -0,0 +1,107 @@
     3.4 +/*
     3.5 + * Copyright (c) 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/*
    3.28 + * @test
    3.29 + * @bug 7126832
    3.30 + * @compile java.java
    3.31 + * @summary com.sun.tools.javac.api.ClientCodeWrapper$WrappedJavaFileManager cannot be cast
    3.32 + * @run main T7126832
    3.33 + */
    3.34 +
    3.35 +import java.io.*;
    3.36 +import java.util.*;
    3.37 +
    3.38 +public class T7126832 {
    3.39 +    public static void main(String... args) throws Exception {
    3.40 +        new T7126832().run();
    3.41 +    }
    3.42 +
    3.43 +    void run() throws Exception {
    3.44 +        Locale prev = Locale.getDefault();
    3.45 +        Locale.setDefault(Locale.ENGLISH);
    3.46 +        try {
    3.47 +            // Verify that a .java file is correctly diagnosed
    3.48 +            File ff = writeFile(new File("JavahTest.java"), "class JavahTest {}");
    3.49 +            test(Arrays.asList(ff.getPath()), 1, "Could not find class file for 'JavahTest.java'.");
    3.50 +
    3.51 +            // Verify that a class named 'xx.java' is accepted.
    3.52 +            // Note that ./xx/java.class exists, so this should work ok
    3.53 +            test(Arrays.asList("xx.java"), 0, null);
    3.54 +
    3.55 +            if (errors > 0) {
    3.56 +                throw new Exception(errors + " errors occurred");
    3.57 +            }
    3.58 +        } finally {
    3.59 +           Locale.setDefault(prev);
    3.60 +        }
    3.61 +    }
    3.62 +
    3.63 +    void test(List<String> args, int expectRC, String expectOut) {
    3.64 +        System.err.println("Test: " + args
    3.65 +                + " rc:" + expectRC
    3.66 +                + ((expectOut != null) ? " out:" + expectOut : ""));
    3.67 +
    3.68 +        StringWriter sw = new StringWriter();
    3.69 +        PrintWriter pw = new PrintWriter(sw);
    3.70 +        int rc = 0;
    3.71 +        String out = null;
    3.72 +        try {
    3.73 +            rc = com.sun.tools.javah.Main.run(args.toArray(new String[args.size()]), pw);
    3.74 +            out = sw.toString();
    3.75 +        } catch(Exception ee) {
    3.76 +            rc = 1;
    3.77 +            out = ee.toString();;
    3.78 +        }
    3.79 +        pw.close();
    3.80 +        if (!out.isEmpty()) {
    3.81 +            System.err.println(out);
    3.82 +        }
    3.83 +        if (rc != expectRC) {
    3.84 +            error("Unexpected exit code: " + rc + "; expected: " + expectRC);
    3.85 +        }
    3.86 +        if (expectOut != null && !out.contains(expectOut)) {
    3.87 +            error("Expected string not found: " + expectOut);
    3.88 +        }
    3.89 +
    3.90 +        System.err.println();
    3.91 +    }
    3.92 +
    3.93 +    File writeFile(File ff, String ss) throws IOException {
    3.94 +        if (ff.getParentFile() != null)
    3.95 +            ff.getParentFile().mkdirs();
    3.96 +
    3.97 +        try (FileWriter out = new FileWriter(ff)) {
    3.98 +            out.write(ss);
    3.99 +        }
   3.100 +        return ff;
   3.101 +    }
   3.102 +
   3.103 +    void error(String msg) {
   3.104 +        System.err.println(msg);
   3.105 +        errors++;
   3.106 +    }
   3.107 +
   3.108 +    int errors;
   3.109 +}
   3.110 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javah/T7126832/java.java	Tue Jan 24 15:51:44 2012 -0800
     4.3 @@ -0,0 +1,27 @@
     4.4 +/*
     4.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.7 + *
     4.8 + * This code is free software; you can redistribute it and/or modify it
     4.9 + * under the terms of the GNU General Public License version 2 only, as
    4.10 + * published by the Free Software Foundation.
    4.11 + *
    4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    4.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    4.15 + * version 2 for more details (a copy is included in the LICENSE file that
    4.16 + * accompanied this code).
    4.17 + *
    4.18 + * You should have received a copy of the GNU General Public License version
    4.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    4.21 + *
    4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    4.23 + * or visit www.oracle.com if you need additional information or have any
    4.24 + * questions.
    4.25 + */
    4.26 +
    4.27 +package xx;
    4.28 +class java {
    4.29 +    int fred;
    4.30 +}

mercurial