1 50 package net.sf.just4log.transform; 51 52 import org.apache.bcel.Constants; 53 import org.apache.bcel.generic.ConstantPoolGen; 54 import org.apache.bcel.generic.IFEQ; 55 import org.apache.bcel.generic.INVOKEINTERFACE; 56 import org.apache.bcel.generic.InstructionHandle; 57 import org.apache.bcel.generic.InstructionList; 58 import org.apache.bcel.generic.MethodGen; 59 import org.apache.bcel.generic.ObjectType; 60 import org.apache.bcel.generic.PUSH; 61 import org.apache.bcel.generic.Type; 62 import org.apache.commons.logging.Log; 63 import org.apache.commons.logging.LogFactory; 64 67 68 public class ApacheCommonTransform extends Transform { 69 private static final String INTERFACE = "org.apache.commons.logging.Log"; 70 73 private static final ObjectType logType = new ObjectType(INTERFACE); 74 public ObjectType getLogType() { 75 76 return logType; 77 } 78 private static Log logger = LogFactory.getLog(ApacheCommonTransform.class); 79 private static String CLASSID = 80 "$Id: ApacheCommonTransform.java,v 1.6 2003/07/22 23:38:37 lbruand Exp $"; 81 82 public static void register() { 83 Transform.register(new ApacheCommonTransform()); 84 } 85 86 public String getClassname() { 87 return INTERFACE; 88 } 89 90 93 private ApacheCommonTransform() { 94 super(); 95 96 } 97 98 public InstructionHandle insertFork( 99 InstructionList il, 100 InstructionHandle getStaticHandle, 101 InstructionHandle invokeInterfaceHandle, 102 ConstantPoolGen cp) { 103 logger.info("Inserting GETSTATIC"); 104 InstructionHandle insertHandle = 105 il.insert(getStaticHandle, getStaticHandle.getInstruction().copy()); 106 String isEnabled = 107 getIsEnabled( 108 ( 109 (INVOKEINTERFACE) invokeInterfaceHandle 110 .getInstruction()) 111 .getMethodName( 112 cp)); 113 logger.info("Inserting INVOKEINTERFACE call to " + isEnabled); 114 il.insert( 115 getStaticHandle, 116 instFact.createInvoke( 117 INTERFACE, 118 isEnabled, 119 Type.BOOLEAN, 120 Type.NO_ARGS, 121 Constants.INVOKEINTERFACE)); 122 logger.info("Inserting IFEQ"); 123 il.insert(getStaticHandle, new IFEQ(invokeInterfaceHandle.getNext())); 124 return insertHandle; 125 } 126 127 132 private static final String getIsEnabled(String originalMethod) { 133 return "is" 134 + originalMethod.substring(0, 1).toUpperCase() 135 + originalMethod.substring(1) 136 + "Enabled"; 137 } 138 139 140 143 public InstructionHandle insertEnter( 144 MethodGen orig, 145 InstructionList il, 146 InstructionHandle firstInstructionHandle, 147 ConstantPoolGen cp) { 148 logger.info("Inserting Enter code."); 149 InstructionHandle backup = il.insert( 150 firstInstructionHandle, 151 instFact.createGetStatic( 152 clazz.getClassName(), 153 loggerAttribute.getName(), 154 loggerAttribute.getType())); 155 156 il.insert(firstInstructionHandle, 157 new PUSH(cp, Transform.ENTER_STRING+ getMethodRepr(orig)) 158 ); 159 160 161 il.insert( 162 firstInstructionHandle, 163 instFact.createInvoke( 164 INTERFACE, 165 "trace", 166 Type.VOID, 167 new Type[] {Type.OBJECT}, 168 Constants.INVOKEINTERFACE)); 169 170 return backup; 171 } 172 173 176 public InstructionHandle insertExit( 177 MethodGen orig, 178 InstructionList il, 179 InstructionHandle returnInstructionHandle, 180 ConstantPoolGen cp) { 181 logger.info("Inserting Exit code."); 182 InstructionHandle backup = il.insert( 183 returnInstructionHandle, 184 instFact.createGetStatic( 185 clazz.getClassName(), 186 loggerAttribute.getName(), 187 loggerAttribute.getType())); 188 189 il.insert(returnInstructionHandle, 190 new PUSH(cp, Transform.EXIT_STRING+ getMethodRepr(orig)) 191 ); 192 193 194 il.insert( 195 returnInstructionHandle, 196 instFact.createInvoke( 197 INTERFACE, 198 "trace", 199 Type.VOID, 200 new Type[] {Type.OBJECT}, 201 Constants.INVOKEINTERFACE)); 202 203 return backup; 204 } 205 206 } 207 | Popular Tags |