Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/6400872/T6400872.java
38813 views
/*1* Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 640087226* @summary REGRESSION: Java Compiler cannot find jar files referenced by other27* @run main T640087228*/2930// ${TESTJAVA}/bin/javac -d ${TESTCLASSES} ${TESTSRC}/A.java ${TESTSRC}/B.java31// ${TESTJAVA}/bin/jar -cfm A.jar ${TESTSRC}/A/META-INF/MANIFEST.MF -C ${TESTCLASSES} A.class32// ${TESTJAVA}/bin/jar -cfm B.jar ${TESTSRC}/B/META-INF/MANIFEST.MF -C ${TESTCLASSES} B.class33// ${TESTJAVA}/bin/javac -cp A.jar ${TESTSRC}/C.java3435import java.io.*;36import java.nio.*;37import java.util.*;38import java.util.jar.*;39import javax.tools.*;40import javax.tools.StandardJavaFileManager.*;4142public class T6400872 {43static File testSrc = new File(System.getProperty("test.src", "."));44static File testClasses = new File(System.getProperty("test.classes", "."));4546public static void main(String... args) throws Exception {47// compile A.java and B.java48compile(testClasses, null, new File(testSrc, "A.java"), new File(testSrc, "B.java"));49// put them in mutually referential class files50jar(new File("A.jar"), iterable(new File(".", "B.jar")), testClasses, new File("A.class"));51jar(new File("B.jar"), iterable(new File(".", "A.jar")), testClasses, new File("B.class"));52// verify we can successfully use the class path entries in the jar files53compile(new File("."), iterable(new File("A.jar")), new File(testSrc, "C.java"));54}5556static void compile(File classOutDir, Iterable<File> classPath, File... files)57throws IOException {58System.err.println("compile...");59JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();60StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);61try {62Iterable<? extends JavaFileObject> fileObjects =63fm.getJavaFileObjectsFromFiles(Arrays.asList(files));6465List<String> options = new ArrayList<String>();66if (classOutDir != null) {67options.add("-d");68options.add(classOutDir.getPath());69}70if (classPath != null) {71options.add("-classpath");72options.add(join(classPath, File.pathSeparator));73}74options.add("-verbose");7576JavaCompiler.CompilationTask task =77compiler.getTask(null, fm, null, options, null, fileObjects);78if (!task.call())79throw new AssertionError("compilation failed");80} finally {81fm.close();82}83}8485static void jar(File jar, Iterable<File> classPath, File base, File... files)86throws IOException {87System.err.println("jar...");88Manifest m = new Manifest();89if (classPath != null) {90Attributes mainAttrs = m.getMainAttributes();91mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");92mainAttrs.put(Attributes.Name.CLASS_PATH, join(classPath, " "));93}94OutputStream out = new BufferedOutputStream(new FileOutputStream(jar));95JarOutputStream j = new JarOutputStream(out, m);96add(j, base, files);97j.close();98}99100static void add(JarOutputStream j, File base, File... files) throws IOException {101if (files == null)102return;103104for (File f: files)105add(j, base, f);106}107108static void add(JarOutputStream j, File base, File file) throws IOException {109File f = new File(base, file.getPath());110if (f.isDirectory()) {111String[] children = f.list();112if (children != null)113for (String c: children)114add(j, base, new File(file, c));115} else {116JarEntry e = new JarEntry(file.getPath());117e.setSize(f.length());118j.putNextEntry(e);119j.write(read(f));120j.closeEntry();121}122123}124125static byte[] read(File f) throws IOException {126byte[] buf = new byte[(int) f.length()];127BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));128int offset = 0;129while (offset < buf.length) {130int n = in.read(buf, offset, buf.length - offset);131if (n < 0)132throw new EOFException();133offset += n;134}135return buf;136}137138static <T> Iterable<T> iterable(T single) {139return Collections.singleton(single);140}141142static <T> String join(Iterable<T> iter, String sep) {143StringBuilder p = new StringBuilder();144for (T t: iter) {145if (p.length() > 0)146p.append(' ');147p.append(t);148}149return p.toString();150}151}152153154