Thu, 25 Oct 2012 13:33:27 -0700
6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
Reviewed-by: jjg, mcimadamore
Contributed-by: vicente.romero@oracle.com
1.1 --- a/src/share/classes/com/sun/tools/javac/file/Locations.java Thu Oct 25 11:09:36 2012 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/file/Locations.java Thu Oct 25 13:33:27 2012 -0700 1.3 @@ -420,14 +420,10 @@ 1.4 if (!options.contains(option)) 1.5 return false; 1.6 searchPath = value == null ? null : 1.7 - Collections.unmodifiableCollection(computePath(value)); 1.8 + Collections.unmodifiableCollection(createPath().addFiles(value)); 1.9 return true; 1.10 } 1.11 1.12 - protected Path computePath(String value) { 1.13 - return new Path().addFiles(value); 1.14 - } 1.15 - 1.16 @Override 1.17 Collection<File> getLocation() { 1.18 return searchPath; 1.19 @@ -439,10 +435,18 @@ 1.20 if (files == null) { 1.21 p = computePath(null); 1.22 } else { 1.23 - p = new Path().addFiles(files); 1.24 + p = createPath().addFiles(files); 1.25 } 1.26 searchPath = Collections.unmodifiableCollection(p); 1.27 } 1.28 + 1.29 + protected Path computePath(String value) { 1.30 + return createPath().addFiles(value); 1.31 + } 1.32 + 1.33 + protected Path createPath() { 1.34 + return new Path(); 1.35 + } 1.36 } 1.37 1.38 /** 1.39 @@ -477,11 +481,15 @@ 1.40 // Default to current working directory. 1.41 if (cp == null) cp = "."; 1.42 1.43 + return createPath().addFiles(cp); 1.44 + } 1.45 + 1.46 + @Override 1.47 + protected Path createPath() { 1.48 return new Path() 1.49 - .expandJarClassPaths(true) // Only search user jars for Class-Paths 1.50 - .emptyPathDefault(new File(".")) // Empty path elt ==> current directory 1.51 - .addFiles(cp); 1.52 - } 1.53 + .expandJarClassPaths(true) // Only search user jars for Class-Paths 1.54 + .emptyPathDefault(new File(".")); // Empty path elt ==> current directory 1.55 + } 1.56 1.57 private void lazy() { 1.58 if (searchPath == null) 1.59 @@ -591,7 +599,6 @@ 1.60 String extdirsOpt = optionValues.get(EXTDIRS); 1.61 String xbootclasspathPrependOpt = optionValues.get(XBOOTCLASSPATH_PREPEND); 1.62 String xbootclasspathAppendOpt = optionValues.get(XBOOTCLASSPATH_APPEND); 1.63 - 1.64 path.addFiles(xbootclasspathPrependOpt); 1.65 1.66 if (endorseddirsOpt != null)
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/Paths/TestCompileJARInClassPath.java Thu Oct 25 13:33:27 2012 -0700 2.3 @@ -0,0 +1,132 @@ 2.4 +/* 2.5 + * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 6725230 2.30 + * @summary Test to make sure that java Compilation with JSR199 does not ignore 2.31 + * Class-Path in manifest 2.32 + * @author vicente.romero 2.33 + * @build TestCompileJARInClassPath 2.34 + * @run main TestCompileJARInClassPath 2.35 + */ 2.36 + 2.37 +import java.io.File; 2.38 +import java.io.FileOutputStream; 2.39 +import java.io.IOException; 2.40 +import java.io.PrintStream; 2.41 +import java.util.ArrayList; 2.42 +import java.util.List; 2.43 +import javax.tools.DiagnosticCollector; 2.44 +import javax.tools.JavaFileObject; 2.45 +import javax.tools.StandardJavaFileManager; 2.46 +import javax.tools.StandardLocation; 2.47 +import javax.tools.ToolProvider; 2.48 + 2.49 +public class TestCompileJARInClassPath { 2.50 + 2.51 + public static void main(String args[]) throws Exception { 2.52 + TestCompileJARInClassPath theTest = new TestCompileJARInClassPath(); 2.53 + theTest.run(); 2.54 + } 2.55 + 2.56 + void run() throws Exception { 2.57 + try { 2.58 + clean(); 2.59 + generateFilesNeeded(); 2.60 + compileWithJSR199(); 2.61 + } finally { 2.62 + clean(); 2.63 + } 2.64 + } 2.65 + 2.66 + void writeFile(String f, String contents) throws IOException { 2.67 + PrintStream s = new PrintStream(new FileOutputStream(f)); 2.68 + s.println(contents); 2.69 + s.close(); 2.70 + } 2.71 + 2.72 + void rm(String filename) throws Exception { 2.73 + File f = new File(filename); 2.74 + f.delete(); 2.75 + if (f.exists()) 2.76 + throw new Exception(filename + ": couldn't remove"); 2.77 + } 2.78 + 2.79 + void clean() throws Exception { 2.80 + rm("C1.java"); 2.81 + rm("C1.class"); 2.82 + rm("C1.jar"); 2.83 + 2.84 + rm("C2.java"); 2.85 + rm("C2.class"); 2.86 + rm("C2.jar"); 2.87 + rm("MANIFEST.MF"); 2.88 + 2.89 + rm("C3.java"); 2.90 + rm("C3.class"); 2.91 + } 2.92 + 2.93 + void generateFilesNeeded() throws Exception { 2.94 + sun.tools.jar.Main jarGenerator = new sun.tools.jar.Main(System.out, System.err, "jar"); 2.95 + 2.96 + writeFile("C1.java", 2.97 + "public class C1 {public static void f() {}}"); 2.98 + com.sun.tools.javac.Main.compile(new String[]{"C1.java"}); 2.99 + jarGenerator.run(new String[] {"cf", "C1.jar", "C1.class"}); 2.100 + 2.101 + writeFile("C2.java", 2.102 + "public class C2 {public static void g() {}}"); 2.103 + writeFile("MANIFEST.MF", 2.104 + "Manifest-Version: 1.0\n" + 2.105 + "Class-Path: C1.jar\n" + 2.106 + "Main-Class: C2"); 2.107 + com.sun.tools.javac.Main.compile(new String[]{"C2.java"}); 2.108 + jarGenerator.run(new String[] {"cfm", "C2.jar", "MANIFEST.MF", "C2.class"}); 2.109 + 2.110 + writeFile("C3.java", 2.111 + "public class C3 {public static void h() {C2.g(); C1.f();}}"); 2.112 + } 2.113 + 2.114 + void compileWithJSR199() throws IOException { 2.115 + String cpath = "C2.jar"; 2.116 + File clientJarFile = new File(cpath); 2.117 + File sourceFileToCompile = new File("C3.java"); 2.118 + 2.119 + 2.120 + javax.tools.JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); 2.121 + DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); 2.122 + StandardJavaFileManager stdFileManager = javac.getStandardFileManager(diagnostics, null, null); 2.123 + 2.124 + List<File> files = new ArrayList<>(); 2.125 + files.add(clientJarFile); 2.126 + 2.127 + stdFileManager.setLocation(StandardLocation.CLASS_PATH, files); 2.128 + 2.129 + Iterable<? extends JavaFileObject> sourceFiles = stdFileManager.getJavaFileObjects(sourceFileToCompile); 2.130 + 2.131 + if (!javac.getTask(null, stdFileManager, diagnostics, null, null, sourceFiles).call()) { 2.132 + throw new AssertionError("compilation failed"); 2.133 + } 2.134 + } 2.135 +}