7031005: javap prints "extends java.lang.Object"

Reviewed-by: mcimadamore
This commit is contained in:
Jonathan Gibbons 2011-03-30 18:32:16 -07:00
parent ce0e6ca247
commit cd57e9960f
6 changed files with 336 additions and 18 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. 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
@ -48,6 +48,13 @@ import com.sun.tools.classfile.Signature;
import com.sun.tools.classfile.Signature_attribute;
import com.sun.tools.classfile.SourceFile_attribute;
import com.sun.tools.classfile.Type;
import com.sun.tools.classfile.Type.ArrayType;
import com.sun.tools.classfile.Type.ClassSigType;
import com.sun.tools.classfile.Type.ClassType;
import com.sun.tools.classfile.Type.MethodType;
import com.sun.tools.classfile.Type.SimpleType;
import com.sun.tools.classfile.Type.TypeParamType;
import com.sun.tools.classfile.Type.WildcardType;
import static com.sun.tools.classfile.AccessFlags.*;
@ -166,8 +173,10 @@ public class ClassWriter extends BasicWriter {
// use info from class file header
if (classFile.isClass() && classFile.super_class != 0 ) {
String sn = getJavaSuperclassName(cf);
print(" extends ");
print(sn);
if (!sn.equals("java.lang.Object")) {
print(" extends ");
print(sn);
}
}
for (int i = 0; i < classFile.interfaces.length; i++) {
print(i == 0 ? (classFile.isClass() ? " implements " : " extends ") : ",");
@ -176,13 +185,14 @@ public class ClassWriter extends BasicWriter {
} else {
try {
Type t = sigAttr.getParsedSignature().getType(constant_pool);
JavaTypePrinter p = new JavaTypePrinter(classFile.isInterface());
// The signature parser cannot disambiguate between a
// FieldType and a ClassSignatureType that only contains a superclass type.
if (t instanceof Type.ClassSigType)
print(getJavaName(t.toString()));
else {
if (t instanceof Type.ClassSigType) {
print(p.print(t));
} else if (options.verbose || !t.isObject()) {
print(" extends ");
print(getJavaName(t.toString()));
print(p.print(t));
}
} catch (ConstantPoolException e) {
print(report(e));
@ -210,6 +220,124 @@ public class ClassWriter extends BasicWriter {
indent(-1);
println("}");
}
// where
class JavaTypePrinter implements Type.Visitor<StringBuilder,StringBuilder> {
boolean isInterface;
JavaTypePrinter(boolean isInterface) {
this.isInterface = isInterface;
}
String print(Type t) {
return t.accept(this, new StringBuilder()).toString();
}
public StringBuilder visitSimpleType(SimpleType type, StringBuilder sb) {
sb.append(getJavaName(type.name));
return sb;
}
public StringBuilder visitArrayType(ArrayType type, StringBuilder sb) {
append(sb, type.elemType);
sb.append("[]");
return sb;
}
public StringBuilder visitMethodType(MethodType type, StringBuilder sb) {
appendIfNotEmpty(sb, "<", type.typeParamTypes, "> ");
append(sb, type.returnType);
append(sb, " (", type.paramTypes, ")");
appendIfNotEmpty(sb, " throws ", type.throwsTypes, "");
return sb;
}
public StringBuilder visitClassSigType(ClassSigType type, StringBuilder sb) {
appendIfNotEmpty(sb, "<", type.typeParamTypes, ">");
if (isInterface) {
appendIfNotEmpty(sb, " extends ", type.superinterfaceTypes, "");
} else {
if (type.superclassType != null
&& (options.verbose || !type.superclassType.isObject())) {
sb.append(" extends ");
append(sb, type.superclassType);
}
appendIfNotEmpty(sb, " implements ", type.superinterfaceTypes, "");
}
return sb;
}
public StringBuilder visitClassType(ClassType type, StringBuilder sb) {
if (type.outerType != null) {
append(sb, type.outerType);
sb.append(".");
}
sb.append(getJavaName(type.name));
appendIfNotEmpty(sb, "<", type.typeArgs, ">");
return sb;
}
public StringBuilder visitTypeParamType(TypeParamType type, StringBuilder sb) {
sb.append(type.name);
String sep = " extends ";
if (type.classBound != null
&& (options.verbose || !type.classBound.isObject())) {
sb.append(sep);
append(sb, type.classBound);
sep = " & ";
}
if (type.interfaceBounds != null) {
for (Type bound: type.interfaceBounds) {
sb.append(sep);
append(sb, bound);
sep = " & ";
}
}
return sb;
}
public StringBuilder visitWildcardType(WildcardType type, StringBuilder sb) {
switch (type.kind) {
case UNBOUNDED:
sb.append("?");
break;
case EXTENDS:
sb.append("? extends ");
append(sb, type.boundType);
break;
case SUPER:
sb.append("? super ");
append(sb, type.boundType);
break;
default:
throw new AssertionError();
}
return sb;
}
private void append(StringBuilder sb, Type t) {
t.accept(this, sb);
}
private void append(StringBuilder sb, String prefix, List<? extends Type> list, String suffix) {
sb.append(prefix);
String sep = "";
for (Type t: list) {
sb.append(sep);
append(sb, t);
sep = ", ";
}
sb.append(suffix);
}
private void appendIfNotEmpty(StringBuilder sb, String prefix, List<? extends Type> list, String suffix) {
if (!isEmpty(list))
append(sb, prefix, list, suffix);
}
private boolean isEmpty(List<? extends Type> list) {
return (list == null || list.isEmpty());
}
}
protected void writeFields() {
for (Field f: classFile.fields) {
@ -298,7 +426,7 @@ public class ClassWriter extends BasicWriter {
try {
methodType = (Type.MethodType) methodSig.getType(constant_pool);
methodExceptions = methodType.throwsTypes;
if (methodExceptions != null && methodExceptions.size() == 0)
if (methodExceptions != null && methodExceptions.isEmpty())
methodExceptions = null;
} catch (ConstantPoolException e) {
// report error?