test/tools/javac/4241573/T4241573.java

Fri, 18 Feb 2011 08:12:06 -0800

author
ksrini
date
Fri, 18 Feb 2011 08:12:06 -0800
changeset 882
3d45cc94ee0f
parent 554
9d9f26857129
child 1013
8eb952f43b11
permissions
-rw-r--r--

7018859: javac turn off the Zip optimization by default
Reviewed-by: jjg

jjg@415 1 /*
ksrini@882 2 * Copyright (c) 2009, 2011 Oracle and/or its affiliates. All rights reserved.
jjg@415 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@415 4 *
jjg@415 5 * This code is free software; you can redistribute it and/or modify it
jjg@415 6 * under the terms of the GNU General Public License version 2 only, as
jjg@415 7 * published by the Free Software Foundation.
jjg@415 8 *
jjg@415 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@415 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@415 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@415 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@415 13 * accompanied this code).
jjg@415 14 *
jjg@415 15 * You should have received a copy of the GNU General Public License version
jjg@415 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@415 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@415 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
jjg@415 22 */
jjg@415 23
jjg@415 24 /*
jjg@415 25 * @test
jjg@415 26 * @bug 4241573
jjg@415 27 * @summary SourceFile attribute includes full path
jjg@415 28 */
jjg@415 29
jjg@415 30 import com.sun.tools.classfile.Attribute;
jjg@415 31 import com.sun.tools.classfile.ClassFile;
jjg@415 32 import com.sun.tools.classfile.SourceFile_attribute;
jjg@415 33 import java.io.*;
jjg@415 34 import java.util.*;
jjg@415 35 import java.util.jar.*;
jjg@415 36
jjg@415 37 public class T4241573 {
jjg@415 38 public static void main(String... args) throws Exception {
jjg@415 39 new T4241573().run();
jjg@415 40 }
jjg@415 41
jjg@415 42 public void run() throws Exception {
jjg@415 43 // Selection of files to be compiled
jjg@415 44 File absJar = createJar(new File("abs.jar").getAbsoluteFile(), "j.A");
jjg@415 45 File relJar = createJar(new File("rel.jar"), "j.R");
jjg@415 46 File absDir = createDir(new File("abs.dir").getAbsoluteFile(), "d.A");
jjg@415 47 File relDir = createDir(new File("rel.dir"), "d.R");
jjg@415 48 File absTestFile = writeFile(new File("AbsTest.java").getAbsoluteFile(), "class AbsTest { class Inner { } }");
jjg@415 49 File relTestFile = writeFile(new File("RelTest.java"), "class RelTest { class Inner { } }");
jjg@415 50 File relTest2File = writeFile(new File("p/RelTest2.java"), "package p; class RelTest2 { class Inner { } }");
jjg@415 51 // This next class references other classes that will be found on the source path
jjg@415 52 // and which will therefore need to be compiled as well.
jjg@415 53 File mainFile = writeFile(new File("Main.java"),
jjg@415 54 "class Main { j.A ja; j.R jr; d.A da; d.R dr; }" +
jjg@415 55 "");
jjg@415 56
jjg@415 57 String sourcePath = createPath(absJar, relJar, absDir, relDir);
jjg@415 58 File outDir = new File("classes");
jjg@415 59 outDir.mkdirs();
jjg@415 60
jjg@415 61 String[] args = {
jjg@415 62 "-sourcepath", sourcePath,
jjg@415 63 "-d", outDir.getPath(),
jjg@415 64 absTestFile.getPath(),
jjg@415 65 relTestFile.getPath(),
jjg@415 66 relTest2File.getPath(),
jjg@415 67 mainFile.getPath(),
jjg@415 68 };
jjg@415 69 System.err.println("compile: " + Arrays.asList(args));
jjg@415 70 StringWriter sw = new StringWriter();
jjg@415 71 PrintWriter pw = new PrintWriter(sw);
jjg@415 72 int rc = com.sun.tools.javac.Main.compile(args, pw);
jjg@415 73 pw.close();
jjg@415 74 if (rc != 0) {
jjg@415 75 System.err.println(sw.toString());
jjg@415 76 throw new Exception("unexpected exit from javac: " + rc);
jjg@415 77 }
jjg@415 78
jjg@415 79 Set<File> expect = getFiles(outDir,
jjg@415 80 "d/A.class", "d/A$Inner.class",
jjg@415 81 "d/R.class", "d/R$Inner.class",
jjg@415 82 "j/A.class", "j/A$Inner.class",
jjg@415 83 "j/R.class", "j/R$Inner.class",
jjg@415 84 "AbsTest.class", "AbsTest$Inner.class",
jjg@415 85 "RelTest.class", "RelTest$Inner.class",
jjg@415 86 "p/RelTest2.class", "p/RelTest2$Inner.class",
jjg@415 87 "Main.class" );
jjg@415 88
jjg@415 89 Set<File> found = findFiles(outDir);
jjg@415 90
jjg@415 91 if (!found.equals(expect)) {
jjg@415 92 if (found.containsAll(expect))
jjg@415 93 throw new Exception("unexpected files found: " + diff(found, expect));
jjg@415 94 else if (expect.containsAll(found))
jjg@415 95 throw new Exception("expected files not found: " + diff(expect, found));
jjg@415 96 }
jjg@415 97
jjg@415 98 for (File f: found)
jjg@415 99 verifySourceFileAttribute(f);
jjg@415 100
jjg@415 101 if (errors > 0)
jjg@415 102 throw new Exception(errors + " errors occurred");
jjg@415 103 }
jjg@415 104
jjg@415 105 /** Check the SourceFileAttribute is the simple name of the original source file. */
jjg@415 106 void verifySourceFileAttribute(File f) {
jjg@415 107 System.err.println("verify: " + f);
jjg@415 108 try {
jjg@415 109 ClassFile cf = ClassFile.read(f);
jjg@415 110 SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);
jjg@415 111 String found = sfa.getSourceFile(cf.constant_pool);
jjg@415 112 String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");
jjg@415 113 if (!expect.equals(found)) {
jjg@415 114 error("bad value found: " + found + ", expected: " + expect);
jjg@415 115 }
jjg@415 116 } catch (Exception e) {
jjg@415 117 error("error reading " + f +": " + e);
jjg@415 118 }
jjg@415 119 }
jjg@415 120
jjg@415 121 /** Create a directory containing one or more files. */
jjg@415 122 File createDir(File dir, String... entries) throws Exception {
jjg@415 123 if (!dir.mkdirs())
jjg@415 124 throw new Exception("cannot create directories " + dir);
jjg@415 125 for (String e: entries) {
ksrini@882 126 writeFile(new File(dir, getPathForDirEntry(e)), getBodyForEntry(e));
jjg@415 127 }
jjg@415 128 return dir;
jjg@415 129 }
jjg@415 130
jjg@415 131 /** Create a jar file containing one or more entries. */
jjg@415 132 File createJar(File jar, String... entries) throws IOException {
jjg@415 133 OutputStream out = new FileOutputStream(jar);
jjg@415 134 try {
jjg@415 135 JarOutputStream jos = new JarOutputStream(out);
jjg@415 136 for (String e: entries) {
ksrini@882 137 jos.putNextEntry(new JarEntry(getPathForZipEntry(e)));
jjg@415 138 jos.write(getBodyForEntry(e).getBytes());
jjg@415 139 }
jjg@415 140 jos.close();
jjg@415 141 } finally {
jjg@415 142 out.close();
jjg@415 143 }
jjg@415 144 return jar;
jjg@415 145 }
jjg@415 146
ksrini@882 147 /** Return the path for an entry given to createDir */
ksrini@882 148 String getPathForDirEntry(String e) {
jjg@415 149 return e.replace(".", File.separator) + ".java";
jjg@415 150 }
jjg@415 151
ksrini@882 152 /** Return the path for an entry given to createJar. */
ksrini@882 153 String getPathForZipEntry(String e) {
ksrini@882 154 return e.replace(".", "/") + ".java";
ksrini@882 155 }
ksrini@882 156
jjg@415 157 /** Return the body text for an entry given to createDir or createJar. */
jjg@415 158 String getBodyForEntry(String e) {
jjg@415 159 int sep = e.lastIndexOf(".");
jjg@415 160 String pkgName = e.substring(0, sep);
jjg@415 161 String className = e.substring(sep + 1);
jjg@415 162 return "package " + pkgName + "; public class " + className + "{ class Inner { } }";
jjg@415 163 }
jjg@415 164
jjg@415 165 /** Write a file containing the given string. Parent directories are
jjg@415 166 * created as needed. */
jjg@415 167 File writeFile(File f, String s) throws IOException {
jjg@415 168 if (f.getParentFile() != null)
jjg@415 169 f.getParentFile().mkdirs();
jjg@415 170 FileWriter out = new FileWriter(f);
jjg@415 171 try {
jjg@415 172 out.write(s);
jjg@415 173 } finally {
jjg@415 174 out.close();
jjg@415 175 }
jjg@415 176 return f;
jjg@415 177 }
jjg@415 178
jjg@415 179 /** Create a path value from a list of directories and jar files. */
jjg@415 180 String createPath(File... files) {
jjg@415 181 StringBuilder sb = new StringBuilder();
jjg@415 182 for (File f: files) {
jjg@415 183 if (sb.length() > 0)
jjg@415 184 sb.append(File.pathSeparatorChar);
jjg@415 185 sb.append(f.getPath());
jjg@415 186 }
jjg@415 187 return sb.toString();
jjg@415 188 }
jjg@415 189
jjg@415 190 /** Create a set of files from a base directory and a set of relative paths. */
jjg@415 191 Set<File> getFiles(File dir, String... paths) {
jjg@415 192 Set<File> files = new LinkedHashSet<File>();
jjg@415 193 for (String p: paths)
jjg@415 194 files.add(new File(dir, p));
jjg@415 195 return files;
jjg@415 196 }
jjg@415 197
jjg@415 198 /** Find all the files in a directory and its subdirectories. */
jjg@415 199 Set<File> findFiles(File dir) {
jjg@415 200 Set<File> files = new LinkedHashSet<File>();
jjg@415 201 findFiles(dir, files);
jjg@415 202 return files;
jjg@415 203 }
jjg@415 204 // where
jjg@415 205 void findFiles(File dir, Set<File> files) {
jjg@415 206 for (File f: dir.listFiles()) {
jjg@415 207 if (f.isDirectory())
jjg@415 208 findFiles(f, files);
jjg@415 209 else
jjg@415 210 files.add(f);
jjg@415 211 }
jjg@415 212 }
jjg@415 213
jjg@415 214 /** Return the difference of two sets, a - b. */
jjg@415 215 <T> Set<T> diff(Set<T> a, Set<T> b) {
jjg@415 216 if (b.isEmpty())
jjg@415 217 return a;
jjg@415 218 Set<T> result = new LinkedHashSet<T>(a);
jjg@415 219 result.removeAll(b);
jjg@415 220 return result;
jjg@415 221 }
jjg@415 222
jjg@415 223 /** Report an error. */
jjg@415 224 void error(String msg) {
jjg@415 225 System.err.println(msg);
jjg@415 226 errors++;
jjg@415 227 }
jjg@415 228
jjg@415 229 int errors;
jjg@415 230 }

mercurial