got it to compile
[org.ibex.classgen.git] / src / org / ibex / classgen / FieldGen.java
1 package org.ibex.classgen;
2
3 import java.io.*;
4
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;
13     
14     private Object constantValue;
15     
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");
20         this.cp = owner.cp;
21         this.name = name;
22         this.type = type;
23         this.flags = flags;
24         this.attrs = new ClassGen.AttrGen(cp);
25         
26         cp.addUtf8(name);
27         cp.addUtf8(type.getDescriptor());
28     }
29     
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");
34         constantValue = val;
35     }
36     
37     void finish() {
38         if(constantValue != null && !attrs.contains("ConstantValue"))
39             attrs.add("ConstantValue",cp.add(constantValue));
40     }
41     
42     void dump(DataOutput o) throws IOException {
43         o.writeShort(flags);
44         o.writeShort(cp.getUtf8Index(name));
45         o.writeShort(cp.getUtf8Index(type.getDescriptor()));
46         o.writeShort(attrs.size());
47         attrs.dump(o);
48     }
49 }