X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fclassgen%2FFieldGen.java;h=cf269d01973fc7502fa1d14cca605e709785ebb4;hb=20b36057bc5aee6e799cce02a3bf289916889fc1;hp=39e91c0b04cc9152180f86894ec2283087fba5fb;hpb=bc9112573cba51be5e7d285ccd3e496be4278c63;p=org.ibex.classgen.git diff --git a/src/org/ibex/classgen/FieldGen.java b/src/org/ibex/classgen/FieldGen.java index 39e91c0..cf269d0 100644 --- a/src/org/ibex/classgen/FieldGen.java +++ b/src/org/ibex/classgen/FieldGen.java @@ -2,27 +2,61 @@ package org.ibex.classgen; import java.io.*; +/** Class representing a field in a generated classfile + @see ClassFile#addField */ public class FieldGen implements CGConst { private final CPGen cp; private final String name; private final Type type; private final int flags; - private final AttrGen attrs; + private final ClassFile.AttrGen attrs; - FieldGen(ClassGen owner, String name,Type type, int flags) { + private Object constantValue; + + public String toString() { StringBuffer sb = new StringBuffer(); toString(sb); return sb.toString(); } + public void toString(StringBuffer sb) { + sb.append(ClassFile.flagsToString(flags)); + sb.append(type); + sb.append(" "); + sb.append(name); + sb.append(";"); + // FIXME: attrs + } + + FieldGen(CPGen cp, DataInput in) throws IOException { + this.cp = cp; + flags = in.readShort(); + name = cp.getUtf8ByIndex(in.readShort()); + type = cp.getType(in.readShort()); + attrs = new ClassFile.AttrGen(cp, in); + } + + FieldGen(ClassFile owner, String name, Type type, int flags) { if((flags & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_VOLATILE|ACC_TRANSIENT|ACC_STATIC|ACC_FINAL)) != 0) throw new IllegalArgumentException("invalid flags"); this.cp = owner.cp; this.name = name; this.type = type; this.flags = flags; - this.attrs = new AttrGen(cp); + this.attrs = new ClassFile.AttrGen(cp); cp.addUtf8(name); cp.addUtf8(type.getDescriptor()); } - public void dump(DataOutput o) throws IOException { + /** Sets the ContantValue attribute for this field. + @param val The value to set this field to. Must be an Integer, Long, Float, Double, or String */ + public void setConstantValue(Object val) { + if((flags & ACC_STATIC) == 0) throw new IllegalStateException("field does not have the ACC_STATIC bit set"); + constantValue = val; + } + + void finish() { + if(constantValue != null && !attrs.contains("ConstantValue")) + attrs.add("ConstantValue", cp.add(constantValue)); + } + + void dump(DataOutput o) throws IOException { o.writeShort(flags); o.writeShort(cp.getUtf8Index(name)); o.writeShort(cp.getUtf8Index(type.getDescriptor()));