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 70 public class ClassGen extends AccessFlags implements Cloneable { 71 73 private String class_name, super_class_name, file_name; 74 private int class_name_index = -1, superclass_name_index = -1; 75 private int major = Constants.MAJOR_1_1, minor = Constants.MINOR_1_1; 76 77 private ConstantPoolGen cp; 79 private ArrayList field_vec = new ArrayList (); 81 private ArrayList method_vec = new ArrayList (); 82 private ArrayList attribute_vec = new ArrayList (); 83 private ArrayList interface_vec = new ArrayList (); 84 85 93 public ClassGen(String class_name, String super_class_name, String file_name, 94 int access_flags, String [] interfaces) { 95 this.class_name = class_name; 96 this.super_class_name = super_class_name; 97 this.file_name = file_name; 98 this.access_flags = access_flags; 99 cp = new ConstantPoolGen(); 101 addAttribute(new SourceFile(cp.addUtf8("SourceFile"), 2, 103 cp.addUtf8(file_name), cp.getConstantPool())); 104 class_name_index = cp.addClass(class_name); 105 superclass_name_index = cp.addClass(super_class_name); 106 107 if(interfaces != null) 108 for(int i=0; i < interfaces.length; i++) 109 addInterface(interfaces[i]); 110 } 111 112 116 public ClassGen(JavaClass clazz) { 117 class_name_index = clazz.getClassNameIndex(); 118 superclass_name_index = clazz.getSuperclassNameIndex(); 119 class_name = clazz.getClassName(); 120 super_class_name = clazz.getSuperclassName(); 121 file_name = clazz.getSourceFileName(); 122 access_flags = clazz.getAccessFlags(); 123 cp = new ConstantPoolGen(clazz.getConstantPool()); 124 major = clazz.getMajor(); 125 minor = clazz.getMinor(); 126 127 Attribute[] attributes = clazz.getAttributes(); 128 Method[] methods = clazz.getMethods(); 129 Field[] fields = clazz.getFields(); 130 String [] interfaces = clazz.getInterfaceNames(); 131 132 for(int i=0; i < interfaces.length; i++) 133 addInterface(interfaces[i]); 134 135 for(int i=0; i < attributes.length; i++) 136 addAttribute(attributes[i]); 137 138 for(int i=0; i < methods.length; i++) 139 addMethod(methods[i]); 140 141 for(int i=0; i < fields.length; i++) 142 addField(fields[i]); 143 } 144 145 148 public JavaClass getJavaClass() { 149 int[] interfaces = getInterfaces(); 150 Field[] fields = getFields(); 151 Method[] methods = getMethods(); 152 Attribute[] attributes = getAttributes(); 153 154 ConstantPool cp = this.cp.getFinalConstantPool(); 156 157 return new JavaClass(class_name_index, superclass_name_index, 158 file_name, major, minor, access_flags, 159 cp, interfaces, fields, methods, attributes); 160 } 161 162 166 public void addInterface(String name) { 167 interface_vec.add(name); 168 } 169 170 174 public void removeInterface(String name) { 175 interface_vec.remove(name); 176 } 177 178 181 public int getMajor() { return major; } 182 183 186 public void setMajor(int major) { 187 this.major = major; 188 } 189 190 193 public void setMinor(int minor) { 194 this.minor = minor; 195 } 196 197 200 public int getMinor() { return minor; } 201 202 206 public void addAttribute(Attribute a) { attribute_vec.add(a); } 207 208 212 public void addMethod(Method m) { method_vec.add(m); } 213 214 220 public void addEmptyConstructor(int access_flags) { 221 InstructionList il = new InstructionList(); 222 il.append(InstructionConstants.THIS); il.append(new INVOKESPECIAL(cp.addMethodref(super_class_name, 224 "<init>", "()V"))); 225 il.append(InstructionConstants.RETURN); 226 227 MethodGen mg = new MethodGen(access_flags, Type.VOID, Type.NO_ARGS, null, 228 "<init>", class_name, il, cp); 229 mg.setMaxStack(1); 230 addMethod(mg.getMethod()); 231 } 232 233 237 public void addField(Field f) { field_vec.add(f); } 238 239 public boolean containsField(Field f) { return field_vec.contains(f); } 240 241 243 public Field containsField(String name) { 244 for(Iterator e=field_vec.iterator(); e.hasNext(); ) { 245 Field f = (Field)e.next(); 246 if(f.getName().equals(name)) 247 return f; 248 } 249 250 return null; 251 } 252 253 255 public Method containsMethod(String name, String signature) { 256 for(Iterator e=method_vec.iterator(); e.hasNext();) { 257 Method m = (Method)e.next(); 258 if(m.getName().equals(name) && m.getSignature().equals(signature)) 259 return m; 260 } 261 262 return null; 263 } 264 265 269 public void removeAttribute(Attribute a) { attribute_vec.remove(a); } 270 271 275 public void removeMethod(Method m) { method_vec.remove(m); } 276 277 280 public void replaceMethod(Method old, Method new_) { 281 if(new_ == null) 282 throw new ClassGenException("Replacement method must not be null"); 283 284 int i = method_vec.indexOf(old); 285 286 if(i < 0) 287 method_vec.add(new_); 288 else 289 method_vec.set(i, new_); 290 } 291 292 295 public void replaceField(Field old, Field new_) { 296 if(new_ == null) 297 throw new ClassGenException("Replacement method must not be null"); 298 299 int i = field_vec.indexOf(old); 300 301 if(i < 0) 302 field_vec.add(new_); 303 else 304 field_vec.set(i, new_); 305 } 306 307 311 public void removeField(Field f) { field_vec.remove(f); } 312 313 public String getClassName() { return class_name; } 314 public String getSuperclassName() { return super_class_name; } 315 public String getFileName() { return file_name; } 316 317 public void setClassName(String name) { 318 class_name = name.replace('/', '.'); 319 class_name_index = cp.addClass(name); 320 } 321 322 public void setSuperclassName(String name) { 323 super_class_name = name.replace('/', '.'); 324 superclass_name_index = cp.addClass(name); 325 } 326 327 public Method[] getMethods() { 328 Method[] methods = new Method[method_vec.size()]; 329 method_vec.toArray(methods); 330 return methods; 331 } 332 333 public void setMethods(Method[] methods) { 334 method_vec.clear(); 335 for(int m=0; m<methods.length; m++) 336 addMethod(methods[m]); 337 } 338 339 public void setMethodAt(Method method, int pos) { 340 method_vec.set(pos, method); 341 } 342 343 public Method getMethodAt(int pos) { 344 return (Method)method_vec.get(pos); 345 } 346 347 public String [] getInterfaceNames() { 348 int size = interface_vec.size(); 349 String [] interfaces = new String [size]; 350 351 interface_vec.toArray(interfaces); 352 return interfaces; 353 } 354 355 public int[] getInterfaces() { 356 int size = interface_vec.size(); 357 int[] interfaces = new int[size]; 358 359 for(int i=0; i < size; i++) 360 interfaces[i] = cp.addClass((String )interface_vec.get(i)); 361 362 return interfaces; 363 } 364 365 public Field[] getFields() { 366 Field[] fields = new Field[field_vec.size()]; 367 field_vec.toArray(fields); 368 return fields; 369 } 370 371 public Attribute[] getAttributes() { 372 Attribute[] attributes = new Attribute[attribute_vec.size()]; 373 attribute_vec.toArray(attributes); 374 return attributes; 375 } 376 377 public ConstantPoolGen getConstantPool() { return cp; } 378 public void setConstantPool(ConstantPoolGen constant_pool) { 379 cp = constant_pool; 380 } 381 382 public void setClassNameIndex(int class_name_index) { 383 this.class_name_index = class_name_index; 384 class_name = cp.getConstantPool(). 385 getConstantString(class_name_index, Constants.CONSTANT_Class).replace('/', '.'); 386 } 387 388 public void setSuperclassNameIndex(int superclass_name_index) { 389 this.superclass_name_index = superclass_name_index; 390 super_class_name = cp.getConstantPool(). 391 getConstantString(superclass_name_index, Constants.CONSTANT_Class).replace('/', '.'); 392 } 393 394 public int getSuperclassNameIndex() { return superclass_name_index; } 395 396 public int getClassNameIndex() { return class_name_index; } 397 398 private ArrayList observers; 399 400 402 public void addObserver(ClassObserver o) { 403 if(observers == null) 404 observers = new ArrayList (); 405 406 observers.add(o); 407 } 408 409 411 public void removeObserver(ClassObserver o) { 412 if(observers != null) 413 observers.remove(o); 414 } 415 416 420 public void update() { 421 if(observers != null) 422 for(Iterator e = observers.iterator(); e.hasNext(); ) 423 ((ClassObserver)e.next()).notify(this); 424 } 425 426 public Object clone() { 427 try { 428 return super.clone(); 429 } catch(CloneNotSupportedException e) { 430 System.err.println(e); 431 return null; 432 } 433 } 434 } 435 | Popular Tags |