ditch debugToString
[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 ClassFile#addField */
7 public class FieldGen extends Type.Class.Field.Body {
8
9     StringBuffer toString(StringBuffer sb) {
10         sb.append(ClassFile.flagsToString(flags, false));
11         sb.append(getField().getType().toString());
12         sb.append(" ");
13         sb.append(getField().getName());
14         if (attrs.contains("ConstantValue")) sb.append(" = \"").append(attrs.get("ConstantValue")).append("\"");
15         sb.append(";");
16         return sb;
17     }
18     
19     FieldGen(Type.Class c, DataInput in, ConstantPool cp) throws IOException {
20         this(in.readShort(),
21              c.field(cp.getUtf8KeyByIndex(in.readShort()),
22                      Type.fromDescriptor(cp.getUtf8KeyByIndex(in.readShort()))),
23              new ClassFile.AttrGen(in, cp));
24     }
25
26     private FieldGen(int flags, Type.Class.Field field, ClassFile.AttrGen attrs) { this(field, flags, attrs); }
27     FieldGen(Type.Class.Field field, int flags) { this(field, flags, new ClassFile.AttrGen()); }
28     FieldGen(Type.Class.Field field, int flags, ClassFile.AttrGen attrs) { field.super(flags, attrs); }
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 (!isStatic()) throw new IllegalStateException("field does not have the STATIC bit set");
34         attrs.put("ConstantValue",val);
35     }
36     
37     void finish(ConstantPool cp) {
38         cp.addUtf8(getField().getName());
39         cp.addUtf8(getField().getType().getDescriptor());
40         
41         attrs.finish(cp);
42     }
43     
44     void dump(DataOutput o, ConstantPool cp) throws IOException {
45         o.writeShort(getFlags());
46         o.writeShort(cp.getUtf8Index(getField().getName()));
47         o.writeShort(cp.getUtf8Index(getField().getType().getDescriptor()));
48         attrs.dump(o,cp);
49     }
50 }