aoqi@0: /* aoqi@0: * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 4241573 aoqi@0: * @summary SourceFile attribute includes full path aoqi@0: */ aoqi@0: aoqi@0: import com.sun.tools.classfile.Attribute; aoqi@0: import com.sun.tools.classfile.ClassFile; aoqi@0: import com.sun.tools.classfile.SourceFile_attribute; aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: import java.util.jar.*; aoqi@0: aoqi@0: public class T4241573 { aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new T4241573().run(); aoqi@0: } aoqi@0: aoqi@0: public void run() throws Exception { aoqi@0: // Selection of files to be compiled aoqi@0: File absJar = createJar(new File("abs.jar").getAbsoluteFile(), "j.A"); aoqi@0: File relJar = createJar(new File("rel.jar"), "j.R"); aoqi@0: File absDir = createDir(new File("abs.dir").getAbsoluteFile(), "d.A"); aoqi@0: File relDir = createDir(new File("rel.dir"), "d.R"); aoqi@0: File absTestFile = writeFile(new File("AbsTest.java").getAbsoluteFile(), "class AbsTest { class Inner { } }"); aoqi@0: File relTestFile = writeFile(new File("RelTest.java"), "class RelTest { class Inner { } }"); aoqi@0: File relTest2File = writeFile(new File("p/RelTest2.java"), "package p; class RelTest2 { class Inner { } }"); aoqi@0: // This next class references other classes that will be found on the source path aoqi@0: // and which will therefore need to be compiled as well. aoqi@0: File mainFile = writeFile(new File("Main.java"), aoqi@0: "class Main { j.A ja; j.R jr; d.A da; d.R dr; }" + aoqi@0: ""); aoqi@0: aoqi@0: String sourcePath = createPath(absJar, relJar, absDir, relDir); aoqi@0: File outDir = new File("classes"); aoqi@0: outDir.mkdirs(); aoqi@0: aoqi@0: String[] args = { aoqi@0: "-sourcepath", sourcePath, aoqi@0: "-d", outDir.getPath(), aoqi@0: absTestFile.getPath(), aoqi@0: relTestFile.getPath(), aoqi@0: relTest2File.getPath(), aoqi@0: mainFile.getPath(), aoqi@0: }; aoqi@0: System.err.println("compile: " + Arrays.asList(args)); aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter pw = new PrintWriter(sw); aoqi@0: int rc = com.sun.tools.javac.Main.compile(args, pw); aoqi@0: pw.close(); aoqi@0: if (rc != 0) { aoqi@0: System.err.println(sw.toString()); aoqi@0: throw new Exception("unexpected exit from javac: " + rc); aoqi@0: } aoqi@0: aoqi@0: Set expect = getFiles(outDir, aoqi@0: "d/A.class", "d/A$Inner.class", aoqi@0: "d/R.class", "d/R$Inner.class", aoqi@0: "j/A.class", "j/A$Inner.class", aoqi@0: "j/R.class", "j/R$Inner.class", aoqi@0: "AbsTest.class", "AbsTest$Inner.class", aoqi@0: "RelTest.class", "RelTest$Inner.class", aoqi@0: "p/RelTest2.class", "p/RelTest2$Inner.class", aoqi@0: "Main.class" ); aoqi@0: aoqi@0: Set found = findFiles(outDir); aoqi@0: aoqi@0: if (!found.equals(expect)) { aoqi@0: if (found.containsAll(expect)) aoqi@0: throw new Exception("unexpected files found: " + diff(found, expect)); aoqi@0: else if (expect.containsAll(found)) aoqi@0: throw new Exception("expected files not found: " + diff(expect, found)); aoqi@0: } aoqi@0: aoqi@0: for (File f: found) aoqi@0: verifySourceFileAttribute(f); aoqi@0: aoqi@0: if (errors > 0) aoqi@0: throw new Exception(errors + " errors occurred"); aoqi@0: } aoqi@0: aoqi@0: /** Check the SourceFileAttribute is the simple name of the original source file. */ aoqi@0: void verifySourceFileAttribute(File f) { aoqi@0: System.err.println("verify: " + f); aoqi@0: try { aoqi@0: ClassFile cf = ClassFile.read(f); aoqi@0: SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile); aoqi@0: String found = sfa.getSourceFile(cf.constant_pool); aoqi@0: String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java"); aoqi@0: if (!expect.equals(found)) { aoqi@0: error("bad value found: " + found + ", expected: " + expect); aoqi@0: } aoqi@0: } catch (Exception e) { aoqi@0: error("error reading " + f +": " + e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** Create a directory containing one or more files. */ aoqi@0: File createDir(File dir, String... entries) throws Exception { aoqi@0: if (!dir.mkdirs()) aoqi@0: throw new Exception("cannot create directories " + dir); aoqi@0: for (String e: entries) { aoqi@0: writeFile(new File(dir, getPathForDirEntry(e)), getBodyForEntry(e)); aoqi@0: } aoqi@0: return dir; aoqi@0: } aoqi@0: aoqi@0: /** Create a jar file containing one or more entries. */ aoqi@0: File createJar(File jar, String... entries) throws IOException { aoqi@0: OutputStream out = new FileOutputStream(jar); aoqi@0: try { aoqi@0: JarOutputStream jos = new JarOutputStream(out); aoqi@0: for (String e: entries) { aoqi@0: jos.putNextEntry(new JarEntry(getPathForZipEntry(e))); aoqi@0: jos.write(getBodyForEntry(e).getBytes()); aoqi@0: } aoqi@0: jos.close(); aoqi@0: } finally { aoqi@0: out.close(); aoqi@0: } aoqi@0: return jar; aoqi@0: } aoqi@0: aoqi@0: /** Return the path for an entry given to createDir */ aoqi@0: String getPathForDirEntry(String e) { aoqi@0: return e.replace(".", File.separator) + ".java"; aoqi@0: } aoqi@0: aoqi@0: /** Return the path for an entry given to createJar. */ aoqi@0: String getPathForZipEntry(String e) { aoqi@0: return e.replace(".", "/") + ".java"; aoqi@0: } aoqi@0: aoqi@0: /** Return the body text for an entry given to createDir or createJar. */ aoqi@0: String getBodyForEntry(String e) { aoqi@0: int sep = e.lastIndexOf("."); aoqi@0: String pkgName = e.substring(0, sep); aoqi@0: String className = e.substring(sep + 1); aoqi@0: return "package " + pkgName + "; public class " + className + "{ class Inner { } }"; aoqi@0: } aoqi@0: aoqi@0: /** Write a file containing the given string. Parent directories are aoqi@0: * created as needed. */ aoqi@0: File writeFile(File f, String s) throws IOException { aoqi@0: if (f.getParentFile() != null) aoqi@0: f.getParentFile().mkdirs(); aoqi@0: FileWriter out = new FileWriter(f); aoqi@0: try { aoqi@0: out.write(s); aoqi@0: } finally { aoqi@0: out.close(); aoqi@0: } aoqi@0: return f; aoqi@0: } aoqi@0: aoqi@0: /** Create a path value from a list of directories and jar files. */ aoqi@0: String createPath(File... files) { aoqi@0: StringBuilder sb = new StringBuilder(); aoqi@0: for (File f: files) { aoqi@0: if (sb.length() > 0) aoqi@0: sb.append(File.pathSeparatorChar); aoqi@0: sb.append(f.getPath()); aoqi@0: } aoqi@0: return sb.toString(); aoqi@0: } aoqi@0: aoqi@0: /** Create a set of files from a base directory and a set of relative paths. */ aoqi@0: Set getFiles(File dir, String... paths) { aoqi@0: Set files = new LinkedHashSet(); aoqi@0: for (String p: paths) aoqi@0: files.add(new File(dir, p)); aoqi@0: return files; aoqi@0: } aoqi@0: aoqi@0: /** Find all the files in a directory and its subdirectories. */ aoqi@0: Set findFiles(File dir) { aoqi@0: Set files = new LinkedHashSet(); aoqi@0: findFiles(dir, files); aoqi@0: return files; aoqi@0: } aoqi@0: // where aoqi@0: void findFiles(File dir, Set files) { aoqi@0: for (File f: dir.listFiles()) { aoqi@0: if (f.isDirectory()) aoqi@0: findFiles(f, files); aoqi@0: else aoqi@0: files.add(f); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** Return the difference of two sets, a - b. */ aoqi@0: Set diff(Set a, Set b) { aoqi@0: if (b.isEmpty()) aoqi@0: return a; aoqi@0: Set result = new LinkedHashSet(a); aoqi@0: result.removeAll(b); aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: /** Report an error. */ aoqi@0: void error(String msg) { aoqi@0: System.err.println(msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: int errors; aoqi@0: }