test/tools/javac/6400872/T6400872.java

Thu, 25 Aug 2011 17:18:25 -0700

author
schien
date
Thu, 25 Aug 2011 17:18:25 -0700
changeset 1067
f497fac86cf9
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8-b02 for changeset b3c059de2a61

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

mercurial