jdk-24/langtools/test/tools/javap/ListTest.java
Jonathan Gibbons 0a7516d744 4075303: Use javap to enquire aboput a specific inner class
4348375: Javap is not internationalized
4459541: "javap -l" shows line numbers as signed short; they should be unsigned
4501660: change diagnostic of -help as 'print this help message and exit'
4776241: unused source file in javap..
4870651: javap should recognize generics, varargs, enum
4876942: javap invoked without args does not print help screen
4880663: javap could output whitespace between class name and opening brace
4975569: javap doesn't print new flag bits
6271787: javap dumps LocalVariableTypeTable attribute in hex, needs to print a table
6305779: javap: support annotations
6439940: Clean up javap implementation
6469569: wrong check of searchpath in JavapEnvironment
6474890: javap does not open .zip files in -classpath
6587786: Javap throws error : "ERROR:Could not find <classname>" for JRE classes
6622215: javap ignores certain relevant access flags
6622216: javap names some attributes incorrectly
6622232: javap gets whitespace confused
6622260: javap prints negative bytes incorrectly in hex

Reviewed-by: ksrini
2008-06-03 13:26:47 -07:00

148 lines
5.0 KiB
Java

/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
import java.io.*;
import java.util.*;
import javax.tools.*;
/*
* @test
* @bug 6439940
* @summary Cleanup javap implementation
* @run main/othervm ListTest
*/
public class ListTest {
public static void main(String[] args) throws Exception {
new ListTest().run();
}
ListTest() {
String v = System.getProperty("view.cmd");
// v = "/opt/teamware/7.7/bin/filemerge -r";
if (v != null) {
viewResults = true;
viewCmd = Arrays.asList(v.split(" +"));
}
}
void run() throws Exception {
StandardLocation[] locs = new StandardLocation[] {
StandardLocation.PLATFORM_CLASS_PATH,
StandardLocation.CLASS_PATH,
};
int count = 0;
int pass = 0;
for (StandardLocation loc: locs) {
for (String testClassName: list(loc)) {
count++;
if (test(testClassName))
pass++;
}
}
if (pass < count)
throw new Error(pass + "/" + count + " test cases passed");
}
Iterable<String> list(StandardLocation loc) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager sfm = compiler.getStandardFileManager(null, null, null);
Set<JavaFileObject.Kind> kinds = Collections.singleton(JavaFileObject.Kind.CLASS);
List<String> list = new ArrayList<String>();
for (JavaFileObject fo: sfm.list(loc, testPackage, kinds, true)) {
//System.err.println(com.sun.tools.javac.util.Old199.getPath(fo));
list.add(sfm.inferBinaryName(loc, fo));
}
return list;
}
boolean test(String testClassName) throws Exception {
String[] args = new String[options.size() + 1];
options.toArray(args);
args[args.length - 1] = testClassName;
String oldOut = runOldJavap(args);
String newOut = runNewJavap(args);
boolean ok = oldOut.equals(newOut);
System.err.println((ok ? "pass" : "FAIL") + ": " + testClassName);
if (!ok && viewResults)
view(oldOut, newOut);
return ok;
}
String runOldJavap(String[] args) {
//System.err.println("OLD: " + Arrays.asList(args));
PrintStream oldOut = System.out;
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
try {
sun.tools.javap.Main.entry(args);
} finally {
System.setOut(oldOut);
}
return out.toString();
}
String runNewJavap(String[] args) {
String[] nArgs = new String[args.length + 2];
nArgs[0] = "-XDcompat";
nArgs[1] = "-XDignore.symbol.file";
System.arraycopy(args, 0, nArgs, 2, args.length);
//System.err.println("NEW: " + Arrays.asList(nArgs));
StringWriter out = new StringWriter();
com.sun.tools.javap.Main.run(nArgs, new PrintWriter(out, true));
return out.toString();
}
File write(String text, String suffix) throws IOException {
File f = File.createTempFile("ListTest", suffix);
FileWriter out = new FileWriter(f);
out.write(text);
out.close();
return f;
}
void view(String oldOut, String newOut) throws Exception {
File oldFile = write(oldOut, "old");
File newFile = write(newOut, "new");
List<String> cmd = new ArrayList<String>();
cmd.addAll(viewCmd);
cmd.add(oldFile.getPath());
cmd.add(newFile.getPath());
Process p = new ProcessBuilder(cmd).redirectErrorStream(true).start();
p.getOutputStream().close();
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = in.readLine()) != null)
System.err.println(line);
in.close();
p.waitFor();
}
String testPackage = "java.lang";
List<String> options = Arrays.asList("-v");
boolean viewResults;
List<String> viewCmd;
}