1 50 package net.sf.just4log.transform; 51 52 import java.util.HashMap ; 53 54 import org.apache.bcel.Constants; 55 import org.apache.bcel.generic.ConstantPoolGen; 56 import org.apache.bcel.generic.IFEQ; 57 import org.apache.bcel.generic.InstructionHandle; 58 import org.apache.bcel.generic.InstructionList; 59 import org.apache.bcel.generic.InvokeInstruction; 60 import org.apache.bcel.generic.MethodGen; 61 import org.apache.bcel.generic.ObjectType; 62 import org.apache.bcel.generic.PUSH; 63 import org.apache.bcel.generic.Type; 64 import org.apache.commons.logging.Log; 65 import org.apache.commons.logging.LogFactory; 66 67 70 71 public class LogJDK14Transform extends Transform { 72 73 private static final String INTERFACE = "java.util.logging.Logger"; 74 77 private static final ObjectType logType = new ObjectType(INTERFACE); 78 public ObjectType getLogType() { 79 return logType; 80 } 81 private static final String LEVEL = "java.util.logging.Level"; 82 83 private static final ObjectType LevelType = new ObjectType(LEVEL); 84 private static final ObjectType[] ARGUMENTS_ISENABLEDFOR = 85 new ObjectType[] { new ObjectType(LEVEL)}; 86 87 private static Log logger = LogFactory.getLog(LogJDK14Transform.class); 88 private static String CLASSID = 89 "$Id: LogJDK14Transform.java,v 1.6 2003/07/23 21:19:12 lbruand Exp $"; 90 91 public static void register() { 92 Transform.register(new LogJDK14Transform()); 93 } 94 95 98 public LogJDK14Transform() { 99 super(); 100 } 101 102 105 public InstructionHandle insertFork( 106 InstructionList il, 107 InstructionHandle getStaticHandle, 108 InstructionHandle invokeHandle, 109 ConstantPoolGen cp) { 110 String level = 111 getIsEnabled( 112 ((InvokeInstruction) invokeHandle.getInstruction()), 113 cp); 114 if (level == null) { 115 return null; 117 } 118 logger.info("the level of enabling is: " + level); 119 logger.info("Inserting GETSTATIC"); 120 InstructionHandle insertHandle = 121 il.insert(getStaticHandle, getStaticHandle.getInstruction().copy()); 122 il.insert( 123 getStaticHandle, 124 instFact.createGetStatic(LEVEL, level, LevelType)); 125 126 logger.info("Inserting INVOKE call to isLoggable"); 127 128 il.insert( 129 getStaticHandle, 130 instFact.createInvoke( 131 INTERFACE, 132 "isLoggable", 133 Type.BOOLEAN, 134 ARGUMENTS_ISENABLEDFOR, 135 Constants.INVOKEVIRTUAL)); 136 logger.info("Inserting IFEQ"); 137 il.insert(getStaticHandle, new IFEQ(invokeHandle.getNext())); 138 return insertHandle; 139 } 140 private static HashMap mapping = new HashMap (); 141 static { 142 mapping.put("finest", "FINEST"); 143 mapping.put("finer", "FINER"); 144 mapping.put("fine", "FINE"); 145 mapping.put("config", "CONFIG"); 146 mapping.put("info", "INFO"); 147 mapping.put("warning", "WARNING"); 148 mapping.put("severe", "SEVERE"); 149 mapping.put("entering", "FINER"); 150 mapping.put("exiting", "FINER"); 151 mapping.put("throwing", "FINER"); 152 } 153 public String getIsEnabled( 154 InvokeInstruction invokeInstruction, 155 ConstantPoolGen cp) { 156 return (String ) mapping.get(invokeInstruction.getMethodName(cp)); 157 } 158 159 public String getClassname() { 160 return INTERFACE; 161 } 162 165 public InstructionHandle insertEnter( 166 MethodGen orig, 167 InstructionList il, 168 InstructionHandle firstInstructionHandle, 169 ConstantPoolGen cp) { 170 logger.info("Inserting Enter code."); 171 InstructionHandle backup = il.insert( 172 firstInstructionHandle, 173 instFact.createGetStatic( 174 clazz.getClassName(), 175 loggerAttribute.getName(), 176 loggerAttribute.getType())); 177 178 179 il.insert(firstInstructionHandle, 180 new PUSH(cp, clazz.getClassName()) 181 ); 182 il.insert(firstInstructionHandle, 183 new PUSH(cp, getMethodRepr(orig)) 184 ); 185 186 187 il.insert( 188 firstInstructionHandle, 189 instFact.createInvoke( 190 INTERFACE, 191 "entering", 192 Type.VOID, 193 new Type[] {Type.STRING, Type.STRING}, 194 Constants.INVOKEVIRTUAL)); 195 196 return backup; 197 } 198 199 202 public InstructionHandle insertExit( 203 MethodGen orig, 204 InstructionList il, 205 InstructionHandle returnInstructionHandle, 206 ConstantPoolGen cp) { 207 logger.info("Inserting Exit code."); 208 InstructionHandle backup = il.insert( 209 returnInstructionHandle, 210 instFact.createGetStatic( 211 clazz.getClassName(), 212 loggerAttribute.getName(), 213 loggerAttribute.getType())); 214 215 216 217 il.insert(returnInstructionHandle, 218 new PUSH(cp, clazz.getClassName()) 219 ); 220 il.insert(returnInstructionHandle, 221 new PUSH(cp, getMethodRepr(orig)) 222 ); 223 224 il.insert( 225 returnInstructionHandle, 226 instFact.createInvoke( 227 INTERFACE, 228 "exiting", 229 Type.VOID, 230 new Type[] {Type.STRING, Type.STRING}, 231 Constants.INVOKEVIRTUAL)); 232 233 return backup; 234 } 235 236 } 237 | Popular Tags |