test/tools/javac/Paths/TestCompileJARInClassPath.java

Mon, 26 Oct 2015 13:23:30 -0700

author
asaha
date
Mon, 26 Oct 2015 13:23:30 -0700
changeset 2999
683b3e7e05a7
parent 0
959103a6100f
permissions
-rw-r--r--

Added tag jdk8u76-b00 for changeset 10ffafaf5340

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2002, 2012, 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 6725230
aoqi@0 27 * @summary Test to make sure that java Compilation with JSR199 does not ignore
aoqi@0 28 * Class-Path in manifest
aoqi@0 29 * @author vicente.romero
aoqi@0 30 * @build TestCompileJARInClassPath
aoqi@0 31 * @run main TestCompileJARInClassPath
aoqi@0 32 */
aoqi@0 33
aoqi@0 34 import java.io.File;
aoqi@0 35 import java.io.FileOutputStream;
aoqi@0 36 import java.io.IOException;
aoqi@0 37 import java.io.PrintStream;
aoqi@0 38 import java.util.ArrayList;
aoqi@0 39 import java.util.List;
aoqi@0 40 import javax.tools.DiagnosticCollector;
aoqi@0 41 import javax.tools.JavaFileObject;
aoqi@0 42 import javax.tools.StandardJavaFileManager;
aoqi@0 43 import javax.tools.StandardLocation;
aoqi@0 44 import javax.tools.ToolProvider;
aoqi@0 45
aoqi@0 46 public class TestCompileJARInClassPath {
aoqi@0 47
aoqi@0 48 public static void main(String args[]) throws Exception {
aoqi@0 49 TestCompileJARInClassPath theTest = new TestCompileJARInClassPath();
aoqi@0 50 theTest.run();
aoqi@0 51 }
aoqi@0 52
aoqi@0 53 void run() throws Exception {
aoqi@0 54 try {
aoqi@0 55 clean();
aoqi@0 56 generateFilesNeeded();
aoqi@0 57 compileWithJSR199();
aoqi@0 58 } finally {
aoqi@0 59 clean();
aoqi@0 60 }
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 void writeFile(String f, String contents) throws IOException {
aoqi@0 64 PrintStream s = new PrintStream(new FileOutputStream(f));
aoqi@0 65 s.println(contents);
aoqi@0 66 s.close();
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 void rm(String filename) throws Exception {
aoqi@0 70 File f = new File(filename);
aoqi@0 71 f.delete();
aoqi@0 72 if (f.exists())
aoqi@0 73 throw new Exception(filename + ": couldn't remove");
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 void clean() throws Exception {
aoqi@0 77 rm("C1.java");
aoqi@0 78 rm("C1.class");
aoqi@0 79 rm("C1.jar");
aoqi@0 80
aoqi@0 81 rm("C2.java");
aoqi@0 82 rm("C2.class");
aoqi@0 83 rm("C2.jar");
aoqi@0 84 rm("MANIFEST.MF");
aoqi@0 85
aoqi@0 86 rm("C3.java");
aoqi@0 87 rm("C3.class");
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 void generateFilesNeeded() throws Exception {
aoqi@0 91 sun.tools.jar.Main jarGenerator = new sun.tools.jar.Main(System.out, System.err, "jar");
aoqi@0 92
aoqi@0 93 writeFile("C1.java",
aoqi@0 94 "public class C1 {public static void f() {}}");
aoqi@0 95 com.sun.tools.javac.Main.compile(new String[]{"C1.java"});
aoqi@0 96 jarGenerator.run(new String[] {"cf", "C1.jar", "C1.class"});
aoqi@0 97
aoqi@0 98 writeFile("C2.java",
aoqi@0 99 "public class C2 {public static void g() {}}");
aoqi@0 100 writeFile("MANIFEST.MF",
aoqi@0 101 "Manifest-Version: 1.0\n" +
aoqi@0 102 "Class-Path: C1.jar\n" +
aoqi@0 103 "Main-Class: C2");
aoqi@0 104 com.sun.tools.javac.Main.compile(new String[]{"C2.java"});
aoqi@0 105 jarGenerator.run(new String[] {"cfm", "C2.jar", "MANIFEST.MF", "C2.class"});
aoqi@0 106
aoqi@0 107 writeFile("C3.java",
aoqi@0 108 "public class C3 {public static void h() {C2.g(); C1.f();}}");
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 void compileWithJSR199() throws IOException {
aoqi@0 112 String cpath = "C2.jar";
aoqi@0 113 File clientJarFile = new File(cpath);
aoqi@0 114 File sourceFileToCompile = new File("C3.java");
aoqi@0 115
aoqi@0 116
aoqi@0 117 javax.tools.JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
aoqi@0 118 DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
aoqi@0 119 StandardJavaFileManager stdFileManager = javac.getStandardFileManager(diagnostics, null, null);
aoqi@0 120
aoqi@0 121 List<File> files = new ArrayList<>();
aoqi@0 122 files.add(clientJarFile);
aoqi@0 123
aoqi@0 124 stdFileManager.setLocation(StandardLocation.CLASS_PATH, files);
aoqi@0 125
aoqi@0 126 Iterable<? extends JavaFileObject> sourceFiles = stdFileManager.getJavaFileObjects(sourceFileToCompile);
aoqi@0 127
aoqi@0 128 if (!javac.getTask(null, stdFileManager, diagnostics, null, null, sourceFiles).call()) {
aoqi@0 129 throw new AssertionError("compilation failed");
aoqi@0 130 }
aoqi@0 131 }
aoqi@0 132 }

mercurial