KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.tcclient.util.DSOUnsafe;
12
13 import java.lang.reflect.Modifier JavaDoc;
14
15 public class UnsafeAdapter extends ClassAdapter implements Opcodes, ClassAdapterFactory {
16   private final static String JavaDoc UNSAFE_CLASS_SLASH = "sun/misc/Unsafe";
17   public final static String JavaDoc TC_UNSAFE_FIELD_NAME = ByteCodeUtil.TC_FIELD_PREFIX + "theUnsafe";
18
19   public UnsafeAdapter() {
20     super(null);
21   }
22   
23   private UnsafeAdapter(ClassVisitor cv, ClassLoader JavaDoc caller) {
24     super(cv);
25   }
26   
27   public ClassAdapter create(ClassVisitor visitor, ClassLoader JavaDoc loader) {
28     return new UnsafeAdapter(visitor, loader);
29   }
30
31   public final void visit(int version, int access, String JavaDoc name, String JavaDoc signature, String JavaDoc superName, String JavaDoc[] interfaces) {
32     if (UNSAFE_CLASS_SLASH.equals(name)) {
33       access = ~Modifier.FINAL & access;
34     }
35
36     super.visit(version, access, name, signature, superName, interfaces);
37   }
38
39   public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
40     access = access & ~Modifier.FINAL;
41     if ("<clinit>".equals(name)) {
42       return new UnsafeMethodAdapter(super.visitMethod(access, name, desc, signature, exceptions));
43     } else {
44       return super.visitMethod(access, name, desc, signature, exceptions);
45     }
46   }
47
48   private class UnsafeMethodAdapter extends MethodAdapter implements Opcodes {
49     public UnsafeMethodAdapter(MethodVisitor mv) {
50       super(mv);
51     }
52
53     public void visitMethodInsn(int opcode, String JavaDoc className, String JavaDoc methodName, String JavaDoc desc) {
54       if (Opcodes.INVOKESPECIAL == opcode && UNSAFE_CLASS_SLASH.equals(className) && "<init>".equals(methodName)) {
55         super.visitMethodInsn(opcode, DSOUnsafe.CLASS_SLASH, methodName, desc);
56       } else {
57         super.visitMethodInsn(opcode, className, methodName, desc);
58       }
59     }
60
61     public void visitTypeInsn(int opcode, String JavaDoc desc) {
62       if (NEW == opcode && UNSAFE_CLASS_SLASH.equals(desc)) {
63         super.visitTypeInsn(NEW, DSOUnsafe.CLASS_SLASH);
64       } else {
65         super.visitTypeInsn(opcode, desc);
66       }
67     }
68   }
69 }
70
Popular Tags