6638501: Regression with Javac in JDK6 U4 b03?
authorjjg
Fri, 14 Mar 2008 16:09:30 -0700
changeset 171 2afbdd3321d0
parent 170 68aea6dcb358
child 172 249e283e0446
6638501: Regression with Javac in JDK6 U4 b03? Summary: replace some String paths with File paths in Paths.java Reviewed-by: ksrini
langtools/src/share/classes/com/sun/tools/javac/util/Paths.java
langtools/test/tools/javac/Paths/6638501/HelloLib/test/HelloImpl.java
langtools/test/tools/javac/Paths/6638501/JarFromManifestFailure.java
langtools/test/tools/javac/Paths/6638501/WsCompileExample.java
langtools/test/tools/javac/Paths/6638501/test/SayHello.java
langtools/test/tools/javac/Paths/6638501/test1/SayHelloToo.java
--- a/langtools/src/share/classes/com/sun/tools/javac/util/Paths.java	Thu Mar 13 13:42:38 2008 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/util/Paths.java	Fri Mar 14 16:09:30 2008 -0700
@@ -38,13 +38,8 @@
 import java.util.LinkedHashSet;
 import java.util.Iterator;
 import java.util.StringTokenizer;
-import java.util.zip.ZipException;
 import java.util.zip.ZipFile;
 import com.sun.tools.javac.code.Lint;
-import com.sun.tools.javac.util.Context;
-import com.sun.tools.javac.util.Log;
-import com.sun.tools.javac.util.Options;
-import com.sun.tools.javac.util.Position;
 import java.util.ArrayList;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.locks.Lock;
@@ -70,7 +65,10 @@
     protected static final Context.Key<Paths> pathsKey =
         new Context.Key<Paths>();
 
-    /** Get the Paths instance for this context. */
+    /** Get the Paths instance for this context.
+     *  @param context the context
+     *  @return the Paths instance for this context
+     */
     public static Paths instance(Context context) {
         Paths instance = context.get(pathsKey);
         if (instance == null)
@@ -89,7 +87,7 @@
 
     private static boolean NON_BATCH_MODE = System.getProperty("nonBatchMode") != null;// TODO: Use -XD compiler switch for this.
     private static Map<File, PathEntry> pathExistanceCache = new ConcurrentHashMap<File, PathEntry>();
-    private static Map<File, java.util.List<String>> manifestEntries = new ConcurrentHashMap<File, java.util.List<String>>();
+    private static Map<File, java.util.List<File>> manifestEntries = new ConcurrentHashMap<File, java.util.List<File>>();
     private static Map<File, Boolean> isDirectory = new ConcurrentHashMap<File, Boolean>();
     private static Lock lock = new ReentrantLock();
 
@@ -369,13 +367,13 @@
         // filenames, but if we do, we should redo all path-related code.
         private void addJarClassPath(File jarFile, boolean warn) {
             try {
-                java.util.List<String> manifestsList = manifestEntries.get(jarFile);
+                java.util.List<File> manifestsList = manifestEntries.get(jarFile);
                 if (!NON_BATCH_MODE) {
                     lock.lock();
                     try {
                         if (manifestsList != null) {
-                            for (String entr : manifestsList) {
-                                addFile(new File(entr), warn);
+                            for (File entr : manifestsList) {
+                                addFile(entr, warn);
                             }
                             return;
                         }
@@ -386,7 +384,7 @@
                 }
 
                 if (!NON_BATCH_MODE) {
-                    manifestsList = new ArrayList<String>();
+                    manifestsList = new ArrayList<File>();
                     manifestEntries.put(jarFile, manifestsList);
                 }
 
@@ -412,7 +410,7 @@
                         if (!NON_BATCH_MODE) {
                             lock.lock();
                             try {
-                                manifestsList.add(elt);
+                                manifestsList.add(f);
                             }
                             finally {
                                 lock.unlock();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/Paths/6638501/HelloLib/test/HelloImpl.java	Fri Mar 14 16:09:30 2008 -0700
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2007-2008 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.
+ */
+
+package test;
+
+public class HelloImpl {
+
+ public void Hello() {
+    java.lang.System.out.println("Hello");
+ }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/Paths/6638501/JarFromManifestFailure.java	Fri Mar 14 16:09:30 2008 -0700
@@ -0,0 +1,169 @@
+/*
+ * Copyright 2007-2008 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 6638501
+ * @summary REGRESSION:  Java Compiler cannot find jar files referenced by other
+ * @run main JarFromManifestFailure
+ */
+
+import java.io.*;
+import java.nio.*;
+import java.util.*;
+import java.util.jar.*;
+import javax.tools.*;
+import javax.tools.StandardJavaFileManager.*;
+
+public class JarFromManifestFailure {
+    static File testSrc = new File(System.getProperty("test.src", "."));
+    static File testClasses = new File(System.getProperty("test.classes", "."));
+
+    public static void main(String... args) throws Exception {
+        compile(testClasses, null, new File(testSrc, "HelloLib/test/HelloImpl.java"), new File(testSrc, "WsCompileExample.java"));
+        File libFile = new File(testClasses, "lib");
+        libFile.mkdir();
+        jar(new File(libFile, "HelloLib.jar"), new ArrayList(), testClasses, new File("test"));
+
+        ArrayList arList = new ArrayList();
+        arList.add(new File("HelloLib.jar"));
+        jar(new File(libFile, "JarPointer.jar"), arList, testClasses);
+
+        String[] args1 = {
+            "-d", ".",
+            "-cp", new File(libFile, "JarPointer.jar").getPath().replace('\\', '/'),
+            new File(testSrc, "test/SayHello.java").getPath().replace('\\', '/')
+        };
+        System.err.println("First compile!!!");
+        if (com.sun.tools.javac.Main.compile(args1) != 0) {
+            throw new AssertionError("Failure in first compile!");
+        }
+
+        System.err.println("Second compile!!!");
+
+        args1 = new String[] {
+            "-d", ".",
+            "-cp", new File(libFile, "JarPointer.jar").getPath().replace('\\', '/'),
+            new File(testSrc, "test1/SayHelloToo.java").getPath().replace('\\', '/')
+        };
+        if (com.sun.tools.javac.Main.compile(args1) != 0) {
+            throw new AssertionError("Failure in second compile!");
+        }
+    }
+
+    static void compile(File classOutDir, Iterable<File> classPath, File... files) {
+        System.err.println("compile...");
+        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+        StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
+        Iterable<? extends JavaFileObject> fileObjects =
+                    fm.getJavaFileObjectsFromFiles(Arrays.asList(files));
+
+        List<String> options = new ArrayList<String>();
+        if (classOutDir != null) {
+            options.add("-d");
+            options.add(classOutDir.getPath());
+        }
+        if (classPath != null) {
+            options.add("-classpath");
+            options.add(join(classPath, File.pathSeparator));
+        }
+        options.add("-verbose");
+
+        JavaCompiler.CompilationTask task =
+            compiler.getTask(null, fm, null, options, null, fileObjects);
+        if (!task.call())
+            throw new AssertionError("compilation failed");
+    }
+
+    static void jar(File jar, Iterable<File> classPath, File base, File... files)
+            throws IOException {
+        System.err.println("jar...");
+        Manifest m = new Manifest();
+        if (classPath != null) {
+            Attributes mainAttrs = m.getMainAttributes();
+            mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
+            mainAttrs.put(Attributes.Name.CLASS_PATH, join(classPath, " "));
+        }
+        OutputStream out = new BufferedOutputStream(new FileOutputStream(jar));
+        JarOutputStream j = new JarOutputStream(out, m);
+        add(j, base, files);
+        j.close();
+    }
+
+    static void add(JarOutputStream j, File base, File... files) throws IOException {
+        if (files == null)
+            return;
+
+        for (File f: files)
+            add(j, base, f);
+    }
+
+    static void add(JarOutputStream j, File base, File file) throws IOException {
+        File f = new File(base, file.getPath());
+        if (f.isDirectory()) {
+            JarEntry e = new JarEntry(new String(file.getPath() + File.separator).replace('\\', '/'));
+            e.setSize(file.length());
+            j.putNextEntry(e);
+            String[] children = f.list();
+            if (children != null) {
+                for (String c: children) {
+                    add(j, base, new File(file, c));
+                }
+            }
+        } else {
+            JarEntry e = new JarEntry(file.getPath().replace('\\', '/'));
+            e.setSize(f.length());
+            j.putNextEntry(e);
+            j.write(read(f));
+            j.closeEntry();
+        }
+
+    }
+
+    static byte[] read(File f) throws IOException {
+        byte[] buf = new byte[(int) f.length()];
+        BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
+        int offset = 0;
+        while (offset < buf.length) {
+            int n = in.read(buf, offset, buf.length - offset);
+            if (n < 0)
+                throw new EOFException();
+            offset += n;
+        }
+        return buf;
+    }
+
+    static <T> Iterable<T> iterable(T single) {
+        return Collections.singleton(single);
+    }
+
+    static <T> String join(Iterable<T> iter, String sep) {
+        StringBuilder p = new StringBuilder();
+        for (T t: iter) {
+            if (p.length() > 0)
+                p.append(' ');
+            p.append(t);
+        }
+        return p.toString();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/Paths/6638501/WsCompileExample.java	Fri Mar 14 16:09:30 2008 -0700
@@ -0,0 +1,189 @@
+/*
+ * Copyright 2007-2008 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.
+ */
+
+import java.util.List;
+import java.util.ArrayList;
+import java.io.File;
+//for CompilerHelper
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+
+
+
+public class WsCompileExample {
+    File destDir;
+    File srcDir;
+    protected boolean compilerDebug = false;
+    protected boolean compilerOptimize = false;
+    protected String userClasspath = null;
+
+    public static void main(String[] args) {
+        new WsCompileExample().do_main(args);
+    }
+
+    public void do_main(String[] args) {
+        if(!args[0].equals("-s")) {
+            throw new RuntimeException("specify -s for src");
+        }
+
+        //run it once
+        srcDir =  new File(args[1]);
+        if(!args[2].equals("-d")) {
+            throw new RuntimeException("specify -d for dest");
+        }
+        destDir =  new File(args[3]);
+        if(!destDir.exists())
+            destDir.mkdirs();
+        System.out.println("----test compile 1-----");
+        compileGeneratedClasses();
+
+        //run it twice
+         srcDir = new File(args[1]+"1");
+         destDir =  new File(args[3]+"1");
+        if(!destDir.exists())
+            destDir.mkdirs();
+        System.out.println("----test compile 2-----");
+        compileGeneratedClasses();
+
+    }
+    protected void compileGeneratedClasses() {
+        List sourceFiles = new ArrayList();
+
+        for (File f: srcDir.listFiles()) {
+            if (f.getName().endsWith(".java")) {
+                sourceFiles.add(f.getAbsolutePath());
+            }
+        }
+
+        if (sourceFiles.size() > 0) {
+                                String classDir = destDir.getAbsolutePath();
+            String classpathString = createClasspathString();
+            System.out.println("classpathString: " + classpathString);
+
+                                String[] args = new String[4 + (compilerDebug == true ? 1 : 0) +
+                (compilerOptimize == true ? 1 : 0) +
+                sourceFiles.size()];
+            args[0] = "-d";
+            args[1] = classDir;
+            args[2] = "-classpath";
+            args[3] = classpathString;
+//                              args[4]="-DnonBatchMode";
+            int baseIndex = 4;
+            if (compilerDebug) {
+                args[baseIndex++] = "-g";
+            }
+            if (compilerOptimize) {
+                args[baseIndex++] = "-O";
+            }
+            for (int i = 0; i < sourceFiles.size(); ++i) {
+                args[baseIndex + i] = (String)sourceFiles.get(i);
+            }
+
+            // ByteArrayOutputStream javacOutput = new ByteArrayOutputStream();
+            JavaCompilerHelper compilerHelper = new JavaCompilerHelper(System.out);
+            boolean result = compilerHelper.compile(args);
+            if (!result) {
+                System.out.println("wscompile.compilation Failed");
+            }
+        }
+    }
+
+    protected String createClasspathString() {
+        if (userClasspath == null) {
+            userClasspath = "";
+        }
+                          String jcp = userClasspath + File.pathSeparator + System.getProperty("java.class.path");
+                  return jcp;
+    }
+}
+///////////////////////////////////////////////////////////////////
+class JavaCompilerHelper {
+    public JavaCompilerHelper(OutputStream out) {
+                this.out = out;
+        }
+
+        public boolean compile(String[] args) {
+                return internalCompile(args);
+        }
+
+        protected boolean internalCompile(String[] args) {
+
+                System.out.println("Args: ");
+                for(String arg : args){
+                        System.out.print(arg+" ");
+                }
+        System.out.println();
+                ClassLoader cl = Thread.currentThread().getContextClassLoader();
+                Class comSunToolsJavacMainClass = null;
+                try {
+                        /* try to use the new compiler */
+                        comSunToolsJavacMainClass =
+                                cl.loadClass("com.sun.tools.javac.Main");
+                        try {
+                                Method compileMethod =
+                                        comSunToolsJavacMainClass.getMethod(
+                                                "compile",
+                                                compile141MethodSignature);
+                                try {
+                                        Object result =
+                                                compileMethod.invoke(
+                                                        null,
+                                                        new Object[] { args, new PrintWriter(out)});
+                                        if (!(result instanceof Integer)) {
+                                                return false;
+                                        }
+                                        return ((Integer) result).intValue() == 0;
+                                } catch (IllegalAccessException e3) {
+                                        return false;
+                                } catch (IllegalArgumentException e3) {
+                                        return false;
+                                } catch (InvocationTargetException e3) {
+                                        return false;
+                                }
+                        } catch (NoSuchMethodException e2) {
+              System.out.println("ERROR: Compile failed with error:" + e2.toString() );
+                        }
+                } catch (ClassNotFoundException e) {
+                        e.printStackTrace();
+                        return false;
+                } catch (SecurityException e) {
+                        return false;
+                }
+                return true;
+        }
+
+        protected String getGenericErrorMessage() {return "javacompiler.error"; }
+        protected void run() {  }
+        protected boolean parseArguments(String[] args) {return false;}
+        protected OutputStream out;
+
+        protected static final Class[] compile141MethodSignature;
+        static
+        {
+                compile141MethodSignature = new Class[2];
+                compile141MethodSignature[0] = (new String[0]).getClass();
+                compile141MethodSignature[1] = PrintWriter.class;
+        }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/Paths/6638501/test/SayHello.java	Fri Mar 14 16:09:30 2008 -0700
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2007-2008 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.
+ */
+
+import test.HelloImpl;
+
+public class SayHello extends HelloImpl {
+  public static void main(String... args) {
+    new SayHello().Hello();
+}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/Paths/6638501/test1/SayHelloToo.java	Fri Mar 14 16:09:30 2008 -0700
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2007-2008 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.
+ */
+
+import test.HelloImpl;
+
+public class SayHelloToo extends HelloImpl {
+  public static void main(String... args) {
+    new SayHelloToo().Hello();
+}
+
+}