1 package com.sun.org.apache.bcel.internal.generic; 2 3 56 57 import com.sun.org.apache.bcel.internal.Constants; 58 import com.sun.org.apache.bcel.internal.classfile.*; 59 import java.util.ArrayList ; 60 import java.util.Iterator ; 61 62 71 public class FieldGen extends FieldGenOrMethodGen { 72 private Object value = null; 73 74 84 public FieldGen(int access_flags, Type type, String name, ConstantPoolGen cp) { 85 setAccessFlags(access_flags); 86 setType(type); 87 setName(name); 88 setConstantPool(cp); 89 } 90 91 97 public FieldGen(Field field, ConstantPoolGen cp) { 98 this(field.getAccessFlags(), Type.getType(field.getSignature()), field.getName(), cp); 99 100 Attribute[] attrs = field.getAttributes(); 101 102 for(int i=0; i < attrs.length; i++) { 103 if(attrs[i] instanceof ConstantValue) 104 setValue(((ConstantValue)attrs[i]).getConstantValueIndex()); 105 else 106 addAttribute(attrs[i]); 107 } 108 } 109 110 private void setValue(int index) { 111 ConstantPool cp = this.cp.getConstantPool(); 112 Constant c = cp.getConstant(index); 113 value = ((ConstantObject)c).getConstantValue(cp); 114 } 115 116 120 public void setInitValue(String str) { 121 checkType(new ObjectType("java.lang.String")); 122 123 if(str != null) 124 value = str; 125 } 126 127 public void setInitValue(long l) { 128 checkType(Type.LONG); 129 130 if(l != 0L) 131 value = new Long (l); 132 } 133 134 public void setInitValue(int i) { 135 checkType(Type.INT); 136 137 if(i != 0) 138 value = new Integer (i); 139 } 140 141 public void setInitValue(short s) { 142 checkType(Type.SHORT); 143 144 if(s != 0) 145 value = new Integer (s); 146 } 147 148 public void setInitValue(char c) { 149 checkType(Type.CHAR); 150 151 if(c != 0) 152 value = new Integer (c); 153 } 154 155 public void setInitValue(byte b) { 156 checkType(Type.BYTE); 157 158 if(b != 0) 159 value = new Integer (b); 160 } 161 162 public void setInitValue(boolean b) { 163 checkType(Type.BOOLEAN); 164 165 if(b) 166 value = new Integer (1); 167 } 168 169 public void setInitValue(float f) { 170 checkType(Type.FLOAT); 171 172 if(f != 0.0) 173 value = new Float (f); 174 } 175 176 public void setInitValue(double d) { 177 checkType(Type.DOUBLE); 178 179 if(d != 0.0) 180 value = new Double (d); 181 } 182 183 185 public void cancelInitValue() { 186 value = null; 187 } 188 189 private void checkType(Type atype) { 190 if(type == null) 191 throw new ClassGenException("You haven't defined the type of the field yet"); 192 193 if(!isFinal()) 194 throw new ClassGenException("Only final fields may have an initial value!"); 195 196 if(!type.equals(atype)) 197 throw new ClassGenException("Types are not compatible: " + type + " vs. " + atype); 198 } 199 200 203 public Field getField() { 204 String signature = getSignature(); 205 int name_index = cp.addUtf8(name); 206 int signature_index = cp.addUtf8(signature); 207 208 if(value != null) { 209 checkType(type); 210 int index = addConstant(); 211 addAttribute(new ConstantValue(cp.addUtf8("ConstantValue"), 212 2, index, cp.getConstantPool())); 213 } 214 215 return new Field(access_flags, name_index, signature_index, getAttributes(), 216 cp.getConstantPool()); 217 } 218 219 private int addConstant() { 220 switch(type.getType()) { 221 case Constants.T_INT: case Constants.T_CHAR: case Constants.T_BYTE: 222 case Constants.T_BOOLEAN: case Constants.T_SHORT: 223 return cp.addInteger(((Integer )value).intValue()); 224 225 case Constants.T_FLOAT: 226 return cp.addFloat(((Float )value).floatValue()); 227 228 case Constants.T_DOUBLE: 229 return cp.addDouble(((Double )value).doubleValue()); 230 231 case Constants.T_LONG: 232 return cp.addLong(((Long )value).longValue()); 233 234 case Constants.T_REFERENCE: 235 return cp.addString(((String )value)); 236 237 default: 238 throw new RuntimeException ("Oops: Unhandled : " + type.getType()); 239 } 240 } 241 242 public String getSignature() { return type.getSignature(); } 243 244 private ArrayList observers; 245 246 248 public void addObserver(FieldObserver o) { 249 if(observers == null) 250 observers = new ArrayList (); 251 252 observers.add(o); 253 } 254 255 257 public void removeObserver(FieldObserver o) { 258 if(observers != null) 259 observers.remove(o); 260 } 261 262 266 public void update() { 267 if(observers != null) 268 for(Iterator e = observers.iterator(); e.hasNext(); ) 269 ((FieldObserver)e.next()).notify(this); 270 } 271 272 public String getInitValue() { 273 if(value != null) { 274 return value.toString(); 275 } else 276 return null; 277 } 278 279 285 public final String toString() { 286 String name, signature, access; 288 access = Utility.accessToString(access_flags); 289 access = access.equals("")? "" : (access + " "); 290 signature = type.toString(); 291 name = getName(); 292 293 StringBuffer buf = new StringBuffer (access + signature + " " + name); 294 String value = getInitValue(); 295 296 if(value != null) 297 buf.append(" = " + value); 298 299 return buf.toString(); 300 } 301 302 304 public FieldGen copy(ConstantPoolGen cp) { 305 FieldGen fg = (FieldGen)clone(); 306 307 fg.setConstantPool(cp); 308 return fg; 309 } 310 } 311 | Popular Tags |