00001 package de.fub.bytecode.generic;
00002
00003 import de.fub.bytecode.Constants;
00004 import de.fub.bytecode.classfile.*;
00005 import java.util.Vector;
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 public class FieldGen extends AccessFlags implements Constants {
00017 private Type type;
00018 private String name;
00019 private ConstantPoolGen cp;
00020 private int index = -1;
00021 private Vector attributes;
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 public FieldGen(int access_flags, Type type, String name, ConstantPoolGen cp) {
00033 this.access_flags = access_flags;
00034 this.type = type;
00035 this.name = name;
00036 this.cp = cp;
00037 }
00038 public FieldGen(Field field, ConstantPoolGen cp) {
00039 this(field.getAccessFlags(), Type.getType(field.getSignature()), field.getName(), cp);
00040 }
00041 public void addAttribute(Attribute attr) {
00042 if(attributes == null)
00043 attributes = new Vector();
00044
00045 attributes.addElement(attr);
00046 }
00047 public Attribute[] getAttributes() {
00048 if(attributes != null) {
00049 Attribute[] attrs = new Attribute[attributes.size()];
00050 attributes.copyInto(attrs);
00051 return attrs;
00052 } else
00053 return null;
00054 }
00055 public ConstantPoolGen getConstantPool() { return cp; }
00056
00057
00058
00059 public Field getField() {
00060 String signature = type.getSignature();
00061 int name_index = cp.addUtf8(name);
00062 int signature_index = cp.addUtf8(signature);
00063
00064 if(index > 0)
00065 if(isStatic())
00066 addAttribute(new ConstantValue(cp.addUtf8("ConstantValue"),
00067 2, index, cp.getConstantPool()));
00068 else
00069 throw new ClassGenException("Only static fields may have an initial value!");
00070
00071 return new Field(access_flags, name_index, signature_index, getAttributes(),
00072 cp.getConstantPool());
00073 }
00074 public String getName() { return name; }
00075 public String getSignature() { return type.getSignature(); }
00076 public Type getType() { return type; }
00077 public void setConstantPool(ConstantPoolGen cp) { this.cp = cp; }
00078 public void setInitValue(byte b) { if(b != 0) index = cp.addInteger(b); }
00079 public void setInitValue(char c) { if(c != 0) index = cp.addInteger(c); }
00080 public void setInitValue(double d) { if(d != 0.0) index = cp.addDouble(d); }
00081 public void setInitValue(float f) { if(f != 0.0) index = cp.addFloat(f); }
00082 public void setInitValue(int i) { if(i != 0) index = cp.addInteger(i); }
00083 public void setInitValue(long l) { if(l != 0L) index = cp.addLong(l); }
00084
00085
00086
00087
00088 public void setInitValue(String str) { if(str != null) index = cp.addString(str); }
00089 public void setInitValue(short s) { if(s != 0) index = cp.addInteger(s); }
00090 public void setInitValue(boolean b) { if(b) index = cp.addInteger(1); }
00091 public void setName(String name) { this.name = name; }
00092 public void setType(Type type) { this.type = type; }
00093 }