KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > bytecode > DateMethodAdapter


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.bytecode;
5
6
7 import com.tc.asm.ClassVisitor;
8 import com.tc.asm.Label;
9 import com.tc.asm.MethodAdapter;
10 import com.tc.asm.MethodVisitor;
11 import com.tc.asm.Opcodes;
12 import com.tc.asm.Type;
13 import com.tc.object.SerializationUtil;
14 import com.tc.object.config.MethodSpec;
15
16 public class DateMethodAdapter extends LogicalMethodAdapter {
17
18   public DateMethodAdapter() {
19     super();
20   }
21
22   public DateMethodAdapter(String JavaDoc methodName, int instrumentationType) {
23     super(methodName, instrumentationType);
24   }
25
26   public MethodVisitor adapt(ClassVisitor classVisitor) {
27     MethodVisitor mv = super.adapt(classVisitor);
28
29     if (getOwnerSlashes().equals("java/sql/Timestamp")) { return new TimestampMethodAdapter(mv); }
30     return mv;
31   }
32
33   protected void createWrapperMethod(ClassVisitor classVisitor) {
34     switch (getInstrumentationType()) {
35       case MethodSpec.DATE_ADD_SET_TIME_WRAPPER_LOG:
36         addSetTimeMethodWrapper(classVisitor);
37         break;
38       case MethodSpec.TIMESTAMP_SET_TIME_METHOD_WRAPPER_LOG:
39         addTimestampSetTimeMethodWrapper(classVisitor);
40         break;
41       default:
42         super.createWrapperMethod(classVisitor);
43     }
44   }
45
46   private class TimestampMethodAdapter extends MethodAdapter implements Opcodes {
47     public TimestampMethodAdapter(MethodVisitor mv) {
48       super(mv);
49     }
50
51     public void visitMethodInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
52       if ((opcode == INVOKESPECIAL) && ("setTime".equals(name))) {
53         super.visitMethodInsn(opcode, owner, ByteCodeUtil.TC_METHOD_PREFIX + name, desc);
54       } else {
55         super.visitMethodInsn(opcode, owner, name, desc);
56       }
57     }
58   }
59
60   private void addTimestampSetTimeMethodWrapper(ClassVisitor classVisitor) {
61     MethodVisitor mv = classVisitor.visitMethod(getWrapperAccess(), getMethodName(), getDescription(), getSignature(), getExceptions());
62     addCheckWriteAccessInstrumentedCode(mv, true);
63     Label l0 = new Label();
64     mv.visitLabel(l0);
65     ByteCodeUtil.pushThis(mv);
66     Type[] params = Type.getArgumentTypes(getDescription());
67     Type returnType = Type.getReturnType(getDescription());
68     for (int i = 0; i < params.length; i++) {
69       mv.visitVarInsn(params[i].getOpcode(ILOAD), i + 1);
70     }
71
72     mv.visitMethodInsn(INVOKESPECIAL, getOwnerSlashes(), getNewName(), getDescription());
73     if (!returnType.equals(Type.VOID_TYPE)) {
74       mv.visitVarInsn(returnType.getOpcode(ISTORE), params.length + 1);
75     }
76     ByteCodeUtil.pushThis(mv);
77     mv.visitLdcInsn(getMethodName() + getDescription());
78
79     ByteCodeUtil.createParametersToArrayByteCode(mv, params);
80     getManagerHelper().callManagerMethod("logicalInvoke", mv);
81
82     ByteCodeUtil.pushThis(mv);
83     mv.visitLdcInsn("setNanos(I)V");
84     mv.visitInsn(ICONST_1);
85     mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
86     mv.visitInsn(DUP);
87     mv.visitInsn(ICONST_0);
88     mv.visitTypeInsn(NEW, "java/lang/Integer");
89     mv.visitInsn(DUP);
90     ByteCodeUtil.pushThis(mv);
91     mv.visitMethodInsn(INVOKESPECIAL, getOwnerSlashes(), "getNanos", "()I");
92     mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Integer", "<init>", "(I)V");
93     mv.visitInsn(AASTORE);
94     getManagerHelper().callManagerMethod("logicalInvoke", mv);
95
96     if (!returnType.equals(Type.VOID_TYPE)) {
97       mv.visitVarInsn(returnType.getOpcode(ILOAD), params.length + 1);
98     }
99     mv.visitInsn(returnType.getOpcode(IRETURN));
100     mv.visitMaxs(0, 0);
101
102   }
103
104   private void addSetTimeMethodWrapper(ClassVisitor classVisitor) {
105     MethodVisitor mv = classVisitor.visitMethod(getWrapperAccess(), getMethodName(), getDescription(), getSignature(),
106                                                 getExceptions());
107     addCheckWriteAccessInstrumentedCode(mv, true);
108     Label l0 = new Label();
109     mv.visitLabel(l0);
110     ByteCodeUtil.pushThis(mv);
111     Type[] params = Type.getArgumentTypes(getDescription());
112     Type returnType = Type.getReturnType(getDescription());
113     for (int i = 0; i < params.length; i++) {
114       mv.visitVarInsn(params[i].getOpcode(ILOAD), i + 1);
115     }
116
117     mv.visitMethodInsn(INVOKESPECIAL, getOwnerSlashes(), getNewName(), getDescription());
118
119     addSetTimeInstrumentedCode(mv, params.length + 1);
120
121     mv.visitInsn(returnType.getOpcode(IRETURN));
122     mv.visitMaxs(0, 0);
123   }
124
125   private void addSetTimeInstrumentedCode(MethodVisitor mv, int variableOffset) {
126     String JavaDoc getTimeDescription = "()J";
127     Type getTimeReturnType = Type.getReturnType(getTimeDescription);
128     ByteCodeUtil.pushThis(mv);
129     mv.visitMethodInsn(INVOKESPECIAL, getOwnerSlashes(), "getTime", getTimeDescription);
130     mv.visitVarInsn(getTimeReturnType.getOpcode(ISTORE), variableOffset);
131
132     String JavaDoc setTimeDescription = "(J)V";
133     Type[] setTimeParams = Type.getArgumentTypes(setTimeDescription);
134
135     ByteCodeUtil.pushThis(mv);
136     mv.visitLdcInsn(SerializationUtil.SET_TIME_SIGNATURE);
137
138     ByteCodeUtil.createParametersToArrayByteCode(mv, setTimeParams, variableOffset);
139
140     getManagerHelper().callManagerMethod("logicalInvoke", mv);
141   }
142
143 }
144
Popular Tags