test/tools/javac/4241573/T4241573.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial