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(ClassGen owner, String name,Type type, int flags) {
17 if((flags & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_VOLATILE|ACC_TRANSIENT|ACC_STATIC|ACC_FINAL)) != 0)
18 throw new IllegalArgumentException("invalid flags");
23 this.attrs = new ClassGen.AttrGen(cp);
26 cp.addUtf8(type.getDescriptor());
29 /** Sets the ContantValue attribute for this field.
30 @param val The value to set this field to. Must be an Integer, Long, Float, Double, or String */
31 public void setConstantValue(Object val) {
32 if((flags & ACC_STATIC) == 0) throw new IllegalStateException("field does not have the ACC_STATIC bit set");
37 if(constantValue != null && !attrs.contains("ConstantValue"))
38 attrs.add("ConstantValue",cp.add(constantValue));
41 void dump(DataOutput o) throws IOException {
43 o.writeShort(cp.getUtf8Index(name));
44 o.writeShort(cp.getUtf8Index(type.getDescriptor()));
45 o.writeShort(attrs.size());