1 package org.ibex.classgen;
5 /** Class representing a field in a generated classfile
6 @see ClassFile#addField */
7 public class FieldGen extends Type.Class.Field.Body {
9 StringBuffer toString(StringBuffer sb) {
10 sb.append(ClassFile.flagsToString(flags, false));
11 sb.append(getField().getType().toString());
13 sb.append(getField().getName());
14 if (attrs.contains("ConstantValue")) sb.append(" = \"").append(attrs.get("ConstantValue")).append("\"");
19 FieldGen(Type.Class c, DataInput in, ConstantPool cp) throws IOException {
21 c.field(cp.getUtf8KeyByIndex(in.readShort()),
22 Type.fromDescriptor(cp.getUtf8KeyByIndex(in.readShort()))),
23 new ClassFile.AttrGen(in, cp));
26 private FieldGen(int flags, Type.Class.Field field, ClassFile.AttrGen attrs) { this(field, flags, attrs); }
27 FieldGen(Type.Class.Field field, int flags) { this(field, flags, new ClassFile.AttrGen()); }
28 FieldGen(Type.Class.Field field, int flags, ClassFile.AttrGen attrs) { field.super(flags, attrs); }
30 /** Sets the ContantValue attribute for this field.
31 @param val The value to set this field to. Must be an Integer, Long, Float, Double, or String */
32 public void setConstantValue(Object val) {
33 if (!isStatic()) throw new IllegalStateException("field does not have the STATIC bit set");
34 attrs.put("ConstantValue",val);
37 void finish(ConstantPool cp) {
38 cp.addUtf8(getField().getName());
39 cp.addUtf8(getField().getType().getDescriptor());
44 void dump(DataOutput o, ConstantPool cp) throws IOException {
45 o.writeShort(getFlags());
46 o.writeShort(cp.getUtf8Index(getField().getName()));
47 o.writeShort(cp.getUtf8Index(getField().getType().getDescriptor()));