Commit f350cc75 authored by ebruneton's avatar ebruneton

moved Dumpable interface and associated code to asm.util.attrs, renamed...

moved Dumpable interface and associated code to asm.util.attrs, renamed Dump*Visitor into ASMifier*Visitor
parent 0482a658
......@@ -3,7 +3,7 @@
<jar jarfile="${out.dist.lib}/asm-util.jar"
basedir="${out.build}"
manifest="${archive}/asm-util.mf">
<include name="org/objectweb/asm/util/*.class"/>
<include name="org/objectweb/asm/util/**/*.class"/>
</jar>
</target>
</project>
......@@ -137,22 +137,6 @@ public class Annotation {
}
}
public void dump (StringBuffer buf, String varName) {
buf.append("Annotation ").append(varName).append(" = new Annotation();\n");
buf.append(varName).append(".type = \"").append(type).append("\";\n");
if (memberValues.size() > 0) {
buf.append("{\n");
for (int i = 0; i < memberValues.size(); i++) {
Object[] values = (Object[])memberValues.get(i);
String val = varName + "val" + i;
((AnnotationMemberValue)values[1]).dump(buf, val);
buf.append(varName).append(".add( \"")
.append(values[0]).append("\", ").append(val).append(");\n");
}
buf.append("}\n");
}
}
/**
* Utility method to read List of annotations. Each element of annotations
* List will have Annotation instance.
......@@ -247,33 +231,6 @@ public class Annotation {
return bv;
}
public static void dumpAnnotations (StringBuffer buf,
String varName, List annotations) {
if (annotations.size() > 0) {
buf.append("{\n");
for (int i = 0; i < annotations.size(); i++) {
String val = varName + "ann" + i;
((Annotation)annotations.get(i)).dump(buf, val);
buf.append(varName).append(".add( ").append(val).append(");\n");
}
buf.append("}\n");
}
}
public static void dumpParameterAnnotations (StringBuffer buf,
String varName,
List parameters) {
if (parameters.size() > 0) {
buf.append("{\n");
for (int i = 0; i < parameters.size(); i++) {
String val = varName + "param" + i;
dumpAnnotations(buf, val, (List)parameters.get(i));
buf.append(varName).append(".add( ").append(val).append(");\n");
}
buf.append("}\n");
}
}
/**
* Returns annotation values in the format described in JSR-175 for Java
* source code.
......
......@@ -36,8 +36,6 @@ import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import java.util.Map;
/**
* The AnnotationDefault attribute is a variable length attribute in the
* attributes table of certain method_info structures, namely those representing
......@@ -77,7 +75,7 @@ import java.util.Map;
* @author Eugene Kuleshov
*/
public class AnnotationDefaultAttribute extends Attribute implements Dumpable {
public class AnnotationDefaultAttribute extends Attribute {
public AnnotationMemberValue defaultValue;
......@@ -98,14 +96,6 @@ public class AnnotationDefaultAttribute extends Attribute implements Dumpable {
return defaultValue.write(new ByteVector(), cw);
}
public void dump (StringBuffer buf, String varName, Map labelNames) {
buf.append("AnnotationDefaultAttribute ").append(varName)
.append(" = new AnnotationDefaultAttribute();\n");
defaultValue.dump(buf, varName + "Val");
buf.append(varName).append(".defaultValue = ")
.append(varName).append("Val;\n");
}
/**
* Returns value in the format described in JSR-175 for Java source code.
*/
......
......@@ -140,25 +140,6 @@ import org.objectweb.asm.Type;
public class AnnotationMemberValue {
private static final String TAGS = "BCDFIJSZsec@[";
private static final Class[] TYPES = new Class[]{
Byte.class,
Character.class,
Double.class,
Float.class,
Integer.class,
Long.class,
Short.class,
Boolean.class,
String.class,
EnumConstValue.class, // should we use java.lang.Enum in the future?
Type.class,
Annotation.class,
AnnotationMemberValue[].class};
// private int tag;
private Object value;
public AnnotationMemberValue () {
......@@ -169,10 +150,46 @@ public class AnnotationMemberValue {
}
public int getTag () {
for (int i = 0; i < TYPES.length; i++) {
if (value.getClass().equals(TYPES[i])) return TAGS.charAt(i);
if (value instanceof Byte) {
return 'B';
}
if (value instanceof Character) {
return 'C';
}
if (value instanceof Double) {
return 'D';
}
if (value instanceof Float) {
return 'F';
}
if (value instanceof Integer) {
return 'I';
}
if (value instanceof Long) {
return 'J';
}
if (value instanceof Short) {
return 'S';
}
if (value instanceof Boolean) {
return 'Z';
}
return -1;
if (value instanceof String) {
return 's';
}
if (value instanceof EnumConstValue) {
return 'e';
}
if (value instanceof Type) {
return 'c';
}
if (value instanceof Annotation) {
return '@';
}
if (value instanceof AnnotationMemberValue[]) {
return '[';
}
return -1;
}
public Object getValue () {
......@@ -248,6 +265,7 @@ public class AnnotationMemberValue {
* @param cw the class to which this attribute must be added. This parameter
* can be used to add to the constant pool of this class the items that
* corresponds to this attribute.
* @return bv.
*/
public ByteVector write (ByteVector bv, ClassWriter cw) {
......@@ -270,7 +288,8 @@ public class AnnotationMemberValue {
break;
case 'e': // enum_const_value
((EnumConstValue)value).write(bv, cw);
bv.putShort(cw.newUTF8(((EnumConstValue)value).typeName));
bv.putShort(cw.newUTF8(((EnumConstValue)value).constName));
break;
case 'c': // class_info
......@@ -292,93 +311,6 @@ public class AnnotationMemberValue {
return bv;
}
public void dump (StringBuffer buf, String valName) {
int tag = getTag();
String objName = valName.concat("obj");
switch (tag) {
case 'B': // pointer to CONSTANT_Byte
buf.append("Object ").append(objName)
.append(" = new Byte(").append(value).append(");\n");
break;
case 'C': // pointer to CONSTANT_Char
buf.append("Object ").append(objName)
.append(" = new Character((char)").append(value).append(");\n");
break;
case 'D': // pointer to CONSTANT_Double
buf.append("Object ").append(objName)
.append(" = new Double((double)").append(value).append(");\n");
break;
case 'F': // pointer to CONSTANT_Float
buf.append("Object ").append(objName)
.append(" = new Float((float)").append(value).append(");\n");
break;
case 'I': // pointer to CONSTANT_Integer
buf.append("Object ").append(objName)
.append(" = new Integer((int)").append(value).append(");\n");
break;
case 'J': // pointer to CONSTANT_Long
buf.append("Object ").append(objName)
.append(" = new Long((long)").append(value).append(");\n");
break;
case 'S': // pointer to CONSTANT_Short
buf.append("Object ").append(objName)
.append(" = new Short((short)").append(value).append(");\n");
break;
case 'Z': // pointer to CONSTANT_Boolean
buf.append("Object ").append(objName)
.append(" = new Boolean(").append(value).append(");\n");
break;
case 's': // pointer to CONSTANT_Utf8
buf.append("Object ").append(objName)
.append(" = \"").append(value).append("\";\n");
break;
case 'e': // enum_const_value
EnumConstValue e = (EnumConstValue)value;
buf.append("Object ").append(objName)
.append(" = new AnnotationMemberValue.EnumConstValue(\"")
.append(e.typeName).append("\", \"").append(e.constName)
.append("\"));\n");
break;
case 'c': // class_info
Type t = (Type)value;
buf.append("Object ").append(objName).
append(" = Type.getType(\"" + t.getDescriptor() + "\");\n");
break;
case '@': // annotation_value
((Annotation)value).dump(buf, objName);
break;
case '[': // array_value
AnnotationMemberValue[] v = (AnnotationMemberValue[])value;
buf.append("AnnotationMemberValue[] ").append(objName)
.append(" = new AnnotationMemberValue[")
.append(v.length).append("]\n;");
buf.append("{\n");
buf.append("Object av = null;\n");
for (int i = 0; i < v.length; i++) {
v[i].dump(buf, objName + i);
buf.append(objName)
.append("[").append(i).append("] = ").append(objName + i);
}
buf.append("};\n");
break;
}
buf.append("AnnotationMemberValue ").append(valName);
buf.append(" = new AnnotationMemberValue( ").append(objName).append(");\n");
}
/**
* Returns value in the format described in JSR-175 for Java source code.
*/
......@@ -444,17 +376,6 @@ public class AnnotationMemberValue {
this.constName = constName;
}
public void write (ByteVector bv, ClassWriter cw) {
// TODO verify the data structures
bv.putShort(cw.newUTF8(typeName));
bv.putShort(cw.newUTF8(constName));
}
// public boolean equals( Object o) {
// EnumConstValue v = ( EnumConstValue) o;
// return v.constName.equals( )
// }
public String toString () {
// TODO verify print enum
return typeName + "." + constName;
......
......@@ -30,8 +30,6 @@
package org.objectweb.asm.attrs;
import java.util.Map;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ByteVector;
import org.objectweb.asm.ClassReader;
......@@ -85,7 +83,7 @@ import org.objectweb.asm.Label;
* @author Eugene Kuleshov
*/
public class EnclosingMethodAttribute extends Attribute implements Dumpable {
public class EnclosingMethodAttribute extends Attribute {
public String owner;
public String name;
......@@ -116,22 +114,13 @@ public class EnclosingMethodAttribute extends Attribute implements Dumpable {
protected ByteVector write (ClassWriter cw, byte[] code,
int len, int maxStack, int maxLocals) {
return new ByteVector().putShort(cw.newClass(owner))
.putShort(cw.newNameType(name, desc));
}
public void dump (StringBuffer buf, String varName, Map labelNames) {
buf.append("EnclosingMethodAttribute ").append(varName)
.append(" = new EnclosingMethodAttribute(\"")
.append(owner).append(",")
.append(name).append(",")
.append(desc).append("\");\n");
.putShort(cw.newNameType(name, desc));
}
public String toString () {
return new StringBuffer("owner:").append( owner)
.append(" name:").append(name)
.append(" desc:").append(desc).toString();
}
}
}
......@@ -32,7 +32,6 @@ package org.objectweb.asm.attrs;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ByteVector;
......@@ -93,7 +92,7 @@ import org.objectweb.asm.Label;
* @author Eugene Kuleshov
*/
public class RuntimeInvisibleAnnotations extends Attribute implements Dumpable {
public class RuntimeInvisibleAnnotations extends Attribute {
public List annotations = new LinkedList();
......@@ -113,12 +112,6 @@ public class RuntimeInvisibleAnnotations extends Attribute implements Dumpable {
return Annotation.writeAnnotations(new ByteVector(), annotations, cw);
}
public void dump (StringBuffer buf, String varName, Map labelNames) {
buf.append("RuntimeInvisibleAnnotations ").append(varName)
.append(" = new RuntimeInvisibleAnnotations();\n");
Annotation.dumpAnnotations(buf, varName, annotations);
}
/**
* Returns value in the format described in JSR-175 for Java source code.
*/
......
......@@ -32,7 +32,6 @@ package org.objectweb.asm.attrs;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ByteVector;
......@@ -112,9 +111,7 @@ import org.objectweb.asm.Label;
* @author Eugene Kuleshov
*/
public class RuntimeInvisibleParameterAnnotations
extends Attribute implements Dumpable
{
public class RuntimeInvisibleParameterAnnotations extends Attribute {
public List parameters = new LinkedList();
......@@ -136,12 +133,6 @@ public class RuntimeInvisibleParameterAnnotations
new ByteVector(), parameters, cw);
}
public void dump (StringBuffer buf, String varName, Map labelNames) {
buf.append("RuntimeInvisibleParameterAnnotations ").append(varName)
.append(" = new RuntimeInvisibleParameterAnnotations();\n");
Annotation.dumpParameterAnnotations(buf, varName, parameters);
}
public String toString () {
return Annotation.stringParameterAnnotations(parameters);
}
......
......@@ -32,7 +32,6 @@ package org.objectweb.asm.attrs;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ByteVector;
......@@ -89,7 +88,7 @@ import org.objectweb.asm.Label;
* @author Eugene Kuleshov
*/
public class RuntimeVisibleAnnotations extends Attribute implements Dumpable {
public class RuntimeVisibleAnnotations extends Attribute {
public List annotations = new LinkedList();
......@@ -109,12 +108,6 @@ public class RuntimeVisibleAnnotations extends Attribute implements Dumpable {
return Annotation.writeAnnotations(new ByteVector(), annotations, cw);
}
public void dump (StringBuffer buf, String varName, Map labelNames) {
buf.append("RuntimeVisibleAnnotations ").append(varName)
.append(" = new RuntimeVisibleAnnotations();\n");
Annotation.dumpAnnotations(buf, varName, annotations);
}
/**
* Returns value in the format described in JSR-175 for Java source code.
*/
......
......@@ -32,7 +32,6 @@ package org.objectweb.asm.attrs;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ByteVector;
......@@ -104,9 +103,7 @@ import org.objectweb.asm.Label;
* @author Eugene Kuleshov
*/
public class RuntimeVisibleParameterAnnotations
extends Attribute implements Dumpable
{
public class RuntimeVisibleParameterAnnotations extends Attribute {
public List parameters = new LinkedList();
......@@ -128,12 +125,6 @@ public class RuntimeVisibleParameterAnnotations
new ByteVector(), parameters, cw);
}
public void dump (StringBuffer buf, String varName, Map labelNames) {
buf.append("RuntimeVisibleParameterAnnotations ").append(varName)
.append(" = new RuntimeVisibleParameterAnnotations();\n");
Annotation.dumpParameterAnnotations(buf, varName, parameters);
}
public String toString () {
return Annotation.stringParameterAnnotations(parameters);
}
......
......@@ -30,8 +30,6 @@
package org.objectweb.asm.attrs;
import java.util.Map;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ByteVector;
import org.objectweb.asm.ClassReader;
......@@ -90,7 +88,7 @@ import org.objectweb.asm.Label;
* @author Eugene Kuleshov
*/
public class SignatureAttribute extends Attribute implements Dumpable {
public class SignatureAttribute extends Attribute {
public String signature;
......@@ -113,12 +111,6 @@ public class SignatureAttribute extends Attribute implements Dumpable {
return new ByteVector().putShort(cw.newUTF8(signature));
}
public void dump (StringBuffer buf, String varName, Map labelNames) {
buf.append("SignatureAttribute ").append(varName)
.append(" = new SignatureAttribute(\"")
.append(signature).append("\");\n");
}
public String toString () {
return signature;
}
......
......@@ -36,8 +36,6 @@ import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import java.util.Map;
/**
* The SourceDebugExtension attribute is an optional attribute defined in JSR-045
* in the attributes table of the ClassFile structure. There can be no more than one
......@@ -74,9 +72,7 @@ import java.util.Map;
* @author Eugene Kuleshov
*/
public class SourceDebugExtensionAttribute
extends Attribute implements Dumpable
{
public class SourceDebugExtensionAttribute extends Attribute {
public String debugExtension;
......@@ -100,12 +96,6 @@ public class SourceDebugExtensionAttribute
return new ByteVector().putByteArray(b, 0, b.length);
}
public void dump (StringBuffer buf, String varName, Map labelNames) {
buf.append("SourceDebugExtensionAttribute ").append(varName)
.append(" = new SourceDebugExtensionAttribute(\"")
.append(debugExtension).append("\");\n");
}
private String readUTF8 (ClassReader cr, int index, int utfLen) {
int endIndex = index + utfLen;
byte[] b = cr.b;
......
......@@ -32,7 +32,6 @@ package org.objectweb.asm.attrs;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ByteVector;
......@@ -140,7 +139,7 @@ import org.objectweb.asm.Label;
* @author Eugene Kuleshov
*/
public class StackMapAttribute extends Attribute implements Dumpable {
public class StackMapAttribute extends Attribute {
static final int MAX_SIZE = 65535;
......@@ -203,20 +202,6 @@ public class StackMapAttribute extends Attribute implements Dumpable {
return (Label[])labels.toArray(new Label[labels.size()]);
}
public void dump (StringBuffer buf, String varName, Map labelNames) {
buf.append("{\n");
buf.append("StackMapAttribute ").append(varName).append("Attr");
buf.append(" = new StackMapAttribute();\n");
if (frames.size() > 0) {
for (int i = 0; i < frames.size(); i++) {
((StackMapFrame)frames.get(i))
.dump(buf, varName + "frame" + i, labelNames);
}
}
buf.append(varName).append(".visitAttribute(").append(varName);
buf.append("Attr);\n}\n");
}
public String toString () {
StringBuffer sb = new StringBuffer("StackMap[");
for (int i = 0; i < frames.size(); i++) {
......
......@@ -33,7 +33,6 @@ package org.objectweb.asm.attrs;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.objectweb.asm.ByteVector;
......@@ -161,62 +160,6 @@ public class StackMapFrame {
}
}
public void dump (StringBuffer buf, String varName, Map labelNames) {
declareLabel(buf, labelNames, label);
buf.append("{ // StackMapFrame.dump()\n");
buf.append("StackMapFrame ").append(varName).append( " = new StackMapFrame();\n");
buf.append(varName).append(".label = ")
.append(labelNames.get(label)).append(";\n");
dumpTypeInfo(buf, varName, labelNames, locals, "locals");
dumpTypeInfo(buf, varName, labelNames, stack, "stack");
buf.append( "cvAttr.frames.add(").append(varName).append(");\n");
buf.append("}\n");
}
private void dumpTypeInfo (StringBuffer buf, String varName,
Map labelNames, List infos, String field) {
if (infos.size() > 0) {
buf.append("{ // StackMapFrame.dumpTypeInfo()\n");
for (int i = 0; i < infos.size(); i++) {
StackMapType typeInfo = (StackMapType)infos.get(i);
String localName = varName + "Info" + i;
int type = typeInfo.getType();
buf.append("StackMapType ").append(localName)
.append(" = StackMapType.getTypeInfo( StackMapType.ITEM_")
.append(StackMapType.ITEM_NAMES[type]).append(");\n");
switch (type) {
case StackMapType.ITEM_Object: //
buf.append(localName).append(".setObject(\"")
.append(typeInfo.getObject()).append("\");\n");
break;
case StackMapType.ITEM_Uninitialized: //
declareLabel(buf, labelNames, typeInfo.getLabel());
buf.append(localName).append(".setLabel(")
.append(labelNames.get(typeInfo.getLabel())).append(");\n");
break;
}
buf.append(varName).append(".").append(field)
.append(".add(").append(localName).append(");\n");
}
buf.append("}\n");
}
}
public static void declareLabel (StringBuffer buf, Map labelNames, Label l) {
String name = (String)labelNames.get(l);
if (name == null) {
name = "l" + labelNames.size();
labelNames.put(l, name);
buf.append("Label ").append(name).append(" = new Label();\n");
}
}
public String toString () {
StringBuffer sb = new StringBuffer("Frame:L");
sb.append(System.identityHashCode(label));
......
......@@ -38,7 +38,7 @@ import org.objectweb.asm.ClassReader;
import org.objectweb.asm.CodeVisitor;
import org.objectweb.asm.Constants;
import org.objectweb.asm.Type;
import org.objectweb.asm.attrs.Dumpable;
import org.objectweb.asm.util.attrs.ASMifiable;
/**
* A {@link PrintClassVisitor PrintClassVisitor} that prints the ASM code that
......@@ -48,7 +48,7 @@ import org.objectweb.asm.attrs.Dumpable;
* <li>write the Java source code equivalent to the bytecode you want to
* generate;</li>
* <li>compile it with <tt>javac</tt>;</li>
* <li>make a {@link DumpClassVisitor DumpClassVisitor} visit this compiled
* <li>make a {@link ASMifierClassVisitor} visit this compiled
* class (see the {@link #main main} method);</li>
* <li>edit the generated source code, if necessary.</li>
* </ul>
......@@ -109,7 +109,7 @@ import org.objectweb.asm.attrs.Dumpable;
* @author Eric Bruneton, Eugene Kuleshov
*/
public class DumpClassVisitor extends PrintClassVisitor {
public class ASMifierClassVisitor extends PrintClassVisitor {
private static final int ACCESS_CLASS = 262144;
private static final int ACCESS_FIELD = 524288;
......@@ -118,7 +118,7 @@ public class DumpClassVisitor extends PrintClassVisitor {
* Prints the ASM source code to generate the given class to the standard
* output.
* <p>
* Usage: DumpClassVisitor
* Usage: ASMifierClassVisitor
* &lt;fully qualified class name or class file name&gt;
*
* @param args the command line arguments.
......@@ -130,7 +130,7 @@ public class DumpClassVisitor extends PrintClassVisitor {
public static void main (final String[] args) throws Exception {
if (args.length == 0) {
System.err.println("Prints the ASM code to generate the given class.");
System.err.println("Usage: DumpClassVisitor " +
System.err.println("Usage: ASMifierClassVisitor " +
"<fully qualified class name or class file name>");
System.exit(-1);
}
......@@ -140,17 +140,17 @@ public class DumpClassVisitor extends PrintClassVisitor {
} else {
cr = new ClassReader(args[0]);
}
cr.accept(new DumpClassVisitor(new PrintWriter(System.out)),
PrintClassVisitor.DEFAULT_ATTRIBUTES, true);
cr.accept(new ASMifierClassVisitor(
new PrintWriter(System.out)), getDefaultAttributes(), true);
}
/**
* Constructs a new {@link DumpClassVisitor DumpClassVisitor} object.
* Constructs a new {@link ASMifierClassVisitor} object.
*