X-Git-Url: http://git.megacz.com/?p=org.ibex.classgen.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fclassgen%2FFieldGen.java;h=b6573428f6327ceeab309b6bb02b215344fc8568;hp=826eb7884c56f1efe674f353c3a104bfb7dd6be7;hb=6d02ca13f481e213ee0ae26f7acdd40b20dfc6af;hpb=94cebd4c12247ae7fd0a4b0cc66609fead0efece diff --git a/src/org/ibex/classgen/FieldGen.java b/src/org/ibex/classgen/FieldGen.java index 826eb78..b657342 100644 --- a/src/org/ibex/classgen/FieldGen.java +++ b/src/org/ibex/classgen/FieldGen.java @@ -3,46 +3,60 @@ package org.ibex.classgen; import java.io.*; /** Class representing a field in a generated classfile - @see ClassGen#addField */ + @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 ClassGen.AttrGen attrs; - - private Object constantValue; + private final ClassFile.AttrGen attrs; + + StringBuffer debugToString(StringBuffer sb) { + sb.append(ClassFile.flagsToString(flags,false)); + sb.append(type.debugToString()); + sb.append(" "); + sb.append(name); + if(attrs.contains("ConstantValue")) + sb.append(" = \"").append(attrs.get("ConstantValue")).append("\""); + sb.append(";"); + return sb; + } - FieldGen(ClassGen owner, String name,Type type, int flags) { - if((flags & ~(ACC_PUBLIC|ACC_PRIVATE|ACC_PROTECTED|ACC_VOLATILE|ACC_TRANSIENT|ACC_STATIC|ACC_FINAL)) != 0) + FieldGen(DataInput in, ConstantPool cp) throws IOException { + flags = in.readShort(); + if((flags & ~(PUBLIC|PRIVATE|PROTECTED|VOLATILE|TRANSIENT|STATIC|FINAL)) != 0) + throw new ClassFile.ClassReadExn("invalid flags"); + name = cp.getUtf8KeyByIndex(in.readShort()); + type = Type.instance(cp.getUtf8KeyByIndex(in.readShort())); + attrs = new ClassFile.AttrGen(in,cp); + } + + FieldGen(ClassFile owner, String name, Type type, int flags) { + if((flags & ~(PUBLIC|PRIVATE|PROTECTED|VOLATILE|TRANSIENT|STATIC|FINAL)) != 0) throw new IllegalArgumentException("invalid flags"); - this.cp = owner.cp; this.name = name; this.type = type; this.flags = flags; - this.attrs = new ClassGen.AttrGen(cp); - - cp.addUtf8(name); - cp.addUtf8(type.getDescriptor()); - } + this.attrs = new ClassFile.AttrGen(); + } /** 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; + if((flags & STATIC) == 0) throw new IllegalStateException("field does not have the STATIC bit set"); + attrs.put("ConstantValue",val); } - void finish() { - if(constantValue != null && !attrs.contains("ConstantValue")) - attrs.add("ConstantValue",cp.add(constantValue)); + void finish(ConstantPool cp) { + cp.addUtf8(name); + cp.addUtf8(type.getDescriptor()); + + attrs.finish(cp); } - void dump(DataOutput o) throws IOException { + void dump(DataOutput o, ConstantPool cp) throws IOException { o.writeShort(flags); o.writeShort(cp.getUtf8Index(name)); o.writeShort(cp.getUtf8Index(type.getDescriptor())); - o.writeShort(attrs.size()); - attrs.dump(o); + attrs.dump(o,cp); } }