X-Git-Url: http://git.megacz.com/?p=org.ibex.classgen.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fclassgen%2FFieldGen.java;h=7f5242c87f08c6637ee0f31793c3aa8ed19e7c2f;hp=479a481f16cad5551dac4db6c5566dd28e9ab66e;hb=e70f9a95d3058a30dc93d8d45078ece8bb7a7dc6;hpb=be231cfa8ce0eac52873b9f0f82b8aecca9c458a diff --git a/src/org/ibex/classgen/FieldGen.java b/src/org/ibex/classgen/FieldGen.java index 479a481..7f5242c 100644 --- a/src/org/ibex/classgen/FieldGen.java +++ b/src/org/ibex/classgen/FieldGen.java @@ -3,64 +3,52 @@ package org.ibex.classgen; import java.io.*; /** Class representing a field in a generated classfile - @see ClassGen#addField */ -public class FieldGen implements CGConst { - private final CPGen cp; - private final String name; - private final Type type; - private final int flags; - private final ClassGen.AttrGen attrs; - - private Object constantValue; + @see ClassFile#addField */ +public class FieldGen extends Type.Class.Field.Body { + private final ClassFile.AttrGen attrs; - public String toString() { StringBuffer sb = new StringBuffer(); toString(sb); return sb.toString(); } - public void toString(StringBuffer sb) { - sb.append(ClassGen.flagsToString(flags)); - sb.append(type); + StringBuffer debugToString(StringBuffer sb) { + sb.append(ClassFile.flagsToString(flags, false)); + sb.append(getField().getType().debugToString()); sb.append(" "); - sb.append(name); + sb.append(getField().getName()); + if(attrs.contains("ConstantValue")) + sb.append(" = \"").append(attrs.get("ConstantValue")).append("\""); sb.append(";"); - // FIXME: attrs + return sb; } - FieldGen(CPGen cp, DataInput in) throws IOException { - this.cp = cp; - flags = in.readShort(); - name = cp.getUtf8ByIndex(in.readShort()); - type = cp.getType(in.readShort()); - attrs = new ClassGen.AttrGen(cp, in); + FieldGen(ClassFile cf, DataInput in, ConstantPool cp) throws IOException { + this(in.readShort(), + cf.getType().field(cp.getUtf8KeyByIndex(in.readShort()), + Type.fromDescriptor(cp.getUtf8KeyByIndex(in.readShort())))); } - FieldGen(ClassGen 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.cp = owner.cp; - this.name = name; - this.type = type; - this.flags = flags; - this.attrs = new ClassGen.AttrGen(cp); - - cp.addUtf8(name); - cp.addUtf8(type.getDescriptor()); - } + private FieldGen(int flags, Type.Class.Field field) { this(field, flags); } + 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); + this.attrs = 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"); - constantValue = val; + if (!isStatic()) throw new IllegalStateException("field does not have the STATIC bit set"); + attrs.put("ConstantValue",val); } - void finish() { - if(constantValue != null && !attrs.contains("ConstantValue")) - attrs.add("ConstantValue", cp.add(constantValue)); + void finish(ConstantPool cp) { + cp.addUtf8(getField().getName()); + cp.addUtf8(getField().getType().getDescriptor()); + + attrs.finish(cp); } - void dump(DataOutput o) throws IOException { - o.writeShort(flags); - o.writeShort(cp.getUtf8Index(name)); - o.writeShort(cp.getUtf8Index(type.getDescriptor())); - o.writeShort(attrs.size()); - attrs.dump(o); + void dump(DataOutput o, ConstantPool cp) throws IOException { + o.writeShort(getFlags()); + o.writeShort(cp.getUtf8Index(getField().getName())); + o.writeShort(cp.getUtf8Index(getField().getType().getDescriptor())); + attrs.dump(o,cp); } }