Path: blob/master/test/langtools/tools/javac/4241573/T4241573.java
41149 views
/*1* Copyright (c) 2009, 2015, 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 424157326* @summary SourceFile attribute includes full path27* @modules jdk.jdeps/com.sun.tools.classfile28*/2930import com.sun.tools.classfile.Attribute;31import com.sun.tools.classfile.ClassFile;32import com.sun.tools.classfile.SourceFile_attribute;33import java.io.*;34import java.util.*;35import java.util.jar.*;3637public class T4241573 {38public static void main(String... args) throws Exception {39new T4241573().run();40}4142public void run() throws Exception {43// Selection of files to be compiled44File absJar = createJar(new File("abs.jar").getAbsoluteFile(), "j.A");45File relJar = createJar(new File("rel.jar"), "j.R");46File absDir = createDir(new File("abs.dir").getAbsoluteFile(), "d.A");47File relDir = createDir(new File("rel.dir"), "d.R");48File absTestFile = writeFile(new File("AbsTest.java").getAbsoluteFile(), "class AbsTest { class Inner { } }");49File relTestFile = writeFile(new File("RelTest.java"), "class RelTest { class Inner { } }");50File relTest2File = writeFile(new File("p/RelTest2.java"), "package p; class RelTest2 { class Inner { } }");51// This next class references other classes that will be found on the source path52// and which will therefore need to be compiled as well.53File mainFile = writeFile(new File("Main.java"),54"class Main { j.A ja; j.R jr; d.A da; d.R dr; }" +55"");5657String sourcePath = createPath(absJar, relJar, absDir, relDir);58File outDir = new File("classes");59outDir.mkdirs();6061String[] args = {62"-sourcepath", sourcePath,63"-d", outDir.getPath(),64absTestFile.getPath(),65relTestFile.getPath(),66relTest2File.getPath(),67mainFile.getPath(),68};69System.err.println("compile: " + Arrays.asList(args));70StringWriter sw = new StringWriter();71PrintWriter pw = new PrintWriter(sw);72int rc = com.sun.tools.javac.Main.compile(args, pw);73pw.close();74if (rc != 0) {75System.err.println(sw.toString());76throw new Exception("unexpected exit from javac: " + rc);77}7879Set<File> expect = getFiles(outDir,80"d/A.class", "d/A$Inner.class",81"d/R.class", "d/R$Inner.class",82"j/A.class", "j/A$Inner.class",83"j/R.class", "j/R$Inner.class",84"AbsTest.class", "AbsTest$Inner.class",85"RelTest.class", "RelTest$Inner.class",86"p/RelTest2.class", "p/RelTest2$Inner.class",87"Main.class" );8889Set<File> found = findFiles(outDir);9091if (!found.equals(expect)) {92if (found.containsAll(expect))93throw new Exception("unexpected files found: " + diff(found, expect));94else if (expect.containsAll(found))95throw new Exception("expected files not found: " + diff(expect, found));96}9798for (File f: found)99verifySourceFileAttribute(f);100101if (errors > 0)102throw new Exception(errors + " errors occurred");103}104105/** Check the SourceFileAttribute is the simple name of the original source file. */106void verifySourceFileAttribute(File f) {107System.err.println("verify: " + f);108try {109ClassFile cf = ClassFile.read(f);110SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);111String found = sfa.getSourceFile(cf.constant_pool);112String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");113if (!expect.equals(found)) {114error("bad value found: " + found + ", expected: " + expect);115}116} catch (Exception e) {117error("error reading " + f +": " + e);118}119}120121/** Create a directory containing one or more files. */122File createDir(File dir, String... entries) throws Exception {123if (!dir.mkdirs())124throw new Exception("cannot create directories " + dir);125for (String e: entries) {126writeFile(new File(dir, getPathForDirEntry(e)), getBodyForEntry(e));127}128return dir;129}130131/** Create a jar file containing one or more entries. */132File createJar(File jar, String... entries) throws IOException {133OutputStream out = new FileOutputStream(jar);134try {135JarOutputStream jos = new JarOutputStream(out);136for (String e: entries) {137jos.putNextEntry(new JarEntry(getPathForZipEntry(e)));138jos.write(getBodyForEntry(e).getBytes());139}140jos.close();141} finally {142out.close();143}144return jar;145}146147/** Return the path for an entry given to createDir */148String getPathForDirEntry(String e) {149return e.replace(".", File.separator) + ".java";150}151152/** Return the path for an entry given to createJar. */153String getPathForZipEntry(String e) {154return e.replace(".", "/") + ".java";155}156157/** Return the body text for an entry given to createDir or createJar. */158String getBodyForEntry(String e) {159int sep = e.lastIndexOf(".");160String pkgName = e.substring(0, sep);161String className = e.substring(sep + 1);162return "package " + pkgName + "; public class " + className + "{ class Inner { } }";163}164165/** Write a file containing the given string. Parent directories are166* created as needed. */167File writeFile(File f, String s) throws IOException {168if (f.getParentFile() != null)169f.getParentFile().mkdirs();170FileWriter out = new FileWriter(f);171try {172out.write(s);173} finally {174out.close();175}176return f;177}178179/** Create a path value from a list of directories and jar files. */180String createPath(File... files) {181StringBuilder sb = new StringBuilder();182for (File f: files) {183if (sb.length() > 0)184sb.append(File.pathSeparatorChar);185sb.append(f.getPath());186}187return sb.toString();188}189190/** Create a set of files from a base directory and a set of relative paths. */191Set<File> getFiles(File dir, String... paths) {192Set<File> files = new LinkedHashSet<File>();193for (String p: paths)194files.add(new File(dir, p));195return files;196}197198/** Find all the files in a directory and its subdirectories. */199Set<File> findFiles(File dir) {200Set<File> files = new LinkedHashSet<File>();201findFiles(dir, files);202return files;203}204// where205void findFiles(File dir, Set<File> files) {206for (File f: dir.listFiles()) {207if (f.isDirectory())208findFiles(f, files);209else210files.add(f);211}212}213214/** Return the difference of two sets, a - b. */215<T> Set<T> diff(Set<T> a, Set<T> b) {216if (b.isEmpty())217return a;218Set<T> result = new LinkedHashSet<T>(a);219result.removeAll(b);220return result;221}222223/** Report an error. */224void error(String msg) {225System.err.println(msg);226errors++;227}228229int errors;230}231232233