/** Class generation object representing the whole classfile */
public class ClassFile extends Type.Class.Body {
private final Type.Class thisType;
- private final Type.Class superType;
- private final Type.Class[] interfaces;
+ final Type.Class superType;
+ final Type.Class[] interfaces;
private final short minor;
private final short major;
- final int flags;
private final Vector fields = new Vector();
- public final Vector methods = new Vector();
-
- private final AttrGen attributes;
+ private final Vector methods = new Vector();
+
+ public Type.Class.Method.Body[] methods() {
+ Type.Class.Method.Body[] ret = new Type.Class.Method.Body[methods.size()];
+ methods.copyInto(ret);
+ return ret;
+ }
+ public Type.Class.Field.Body[] fields() {
+ Type.Class.Field.Body[] ret = new Type.Class.Field.Body[fields.size()];
+ fields.copyInto(ret);
+ return ret;
+ }
+
static String flagsToString(int flags, boolean isClass) {
StringBuffer sb = new StringBuffer(32);
if ((flags & PUBLIC) != 0) sb.append("public ");
public Type.Class getType() { return thisType; }
public int getFlags() { return flags; }
- String debugToString() { return debugToString(new StringBuffer(4096)).toString(); }
- StringBuffer debugToString(StringBuffer sb) {
+ public String toString() { return toString(new StringBuffer(4096)).toString(); }
+ StringBuffer toString(StringBuffer sb) {
sb.append(flagsToString(flags,true));
sb.append((flags & INTERFACE) != 0 ? "interface " : "class ");
- sb.append(thisType.debugToString());
- if (superType != null) sb.append(" extends " + superType.debugToString());
+ sb.append(thisType.toString());
+ if (superType != null) sb.append(" extends " + superType.toString());
if (interfaces != null && interfaces.length > 0) sb.append(" implements");
- for(int i=0; i<interfaces.length; i++) sb.append((i==0?" ":", ")+interfaces[i].debugToString());
+ for(int i=0; i<interfaces.length; i++) sb.append((i==0?" ":", ")+interfaces[i].toString());
sb.append(" {");
sb.append(" // v"+major+"."+minor);
- ConstantPool.Utf8Key sourceFile = (ConstantPool.Utf8Key) attributes.get("SourceFile");
+ ConstantPool.Utf8Key sourceFile = (ConstantPool.Utf8Key) attrs.get("SourceFile");
if (sourceFile != null) sb.append(" from " + sourceFile.s);
sb.append("\n");
for(int i=0; i<fields.size(); i++) {
sb.append(" ");
- ((FieldGen)fields.elementAt(i)).debugToString(sb);
+ ((FieldGen)fields.elementAt(i)).toString(sb);
sb.append("\n");
}
for(int i=0; i<methods.size(); i++) {
- ((MethodGen)methods.elementAt(i)).debugToString(sb,thisType.getShortName());
+ ((MethodGen)methods.elementAt(i)).toString(sb,thisType.getShortName());
sb.append("\n");
}
sb.append("}");
public ClassFile(Type.Class thisType, Type.Class superType, int flags) { this(thisType, superType, flags, null); }
public ClassFile(Type.Class thisType, Type.Class superType, int flags, Type.Class[] interfaces) {
- thisType.super();
+ thisType.super(flags, new AttrGen());
this.thisType = thisType;
- if((flags & ~(PUBLIC|FINAL|SUPER|INTERFACE|ABSTRACT)) != 0)
- throw new IllegalArgumentException("invalid flags");
this.superType = superType;
this.interfaces = interfaces;
- this.flags = flags;
this.minor = 3;
this.major = 45;
- this.attributes = new AttrGen();
}
/** Adds a new method to this class
@see FieldGen
@see CGConst
*/
- public final FieldGen addField(String name, Type type, int flags) {
- FieldGen fg = new FieldGen(getType().field(name, type), flags);
+ public final Type.Class.Field.Body addField(Type.Class.Field field, int flags) {
+ FieldGen fg = new FieldGen(field, flags);
fields.addElement(fg);
return fg;
}
/** Sets the source value of the SourceFile attribute of this class
@param sourceFile The string to be uses as the SourceFile of this class
*/
- public void setSourceFile(String sourceFile) { attributes.put("SourceFile", new ConstantPool.Utf8Key(sourceFile)); }
+ public void setSourceFile(String sourceFile) { attrs.put("SourceFile", new ConstantPool.Utf8Key(sourceFile)); }
/** Writes the classfile data to the file specifed
@see ClassFile#dump(OutputStream)
if(interfaces != null) for(int i=0;i<interfaces.length;i++) cp.add(interfaces[i]);
for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).finish(cp);
for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).finish(cp);
- attributes.finish(cp);
+ attrs.finish(cp);
cp.optimize();
cp.seal();
o.writeShort(methods.size()); // methods_count
for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).dump(o,cp); // methods
- attributes.dump(o,cp); // attributes
+ attrs.dump(o,cp); // attributes
}
public static ClassFile read(String s) throws IOException { return read(new File(s)); }
}
private ClassFile(int magic, short minor, short major, ConstantPool cp, short flags,
Type.Class thisType, DataInput i, boolean ssa) throws IOException {
- thisType.super();
+ thisType.super(flags, null);
if (magic != 0xcafebabe) throw new ClassReadExn("invalid magic: " + Long.toString(0xffffffffL & magic, 16));
this.minor = minor;
this.major = major;
- this.flags = flags;
- if((flags & ~(PUBLIC|FINAL|SUPER|INTERFACE|ABSTRACT)) != 0)
- throw new ClassReadExn("invalid flags: " + Integer.toString(flags,16));
this.thisType = thisType;
superType = (Type.Class) cp.getKeyByIndex(i.readShort());
interfaces = new Type.Class[i.readShort()];
for(int j=0; j<interfaces.length; j++) interfaces[j] = (Type.Class) cp.getKeyByIndex(i.readShort());
int numFields = i.readShort();
- for(int j=0; j<numFields; j++) fields.addElement(new FieldGen(this, i, cp));
+ for(int j=0; j<numFields; j++) fields.addElement(new FieldGen(this.getType(), i, cp));
int numMethods = i.readShort();
for(int j=0; j<numMethods; j++) methods.addElement(ssa
? new JSSA(this.getType(), i, cp)
: new MethodGen(this.getType(), i, cp));
- attributes = new AttrGen(i, cp);
+ readAttributes(i, cp);
// FEATURE: Support these
// NOTE: Until we can support them properly we HAVE to delete them,
// they'll be incorrect after we rewrite the constant pool, etc
- attributes.remove("InnerClasses");
+ attrs.remove("InnerClasses");
}
/** Thrown when class generation fails for a reason not under the control of the user
}
else if (args.length==1) {
if (args[0].endsWith(".class")) {
- System.out.println(new ClassFile(new DataInputStream(new FileInputStream(args[0]))).debugToString());
+ System.out.println(new ClassFile(new DataInputStream(new FileInputStream(args[0]))).toString());
} else {
InputStream is = Class.forName(args[0]).getClassLoader().getResourceAsStream(args[0].replace('.', '/')+".class");
- System.out.println(new ClassFile(new DataInputStream(is)).debugToString());
+ System.out.println(new ClassFile(new DataInputStream(is)).toString());
}
} else {
/*