Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/4241573/T4241573.java
38813 views
/*1* Copyright (c) 2009, 2011, 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*/2829import com.sun.tools.classfile.Attribute;30import com.sun.tools.classfile.ClassFile;31import com.sun.tools.classfile.SourceFile_attribute;32import java.io.*;33import java.util.*;34import java.util.jar.*;3536public class T4241573 {37public static void main(String... args) throws Exception {38new T4241573().run();39}4041public void run() throws Exception {42// Selection of files to be compiled43File absJar = createJar(new File("abs.jar").getAbsoluteFile(), "j.A");44File relJar = createJar(new File("rel.jar"), "j.R");45File absDir = createDir(new File("abs.dir").getAbsoluteFile(), "d.A");46File relDir = createDir(new File("rel.dir"), "d.R");47File absTestFile = writeFile(new File("AbsTest.java").getAbsoluteFile(), "class AbsTest { class Inner { } }");48File relTestFile = writeFile(new File("RelTest.java"), "class RelTest { class Inner { } }");49File relTest2File = writeFile(new File("p/RelTest2.java"), "package p; class RelTest2 { class Inner { } }");50// This next class references other classes that will be found on the source path51// and which will therefore need to be compiled as well.52File mainFile = writeFile(new File("Main.java"),53"class Main { j.A ja; j.R jr; d.A da; d.R dr; }" +54"");5556String sourcePath = createPath(absJar, relJar, absDir, relDir);57File outDir = new File("classes");58outDir.mkdirs();5960String[] args = {61"-sourcepath", sourcePath,62"-d", outDir.getPath(),63absTestFile.getPath(),64relTestFile.getPath(),65relTest2File.getPath(),66mainFile.getPath(),67};68System.err.println("compile: " + Arrays.asList(args));69StringWriter sw = new StringWriter();70PrintWriter pw = new PrintWriter(sw);71int rc = com.sun.tools.javac.Main.compile(args, pw);72pw.close();73if (rc != 0) {74System.err.println(sw.toString());75throw new Exception("unexpected exit from javac: " + rc);76}7778Set<File> expect = getFiles(outDir,79"d/A.class", "d/A$Inner.class",80"d/R.class", "d/R$Inner.class",81"j/A.class", "j/A$Inner.class",82"j/R.class", "j/R$Inner.class",83"AbsTest.class", "AbsTest$Inner.class",84"RelTest.class", "RelTest$Inner.class",85"p/RelTest2.class", "p/RelTest2$Inner.class",86"Main.class" );8788Set<File> found = findFiles(outDir);8990if (!found.equals(expect)) {91if (found.containsAll(expect))92throw new Exception("unexpected files found: " + diff(found, expect));93else if (expect.containsAll(found))94throw new Exception("expected files not found: " + diff(expect, found));95}9697for (File f: found)98verifySourceFileAttribute(f);99100if (errors > 0)101throw new Exception(errors + " errors occurred");102}103104/** Check the SourceFileAttribute is the simple name of the original source file. */105void verifySourceFileAttribute(File f) {106System.err.println("verify: " + f);107try {108ClassFile cf = ClassFile.read(f);109SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);110String found = sfa.getSourceFile(cf.constant_pool);111String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");112if (!expect.equals(found)) {113error("bad value found: " + found + ", expected: " + expect);114}115} catch (Exception e) {116error("error reading " + f +": " + e);117}118}119120/** Create a directory containing one or more files. */121File createDir(File dir, String... entries) throws Exception {122if (!dir.mkdirs())123throw new Exception("cannot create directories " + dir);124for (String e: entries) {125writeFile(new File(dir, getPathForDirEntry(e)), getBodyForEntry(e));126}127return dir;128}129130/** Create a jar file containing one or more entries. */131File createJar(File jar, String... entries) throws IOException {132OutputStream out = new FileOutputStream(jar);133try {134JarOutputStream jos = new JarOutputStream(out);135for (String e: entries) {136jos.putNextEntry(new JarEntry(getPathForZipEntry(e)));137jos.write(getBodyForEntry(e).getBytes());138}139jos.close();140} finally {141out.close();142}143return jar;144}145146/** Return the path for an entry given to createDir */147String getPathForDirEntry(String e) {148return e.replace(".", File.separator) + ".java";149}150151/** Return the path for an entry given to createJar. */152String getPathForZipEntry(String e) {153return e.replace(".", "/") + ".java";154}155156/** Return the body text for an entry given to createDir or createJar. */157String getBodyForEntry(String e) {158int sep = e.lastIndexOf(".");159String pkgName = e.substring(0, sep);160String className = e.substring(sep + 1);161return "package " + pkgName + "; public class " + className + "{ class Inner { } }";162}163164/** Write a file containing the given string. Parent directories are165* created as needed. */166File writeFile(File f, String s) throws IOException {167if (f.getParentFile() != null)168f.getParentFile().mkdirs();169FileWriter out = new FileWriter(f);170try {171out.write(s);172} finally {173out.close();174}175return f;176}177178/** Create a path value from a list of directories and jar files. */179String createPath(File... files) {180StringBuilder sb = new StringBuilder();181for (File f: files) {182if (sb.length() > 0)183sb.append(File.pathSeparatorChar);184sb.append(f.getPath());185}186return sb.toString();187}188189/** Create a set of files from a base directory and a set of relative paths. */190Set<File> getFiles(File dir, String... paths) {191Set<File> files = new LinkedHashSet<File>();192for (String p: paths)193files.add(new File(dir, p));194return files;195}196197/** Find all the files in a directory and its subdirectories. */198Set<File> findFiles(File dir) {199Set<File> files = new LinkedHashSet<File>();200findFiles(dir, files);201return files;202}203// where204void findFiles(File dir, Set<File> files) {205for (File f: dir.listFiles()) {206if (f.isDirectory())207findFiles(f, files);208else209files.add(f);210}211}212213/** Return the difference of two sets, a - b. */214<T> Set<T> diff(Set<T> a, Set<T> b) {215if (b.isEmpty())216return a;217Set<T> result = new LinkedHashSet<T>(a);218result.removeAll(b);219return result;220}221222/** Report an error. */223void error(String msg) {224System.err.println(msg);225errors++;226}227228int errors;229}230231232