6729471: javap should accept class files on the command line

Reviewed-by: mcimadamore
This commit is contained in:
Jonathan Gibbons 2009-08-05 08:38:18 -07:00
parent ea9763ee24
commit 9e26dc467b
2 changed files with 216 additions and 24 deletions
langtools
src/share/classes/com/sun/tools/javap
test/tools/javap

@ -32,8 +32,10 @@ import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URI;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@ -49,6 +51,8 @@ import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaFileManager;
@ -57,6 +61,9 @@ import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import com.sun.tools.classfile.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
/**
* "Main" class for javap, normally accessed from the command line
@ -607,30 +614,10 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
protected boolean writeClass(ClassWriter classWriter, String className)
throws IOException, ConstantPoolException {
JavaFileObject fo;
if (className.endsWith(".class")) {
if (fileManager instanceof StandardJavaFileManager) {
StandardJavaFileManager sfm = (StandardJavaFileManager) fileManager;
fo = sfm.getJavaFileObjects(className).iterator().next();
} else {
reportError("err.not.standard.file.manager", className);
return false;
}
} else {
fo = getClassFileObject(className);
if (fo == null) {
// see if it is an inner class, by replacing dots to $, starting from the right
String cn = className;
int lastDot;
while (fo == null && (lastDot = cn.lastIndexOf(".")) != -1) {
cn = cn.substring(0, lastDot) + "$" + cn.substring(lastDot + 1);
fo = getClassFileObject(cn);
}
}
if (fo == null) {
reportError("err.class.not.found", className);
return false;
}
JavaFileObject fo = open(className);
if (fo == null) {
reportError("err.class.not.found", className);
return false;
}
ClassFileInfo cfInfo = read(fo);
@ -675,6 +662,102 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask, Messages {
return true;
}
protected JavaFileObject open(String className) throws IOException {
// for compatibility, first see if it is a class name
JavaFileObject fo = getClassFileObject(className);
if (fo != null)
return fo;
// see if it is an inner class, by replacing dots to $, starting from the right
String cn = className;
int lastDot;
while ((lastDot = cn.lastIndexOf(".")) != -1) {
cn = cn.substring(0, lastDot) + "$" + cn.substring(lastDot + 1);
fo = getClassFileObject(cn);
if (fo != null)
return fo;
}
if (!className.endsWith(".class"))
return null;
if (fileManager instanceof StandardJavaFileManager) {
StandardJavaFileManager sfm = (StandardJavaFileManager) fileManager;
fo = sfm.getJavaFileObjects(className).iterator().next();
if (fo != null && fo.getLastModified() != 0) {
return fo;
}
}
// see if it is a URL, and if so, wrap it in just enough of a JavaFileObject
// to suit javap's needs
if (className.matches("^[A-Za-z]+:.*")) {
try {
final URI uri = new URI(className);
final URL url = uri.toURL();
final URLConnection conn = url.openConnection();
return new JavaFileObject() {
public Kind getKind() {
return JavaFileObject.Kind.CLASS;
}
public boolean isNameCompatible(String simpleName, Kind kind) {
throw new UnsupportedOperationException();
}
public NestingKind getNestingKind() {
throw new UnsupportedOperationException();
}
public Modifier getAccessLevel() {
throw new UnsupportedOperationException();
}
public URI toUri() {
return uri;
}
public String getName() {
return url.toString();
}
public InputStream openInputStream() throws IOException {
return conn.getInputStream();
}
public OutputStream openOutputStream() throws IOException {
throw new UnsupportedOperationException();
}
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
throw new UnsupportedOperationException();
}
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
throw new UnsupportedOperationException();
}
public Writer openWriter() throws IOException {
throw new UnsupportedOperationException();
}
public long getLastModified() {
return conn.getLastModified();
}
public boolean delete() {
throw new UnsupportedOperationException();
}
};
} catch (URISyntaxException ignore) {
} catch (IOException ignore) {
}
}
return null;
}
public static class ClassFileInfo {
ClassFileInfo(JavaFileObject fo, ClassFile cf, byte[] digest, int size) {
this.fo = fo;

@ -0,0 +1,109 @@
/*
* Copyright 2009 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.
*/
/*
* @test
* @bug 6729471
* @summary javap does not output inner interfaces of an interface
*/
import java.io.*;
import java.util.*;
public class T6729471
{
public static void main(String... args) {
new T6729471().run();
}
void run() {
// simple class
verify("java.util.Map",
"public abstract boolean containsKey(java.lang.Object)");
// inner class
verify("java.util.Map.Entry",
"public abstract K getKey()");
// file name
verify("../classes/tools/javap/T6729471.class",
"public static void main(java.lang.String...)");
// file url
verify("file:../classes/tools/javap/T6729471.class",
"public static void main(java.lang.String...)");
// jar url: rt.jar
File java_home = new File(System.getProperty("java.home"));
if (java_home.getName().equals("jre"))
java_home = java_home.getParentFile();
File rt_jar = new File(new File(new File(java_home, "jre"), "lib"), "rt.jar");
verify("jar:file:" + rt_jar + "!/java/util/Map.class",
"public abstract boolean containsKey(java.lang.Object)");
// jar url: ct.sym, if it exists
File ct_sym = new File(new File(java_home, "lib"), "ct.sym");
if (ct_sym.exists()) {
verify("jar:file:" + ct_sym + "!/META-INF/sym/rt.jar/java/util/Map.class",
"public abstract boolean containsKey(java.lang.Object)");
} else
System.err.println("warning: ct.sym not found");
if (errors > 0)
throw new Error(errors + " found.");
}
void verify(String className, String... expects) {
String output = javap(className);
for (String expect: expects) {
if (output.indexOf(expect)< 0)
error(expect + " not found");
}
}
void error(String msg) {
System.err.println(msg);
errors++;
}
int errors;
String javap(String className) {
String testClasses = System.getProperty("test.classes", ".");
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
String[] args = { "-classpath", testClasses, className };
int rc = com.sun.tools.javap.Main.run(args, out);
out.close();
String output = sw.toString();
System.out.println("class " + className);
System.out.println(output);
if (rc != 0)
throw new Error("javap failed. rc=" + rc);
if (output.indexOf("Error:") != -1)
throw new Error("javap reported error.");
return output;
}
}