1 package alt.jiapi.reflect; 2 3 import java.util.Iterator ; 4 import java.util.List ; 5 import java.util.LinkedList ; 6 7 import alt.jiapi.file.ClassFile; 8 import alt.jiapi.file.ConstantPool; 9 import alt.jiapi.file.Field; 10 import alt.jiapi.file.Method; 11 import alt.jiapi.file.SyntheticAttribute; 12 import alt.jiapi.file.CodeAttribute; 13 14 19 class ClassBuilder { 20 private ClassFile clazz; 21 22 ClassBuilder(ClassFile clazz) { 23 this.clazz = clazz; 24 } 25 26 JiapiField addField(short modifiers, String type, String name) throws FieldExistsException { 27 ConstantPool cp = clazz.getConstantPool(); 28 29 List fields = clazz.getFields(); 31 Iterator i = fields.iterator(); 32 while(i.hasNext()) { 33 Field field = (Field)i.next(); 34 if (field.getName().equals(name)) { 35 throw new FieldExistsException(new JiapiField(field)); 36 } 37 } 38 39 List attrs = new LinkedList (); 41 attrs.add(new SyntheticAttribute(cp)); 42 43 Field f = new Field(cp, modifiers, name, 45 TypeHelper.typeToDescriptor(type), attrs); 46 fields.add(f); 47 48 return new JiapiField(f); 49 } 50 51 52 JiapiMethod addMethod(short modifiers, String name, Signature signature) throws MethodExistsException { 53 ConstantPool cp = clazz.getConstantPool(); 54 55 56 List attrs = new LinkedList (); 58 attrs.add(new SyntheticAttribute(cp)); 59 60 short maxStack = 0; 61 int maxLocals = signature.getParameters().length + 1; 62 63 attrs.add(new CodeAttribute(cp, maxStack, (short)maxLocals)); 64 65 Method m = new Method(cp, modifiers, name, signature.getDescriptor(), 67 attrs); 68 69 clazz.getMethods().add(m); 70 71 return new JiapiMethod(m); 72 } 73 74 75 ClassFile getClassFile() { 76 return clazz; 77 } 78 } 79 | Popular Tags |