1 package org.ibex.classgen;
5 /** Class representing a field in a generated classfile
6 @see ClassGen#addField */
7 public class FieldGen implements CGConst {
8 private final CPGen cp;
9 private final String name;
10 private final Type type;
11 private final int flags;
12 private final ClassGen.AttrGen attrs;
14 private Object constantValue;
16 FieldGen(DataInput in) { throw new Error("Brian is lame"); }
17 FieldGen(ClassGen owner, String name, Type type, int flags) {
18 if((flags & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_VOLATILE|ACC_TRANSIENT|ACC_STATIC|ACC_FINAL)) != 0)
19 throw new IllegalArgumentException("invalid flags");
24 this.attrs = new ClassGen.AttrGen(cp);
27 cp.addUtf8(type.getDescriptor());
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((flags & ACC_STATIC) == 0) throw new IllegalStateException("field does not have the ACC_STATIC bit set");
38 if(constantValue != null && !attrs.contains("ConstantValue"))
39 attrs.add("ConstantValue", cp.add(constantValue));
42 void dump(DataOutput o) throws IOException {
44 o.writeShort(cp.getUtf8Index(name));
45 o.writeShort(cp.getUtf8Index(type.getDescriptor()));
46 o.writeShort(attrs.size());