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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 554
9d9f26857129
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial