X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Forg%2Fibex%2Fclassgen%2FFieldGen.java;h=3640834f87aa20f5e9eb05aafb418da46a800c02;hb=0b535cb6b45f0d8a1d43eee75a3fe187ed383ef8;hp=b65cb09d39c7ca8ef8bd873210760791311ded2a;hpb=81fd22a1bc8c3117a5279da19fd3443976d03095;p=org.ibex.classgen.git diff --git a/src/org/ibex/classgen/FieldGen.java b/src/org/ibex/classgen/FieldGen.java index b65cb09..3640834 100644 --- a/src/org/ibex/classgen/FieldGen.java +++ b/src/org/ibex/classgen/FieldGen.java @@ -4,59 +4,47 @@ import java.io.*; /** Class representing a field in a generated classfile @see ClassFile#addField */ -public class FieldGen implements CGConst { - private final String name; - private final Type type; - private final int flags; - private final ClassFile.AttrGen attrs; +public class FieldGen extends Type.Class.Field.Body { StringBuffer debugToString(StringBuffer sb) { - sb.append(ClassFile.flagsToString(flags,false)); - sb.append(type.debugToString()); + sb.append(ClassFile.flagsToString(flags, false)); + sb.append(getField().getType().debugToString()); sb.append(" "); - sb.append(name); - if(attrs.contains("ConstantValue")) - sb.append(" = \"").append(attrs.get("ConstantValue")).append("\""); + sb.append(getField().getName()); + if (attrs.contains("ConstantValue")) sb.append(" = \"").append(attrs.get("ConstantValue")).append("\""); sb.append(";"); return sb; } - FieldGen(DataInput in, ConstantPool cp) throws IOException { - flags = in.readShort(); - if((flags & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_VOLATILE|ACC_TRANSIENT|ACC_STATIC|ACC_FINAL)) != 0) - throw new ClassFile.ClassReadExn("invalid flags"); - name = cp.getUtf8KeyByIndex(in.readShort()); - type = Type.instance(cp.getUtf8KeyByIndex(in.readShort())); - attrs = new ClassFile.AttrGen(in,cp); + FieldGen(Type.Class c, DataInput in, ConstantPool cp) throws IOException { + this(in.readShort(), + c.field(cp.getUtf8KeyByIndex(in.readShort()), + Type.fromDescriptor(cp.getUtf8KeyByIndex(in.readShort()))), + new ClassFile.AttrGen(in, cp)); } - FieldGen(ClassFile owner, String name, Type type, int flags) { - if((flags & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_VOLATILE|ACC_TRANSIENT|ACC_STATIC|ACC_FINAL)) != 0) - throw new IllegalArgumentException("invalid flags"); - this.name = name; - this.type = type; - this.flags = flags; - this.attrs = new ClassFile.AttrGen(); - } + private FieldGen(int flags, Type.Class.Field field, ClassFile.AttrGen attrs) { this(field, flags, attrs); } + FieldGen(Type.Class.Field field, int flags) { this(field, flags, new ClassFile.AttrGen()); } + FieldGen(Type.Class.Field field, int flags, ClassFile.AttrGen attrs) { field.super(flags, attrs); } /** Sets the ContantValue attribute for this field. @param val The value to set this field to. Must be an Integer, Long, Float, Double, or String */ public void setConstantValue(Object val) { - if((flags & ACC_STATIC) == 0) throw new IllegalStateException("field does not have the ACC_STATIC bit set"); + if (!isStatic()) throw new IllegalStateException("field does not have the STATIC bit set"); attrs.put("ConstantValue",val); } void finish(ConstantPool cp) { - cp.addUtf8(name); - cp.addUtf8(type.getDescriptor()); + cp.addUtf8(getField().getName()); + cp.addUtf8(getField().getType().getDescriptor()); attrs.finish(cp); } void dump(DataOutput o, ConstantPool cp) throws IOException { - o.writeShort(flags); - o.writeShort(cp.getUtf8Index(name)); - o.writeShort(cp.getUtf8Index(type.getDescriptor())); + o.writeShort(getFlags()); + o.writeShort(cp.getUtf8Index(getField().getName())); + o.writeShort(cp.getUtf8Index(getField().getType().getDescriptor())); attrs.dump(o,cp); } }