1 23 24 package org.objectweb.fractal.julia.asm; 25 26 import org.objectweb.asm.CodeVisitor; 27 import org.objectweb.asm.Constants; 28 import org.objectweb.asm.Label; 29 30 import java.lang.reflect.Method ; 31 import java.util.List ; 32 import java.util.ArrayList ; 33 34 79 80 public abstract class AbstractCodeGenerator implements Constants, CodeGenerator { 81 82 86 public int init (final InterceptorClassGenerator icg) { 87 return IN; 88 } 89 90 public void generateInitCode (final CodeVisitor cv) 91 throws ClassGenerationException 92 { 93 } 95 96 public CodeVisitor generateInterceptionCode ( 97 final Method m, 98 final CodeVisitor cv) throws ClassGenerationException 99 { 100 if (intercept(m)) { 101 return new SimpleInterceptorCodeAdapter(cv, m); 102 } else { 103 return cv; 104 } 105 } 106 107 public void generateCloneCode (final CodeVisitor cv) 108 throws ClassGenerationException 109 { 110 } 112 113 public void close () { 114 } 116 117 121 131 132 protected boolean intercept (final Method m) { 133 return true; 134 } 135 136 150 151 protected int getInterceptionType (final Method m) { 152 return InterceptorCodeAdapter.NORMAL; 153 } 154 155 164 165 protected int getInterceptionCodeFormals (final Method m) { 166 return 0; 167 } 168 169 181 182 protected abstract void generateInterceptionCodeBlock ( 183 final Method m, 184 final boolean pre, 185 final CodeVisitor cv, 186 final int formals); 187 188 198 public List getImplementedInterfaces() throws ClassGenerationException { 199 return new ArrayList (); 200 } 201 202 206 209 210 class SimpleInterceptorCodeAdapter extends InterceptorCodeAdapter { 211 212 215 216 private int interceptionType; 217 218 221 222 private Label tryLabel; 223 224 227 228 private Label catchLabel; 229 230 236 237 public SimpleInterceptorCodeAdapter (final CodeVisitor cv, final Method m) { 238 super( 243 cv, 244 m, 245 getInterceptionCodeFormals(m) + 246 (getInterceptionType(m) == FINALLY ? 2 : 0), 247 getInterceptionType(m)); 248 interceptionType = getInterceptionType(m); 250 if (interceptionType == FINALLY) { 251 tryLabel = new Label(); 252 catchLabel = new Label(); 253 generateInterceptionCodeBlock(m, true, cv, nbFormals + 2); 254 cv.visitLabel(tryLabel); 255 } else { 256 generateInterceptionCodeBlock(m, true, cv, nbFormals); 257 } 258 } 259 260 public void visitMaxs (final int maxStack, final int maxLocals) { 261 if (interceptionType == NORMAL) { 262 visitLabel(postBlockLabel); 263 generateInterceptionCodeBlock(m, false, cv, nbFormals); 265 generateReturnCode(); 267 } else if (interceptionType == FINALLY) { 268 cv.visitLabel(catchLabel); 270 cv.visitVarInsn(ASTORE, nbFormals + 1); 271 cv.visitJumpInsn(JSR, postBlockLabel); 272 cv.visitVarInsn(ALOAD, nbFormals + 1); 273 cv.visitInsn(ATHROW); 274 cv.visitLabel(postBlockLabel); 275 cv.visitVarInsn(ASTORE, nbFormals); 276 generateInterceptionCodeBlock(m, false, cv, nbFormals + 2); 278 cv.visitVarInsn(RET, nbFormals); 280 cv.visitTryCatchBlock(tryLabel, catchLabel, catchLabel, null); 282 } 283 cv.visitMaxs(maxStack, maxLocals); 285 } 286 } 287 } 288 | Popular Tags |