test/tools/javac/Paths/6638501/JarFromManifestFailure.java

Fri, 14 Mar 2008 16:09:30 -0700

author
jjg
date
Fri, 14 Mar 2008 16:09:30 -0700
changeset 14
58039502942e
child 554
9d9f26857129
permissions
-rw-r--r--

6638501: Regression with Javac in JDK6 U4 b03?
Summary: replace some String paths with File paths in Paths.java
Reviewed-by: ksrini

jjg@14 1 /*
jjg@14 2 * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
jjg@14 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@14 4 *
jjg@14 5 * This code is free software; you can redistribute it and/or modify it
jjg@14 6 * under the terms of the GNU General Public License version 2 only, as
jjg@14 7 * published by the Free Software Foundation.
jjg@14 8 *
jjg@14 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@14 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@14 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@14 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@14 13 * accompanied this code).
jjg@14 14 *
jjg@14 15 * You should have received a copy of the GNU General Public License version
jjg@14 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@14 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@14 18 *
jjg@14 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@14 20 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@14 21 * have any questions.
jjg@14 22 */
jjg@14 23
jjg@14 24 /*
jjg@14 25 * @test
jjg@14 26 * @bug 6638501
jjg@14 27 * @summary REGRESSION: Java Compiler cannot find jar files referenced by other
jjg@14 28 * @run main JarFromManifestFailure
jjg@14 29 */
jjg@14 30
jjg@14 31 import java.io.*;
jjg@14 32 import java.nio.*;
jjg@14 33 import java.util.*;
jjg@14 34 import java.util.jar.*;
jjg@14 35 import javax.tools.*;
jjg@14 36 import javax.tools.StandardJavaFileManager.*;
jjg@14 37
jjg@14 38 public class JarFromManifestFailure {
jjg@14 39 static File testSrc = new File(System.getProperty("test.src", "."));
jjg@14 40 static File testClasses = new File(System.getProperty("test.classes", "."));
jjg@14 41
jjg@14 42 public static void main(String... args) throws Exception {
jjg@14 43 compile(testClasses, null, new File(testSrc, "HelloLib/test/HelloImpl.java"), new File(testSrc, "WsCompileExample.java"));
jjg@14 44 File libFile = new File(testClasses, "lib");
jjg@14 45 libFile.mkdir();
jjg@14 46 jar(new File(libFile, "HelloLib.jar"), new ArrayList(), testClasses, new File("test"));
jjg@14 47
jjg@14 48 ArrayList arList = new ArrayList();
jjg@14 49 arList.add(new File("HelloLib.jar"));
jjg@14 50 jar(new File(libFile, "JarPointer.jar"), arList, testClasses);
jjg@14 51
jjg@14 52 String[] args1 = {
jjg@14 53 "-d", ".",
jjg@14 54 "-cp", new File(libFile, "JarPointer.jar").getPath().replace('\\', '/'),
jjg@14 55 new File(testSrc, "test/SayHello.java").getPath().replace('\\', '/')
jjg@14 56 };
jjg@14 57 System.err.println("First compile!!!");
jjg@14 58 if (com.sun.tools.javac.Main.compile(args1) != 0) {
jjg@14 59 throw new AssertionError("Failure in first compile!");
jjg@14 60 }
jjg@14 61
jjg@14 62 System.err.println("Second compile!!!");
jjg@14 63
jjg@14 64 args1 = new String[] {
jjg@14 65 "-d", ".",
jjg@14 66 "-cp", new File(libFile, "JarPointer.jar").getPath().replace('\\', '/'),
jjg@14 67 new File(testSrc, "test1/SayHelloToo.java").getPath().replace('\\', '/')
jjg@14 68 };
jjg@14 69 if (com.sun.tools.javac.Main.compile(args1) != 0) {
jjg@14 70 throw new AssertionError("Failure in second compile!");
jjg@14 71 }
jjg@14 72 }
jjg@14 73
jjg@14 74 static void compile(File classOutDir, Iterable<File> classPath, File... files) {
jjg@14 75 System.err.println("compile...");
jjg@14 76 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
jjg@14 77 StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
jjg@14 78 Iterable<? extends JavaFileObject> fileObjects =
jjg@14 79 fm.getJavaFileObjectsFromFiles(Arrays.asList(files));
jjg@14 80
jjg@14 81 List<String> options = new ArrayList<String>();
jjg@14 82 if (classOutDir != null) {
jjg@14 83 options.add("-d");
jjg@14 84 options.add(classOutDir.getPath());
jjg@14 85 }
jjg@14 86 if (classPath != null) {
jjg@14 87 options.add("-classpath");
jjg@14 88 options.add(join(classPath, File.pathSeparator));
jjg@14 89 }
jjg@14 90 options.add("-verbose");
jjg@14 91
jjg@14 92 JavaCompiler.CompilationTask task =
jjg@14 93 compiler.getTask(null, fm, null, options, null, fileObjects);
jjg@14 94 if (!task.call())
jjg@14 95 throw new AssertionError("compilation failed");
jjg@14 96 }
jjg@14 97
jjg@14 98 static void jar(File jar, Iterable<File> classPath, File base, File... files)
jjg@14 99 throws IOException {
jjg@14 100 System.err.println("jar...");
jjg@14 101 Manifest m = new Manifest();
jjg@14 102 if (classPath != null) {
jjg@14 103 Attributes mainAttrs = m.getMainAttributes();
jjg@14 104 mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
jjg@14 105 mainAttrs.put(Attributes.Name.CLASS_PATH, join(classPath, " "));
jjg@14 106 }
jjg@14 107 OutputStream out = new BufferedOutputStream(new FileOutputStream(jar));
jjg@14 108 JarOutputStream j = new JarOutputStream(out, m);
jjg@14 109 add(j, base, files);
jjg@14 110 j.close();
jjg@14 111 }
jjg@14 112
jjg@14 113 static void add(JarOutputStream j, File base, File... files) throws IOException {
jjg@14 114 if (files == null)
jjg@14 115 return;
jjg@14 116
jjg@14 117 for (File f: files)
jjg@14 118 add(j, base, f);
jjg@14 119 }
jjg@14 120
jjg@14 121 static void add(JarOutputStream j, File base, File file) throws IOException {
jjg@14 122 File f = new File(base, file.getPath());
jjg@14 123 if (f.isDirectory()) {
jjg@14 124 JarEntry e = new JarEntry(new String(file.getPath() + File.separator).replace('\\', '/'));
jjg@14 125 e.setSize(file.length());
jjg@14 126 j.putNextEntry(e);
jjg@14 127 String[] children = f.list();
jjg@14 128 if (children != null) {
jjg@14 129 for (String c: children) {
jjg@14 130 add(j, base, new File(file, c));
jjg@14 131 }
jjg@14 132 }
jjg@14 133 } else {
jjg@14 134 JarEntry e = new JarEntry(file.getPath().replace('\\', '/'));
jjg@14 135 e.setSize(f.length());
jjg@14 136 j.putNextEntry(e);
jjg@14 137 j.write(read(f));
jjg@14 138 j.closeEntry();
jjg@14 139 }
jjg@14 140
jjg@14 141 }
jjg@14 142
jjg@14 143 static byte[] read(File f) throws IOException {
jjg@14 144 byte[] buf = new byte[(int) f.length()];
jjg@14 145 BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
jjg@14 146 int offset = 0;
jjg@14 147 while (offset < buf.length) {
jjg@14 148 int n = in.read(buf, offset, buf.length - offset);
jjg@14 149 if (n < 0)
jjg@14 150 throw new EOFException();
jjg@14 151 offset += n;
jjg@14 152 }
jjg@14 153 return buf;
jjg@14 154 }
jjg@14 155
jjg@14 156 static <T> Iterable<T> iterable(T single) {
jjg@14 157 return Collections.singleton(single);
jjg@14 158 }
jjg@14 159
jjg@14 160 static <T> String join(Iterable<T> iter, String sep) {
jjg@14 161 StringBuilder p = new StringBuilder();
jjg@14 162 for (T t: iter) {
jjg@14 163 if (p.length() > 0)
jjg@14 164 p.append(' ');
jjg@14 165 p.append(t);
jjg@14 166 }
jjg@14 167 return p.toString();
jjg@14 168 }
jjg@14 169 }

mercurial