1 package org.ibex.classgen;
5 /** Class representing a field in a generated classfile
6 @see ClassFile#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 ClassFile.AttrGen attrs;
14 private Object constantValue;
16 public String toString() { StringBuffer sb = new StringBuffer(); toString(sb); return sb.toString(); }
17 public void toString(StringBuffer sb) {
18 sb.append(ClassFile.flagsToString(flags));
26 FieldGen(CPGen cp, DataInput in) throws IOException {
28 flags = in.readShort();
29 name = cp.getUtf8ByIndex(in.readShort());
30 type = cp.getType(in.readShort());
31 attrs = new ClassFile.AttrGen(cp, in);
34 FieldGen(ClassFile owner, String name, Type type, int flags) {
35 if((flags & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_VOLATILE|ACC_TRANSIENT|ACC_STATIC|ACC_FINAL)) != 0)
36 throw new IllegalArgumentException("invalid flags");
41 this.attrs = new ClassFile.AttrGen(cp);
44 cp.addUtf8(type.getDescriptor());
47 /** Sets the ContantValue attribute for this field.
48 @param val The value to set this field to. Must be an Integer, Long, Float, Double, or String */
49 public void setConstantValue(Object val) {
50 if((flags & ACC_STATIC) == 0) throw new IllegalStateException("field does not have the ACC_STATIC bit set");
55 if(constantValue != null && !attrs.contains("ConstantValue"))
56 attrs.add("ConstantValue", cp.add(constantValue));
59 void dump(DataOutput o) throws IOException {
61 o.writeShort(cp.getUtf8Index(name));
62 o.writeShort(cp.getUtf8Index(type.getDescriptor()));
63 o.writeShort(attrs.size());