1 18 19 package alt.jiapi.instrumentor; 20 21 import java.lang.reflect.Modifier ; 22 23 import org.apache.log4j.Category; 24 25 import alt.jiapi.Runtime; 26 import alt.jiapi.reflect.FieldExistsException; 27 import alt.jiapi.reflect.InstructionList; 28 import alt.jiapi.reflect.JiapiClass; 29 import alt.jiapi.reflect.JiapiField; 30 31 38 public class CreateFieldInstrumentor extends AbstractInstrumentor { 39 private static Category log = Runtime.getLogCategory(CreateFieldInstrumentor.class); 40 private int modifiers; 41 private String type; 42 private String name; 43 44 50 public CreateFieldInstrumentor(int modifiers, String type, String name) { 51 this.modifiers = modifiers; 52 this.type = type; 53 this.name = name; 54 } 55 56 60 public CreateFieldInstrumentor(JiapiField field) { 61 this(field.getModifiers(), field.getTypeName(), field.getName()); 62 } 63 64 69 public void instrument(InstructionList il) { 70 JiapiClass clazz = getCurrentClass(); 71 log.info("Adding field '" + modifiers + " " + type + " " + name + 72 "' to " + clazz.getName()); 73 74 if (Modifier.isInterface(clazz.getModifiers())) { 77 if (!(Modifier.isPublic(modifiers) && 78 Modifier.isStatic(modifiers) && 79 Modifier.isFinal(modifiers))) { 80 81 log.info("Cannot add '" + modifiers + " " + type + " " + name + 82 "' to " + clazz.getName()); 83 84 forward(il); 85 return; 86 } 87 } 88 89 try { 90 clazz.addField(modifiers, type, name); 91 } 92 catch (FieldExistsException fee) { 93 log.info("field " + fee.getField() + " already exists in a class "+ 94 clazz.getName()); 95 } 96 97 forward(il); 98 } 99 } 100 | Popular Tags |