misc cleanup and javadoc comments
[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(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");
19         this.cp = owner.cp;
20         this.name = name;
21         this.type = type;
22         this.flags = flags;
23         this.attrs = new ClassGen.AttrGen(cp);
24         
25         cp.addUtf8(name);
26         cp.addUtf8(type.getDescriptor());
27     }
28     
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");
33         constantValue = val;
34     }
35     
36     void finish() {
37         if(constantValue != null && !attrs.contains("ConstantValue"))
38             attrs.add("ConstantValue",cp.add(constantValue));
39     }
40     
41     void dump(DataOutput o) throws IOException {
42         o.writeShort(flags);
43         o.writeShort(cp.getUtf8Index(name));
44         o.writeShort(cp.getUtf8Index(type.getDescriptor()));
45         o.writeShort(attrs.size());
46         attrs.dump(o);
47     }
48 }