View Javadoc

1   /*
2    *
3    */
4   package org.softevo.jdynpur.runtime;
5   
6   import org.objectweb.asm.Label;
7   import org.objectweb.asm.MethodVisitor;
8   import org.objectweb.asm.Opcodes;
9   import org.objectweb.asm.commons.LocalVariablesSorter;
10  
11  public class MethodStartEndTracingVisitor extends LocalVariablesSorter implements Opcodes {
12  
13  	private Label methodStartLabel;
14  
15  	private Label methodEndLabel;
16  
17  	private Label exceptionHandlerStartLabel;
18  
19  	private int identifier;
20  
21  	private int access;
22  
23  	private String name;
24  	
25  	public MethodStartEndTracingVisitor(MethodVisitor visitor, int access, String className, 
26  	                                    String name, String desc, int identifier) {
27  		super(access, desc, visitor);
28  		this.identifier = identifier;
29  		this.access = access;
30  		this.name = name;
31  	}
32  
33  	public void visitInsn(int opcode) {
34  		if ((opcode == ARETURN) || (opcode == IRETURN) || (opcode == DRETURN) || (opcode == LRETURN) || (opcode == FRETURN)
35  				|| (opcode == RETURN)) {
36  			super.visitLdcInsn(new Integer(identifier));
37  			if (((access & ACC_STATIC) == 0) && !(name.equals("<init>"))) {
38  				super.visitMethodInsn(INVOKESTATIC, OnTheFlyInstrumenter.TRACECLASSNAME, "dynamicMethodEnded", "(I)V");
39  			} else {
40  				super.visitMethodInsn(INVOKESTATIC, OnTheFlyInstrumenter.TRACECLASSNAME, "staticMethodEnded", "(I)V");
41  			}
42  		}
43  		super.visitInsn(opcode);
44  	}
45  
46  	@Override
47  	public void visitCode() {
48  		super.visitCode();
49  		methodStartLabel = new Label();
50  		super.visitLabel(methodStartLabel);
51  		
52  		/*
53  		super.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
54  		super.visitLdcInsn(className + "." + name + descriptor);
55  		super.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
56  		 */
57  		super.visitLdcInsn(new Integer(identifier));
58  		if (((access & ACC_STATIC) == 0) && !(name.equals("<init>"))) {
59  			super.visitVarInsn(ALOAD, 0);
60  			super.visitMethodInsn(INVOKESTATIC, OnTheFlyInstrumenter.TRACECLASSNAME, "dynamicMethodStarted",
61  														"(ILjava/lang/Object;)V");
62  		} else {
63  			super.visitMethodInsn(INVOKESTATIC, OnTheFlyInstrumenter.TRACECLASSNAME, "staticMethodStarted", "(I)V");
64  		}
65  	}
66  
67  	public void visitEnd() {
68  		int exceptionVariableIndex = -1;
69  
70  		methodEndLabel = new Label();
71  		exceptionHandlerStartLabel = new Label();
72  		super.visitLabel(methodEndLabel);
73  		super.visitLabel(exceptionHandlerStartLabel);
74  		exceptionVariableIndex = newLocal(1);
75  		super.visitVarInsn(ASTORE, exceptionVariableIndex);
76  		super.visitLdcInsn(new Integer(identifier));
77  		if (((access & ACC_STATIC) == 0) && !(name.equals("<init>"))) {
78  			super.visitMethodInsn(INVOKESTATIC, OnTheFlyInstrumenter.TRACECLASSNAME, "dynamicMethodEnded", "(I)V");
79  		} else {
80  			super.visitMethodInsn(INVOKESTATIC, OnTheFlyInstrumenter.TRACECLASSNAME, "staticMethodEnded", "(I)V");
81  		}
82  		super.visitVarInsn(ALOAD, exceptionVariableIndex);
83  		super.visitInsn(ATHROW);
84  		super.visitTryCatchBlock(methodStartLabel, methodEndLabel, exceptionHandlerStartLabel, null);
85  		super.visitEnd();
86  	}
87  
88  }