test/tools/javac/6400872/T6400872.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) 2006, 2007, 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 6400872
aoqi@0 27 * @summary REGRESSION: Java Compiler cannot find jar files referenced by other
aoqi@0 28 * @run main T6400872
aoqi@0 29 */
aoqi@0 30
aoqi@0 31 // ${TESTJAVA}/bin/javac -d ${TESTCLASSES} ${TESTSRC}/A.java ${TESTSRC}/B.java
aoqi@0 32 // ${TESTJAVA}/bin/jar -cfm A.jar ${TESTSRC}/A/META-INF/MANIFEST.MF -C ${TESTCLASSES} A.class
aoqi@0 33 // ${TESTJAVA}/bin/jar -cfm B.jar ${TESTSRC}/B/META-INF/MANIFEST.MF -C ${TESTCLASSES} B.class
aoqi@0 34 // ${TESTJAVA}/bin/javac -cp A.jar ${TESTSRC}/C.java
aoqi@0 35
aoqi@0 36 import java.io.*;
aoqi@0 37 import java.nio.*;
aoqi@0 38 import java.util.*;
aoqi@0 39 import java.util.jar.*;
aoqi@0 40 import javax.tools.*;
aoqi@0 41 import javax.tools.StandardJavaFileManager.*;
aoqi@0 42
aoqi@0 43 public class T6400872 {
aoqi@0 44 static File testSrc = new File(System.getProperty("test.src", "."));
aoqi@0 45 static File testClasses = new File(System.getProperty("test.classes", "."));
aoqi@0 46
aoqi@0 47 public static void main(String... args) throws Exception {
aoqi@0 48 // compile A.java and B.java
aoqi@0 49 compile(testClasses, null, new File(testSrc, "A.java"), new File(testSrc, "B.java"));
aoqi@0 50 // put them in mutually referential class files
aoqi@0 51 jar(new File("A.jar"), iterable(new File(".", "B.jar")), testClasses, new File("A.class"));
aoqi@0 52 jar(new File("B.jar"), iterable(new File(".", "A.jar")), testClasses, new File("B.class"));
aoqi@0 53 // verify we can successfully use the class path entries in the jar files
aoqi@0 54 compile(new File("."), iterable(new File("A.jar")), new File(testSrc, "C.java"));
aoqi@0 55 }
aoqi@0 56
aoqi@0 57 static void compile(File classOutDir, Iterable<File> classPath, File... files)
aoqi@0 58 throws IOException {
aoqi@0 59 System.err.println("compile...");
aoqi@0 60 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
aoqi@0 61 StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
aoqi@0 62 try {
aoqi@0 63 Iterable<? extends JavaFileObject> fileObjects =
aoqi@0 64 fm.getJavaFileObjectsFromFiles(Arrays.asList(files));
aoqi@0 65
aoqi@0 66 List<String> options = new ArrayList<String>();
aoqi@0 67 if (classOutDir != null) {
aoqi@0 68 options.add("-d");
aoqi@0 69 options.add(classOutDir.getPath());
aoqi@0 70 }
aoqi@0 71 if (classPath != null) {
aoqi@0 72 options.add("-classpath");
aoqi@0 73 options.add(join(classPath, File.pathSeparator));
aoqi@0 74 }
aoqi@0 75 options.add("-verbose");
aoqi@0 76
aoqi@0 77 JavaCompiler.CompilationTask task =
aoqi@0 78 compiler.getTask(null, fm, null, options, null, fileObjects);
aoqi@0 79 if (!task.call())
aoqi@0 80 throw new AssertionError("compilation failed");
aoqi@0 81 } finally {
aoqi@0 82 fm.close();
aoqi@0 83 }
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 static void jar(File jar, Iterable<File> classPath, File base, File... files)
aoqi@0 87 throws IOException {
aoqi@0 88 System.err.println("jar...");
aoqi@0 89 Manifest m = new Manifest();
aoqi@0 90 if (classPath != null) {
aoqi@0 91 Attributes mainAttrs = m.getMainAttributes();
aoqi@0 92 mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
aoqi@0 93 mainAttrs.put(Attributes.Name.CLASS_PATH, join(classPath, " "));
aoqi@0 94 }
aoqi@0 95 OutputStream out = new BufferedOutputStream(new FileOutputStream(jar));
aoqi@0 96 JarOutputStream j = new JarOutputStream(out, m);
aoqi@0 97 add(j, base, files);
aoqi@0 98 j.close();
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 static void add(JarOutputStream j, File base, File... files) throws IOException {
aoqi@0 102 if (files == null)
aoqi@0 103 return;
aoqi@0 104
aoqi@0 105 for (File f: files)
aoqi@0 106 add(j, base, f);
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 static void add(JarOutputStream j, File base, File file) throws IOException {
aoqi@0 110 File f = new File(base, file.getPath());
aoqi@0 111 if (f.isDirectory()) {
aoqi@0 112 String[] children = f.list();
aoqi@0 113 if (children != null)
aoqi@0 114 for (String c: children)
aoqi@0 115 add(j, base, new File(file, c));
aoqi@0 116 } else {
aoqi@0 117 JarEntry e = new JarEntry(file.getPath());
aoqi@0 118 e.setSize(f.length());
aoqi@0 119 j.putNextEntry(e);
aoqi@0 120 j.write(read(f));
aoqi@0 121 j.closeEntry();
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 static byte[] read(File f) throws IOException {
aoqi@0 127 byte[] buf = new byte[(int) f.length()];
aoqi@0 128 BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
aoqi@0 129 int offset = 0;
aoqi@0 130 while (offset < buf.length) {
aoqi@0 131 int n = in.read(buf, offset, buf.length - offset);
aoqi@0 132 if (n < 0)
aoqi@0 133 throw new EOFException();
aoqi@0 134 offset += n;
aoqi@0 135 }
aoqi@0 136 return buf;
aoqi@0 137 }
aoqi@0 138
aoqi@0 139 static <T> Iterable<T> iterable(T single) {
aoqi@0 140 return Collections.singleton(single);
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 static <T> String join(Iterable<T> iter, String sep) {
aoqi@0 144 StringBuilder p = new StringBuilder();
aoqi@0 145 for (T t: iter) {
aoqi@0 146 if (p.length() > 0)
aoqi@0 147 p.append(' ');
aoqi@0 148 p.append(t);
aoqi@0 149 }
aoqi@0 150 return p.toString();
aoqi@0 151 }
aoqi@0 152 }

mercurial