KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.tc.asm.ClassAdapter;
7 import com.tc.asm.ClassVisitor;
8 import com.tc.asm.MethodAdapter;
9 import com.tc.asm.MethodVisitor;
10 import com.tc.asm.Opcodes;
11
12 import java.lang.reflect.Modifier JavaDoc;
13
14 public class JavaUtilTreeMapAdapter extends ClassAdapter {
15
16   public JavaUtilTreeMapAdapter(ClassVisitor cv) {
17     super(cv);
18   }
19
20   public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
21     MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
22     if ("writeObject".equals(name) && Modifier.isPrivate(access)) { return new WriteObjectAdapter(mv); }
23
24     return mv;
25   }
26
27   private static class WriteObjectAdapter extends MethodAdapter implements Opcodes {
28
29     public WriteObjectAdapter(MethodVisitor mv) {
30       super(mv);
31     }
32
33     public void visitFieldInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
34       if (opcode == GETFIELD) {
35         if ("java/util/TreeMap$Entry".equals(owner)) {
36           if ("key".equals(name)) {
37             mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map$Entry", "getKey", "()Ljava/lang/Object;");
38           } else if ("value".equals(name)) {
39             mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map$Entry", "getValue", "()Ljava/lang/Object;");
40           } else {
41             throw new AssertionError JavaDoc("unknown field name: " + name);
42           }
43           return;
44         }
45       }
46
47       super.visitFieldInsn(opcode, owner, name, desc);
48     }
49
50     public void visitTypeInsn(int opcode, String JavaDoc desc) {
51       if (CHECKCAST == opcode) {
52         if ("java/util/TreeMap$Entry".equals(desc)) {
53           super.visitTypeInsn(opcode, "java/util/Map$Entry");
54           return;
55         }
56       }
57
58       super.visitTypeInsn(opcode, desc);
59     }
60
61   }
62
63 }
64
Popular Tags