01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package org.terracotta.modules;
05:
06: import com.tc.asm.ClassAdapter;
07: import com.tc.asm.ClassVisitor;
08: import com.tc.asm.MethodAdapter;
09: import com.tc.asm.MethodVisitor;
10: import com.tc.asm.Opcodes;
11: import com.tc.object.bytecode.ByteCodeUtil;
12: import com.tc.object.bytecode.ClassAdapterFactory;
13: import com.tcclient.util.DSOUnsafe;
14:
15: import java.lang.reflect.Modifier;
16:
17: public class UnsafeAdapter extends ClassAdapter implements Opcodes,
18: ClassAdapterFactory {
19: private final static String UNSAFE_CLASS_SLASH = "sun/misc/Unsafe";
20: public final static String TC_UNSAFE_FIELD_NAME = ByteCodeUtil.TC_FIELD_PREFIX
21: + "theUnsafe";
22:
23: public UnsafeAdapter() {
24: super (null);
25: }
26:
27: private UnsafeAdapter(ClassVisitor cv, ClassLoader caller) {
28: super (cv);
29: }
30:
31: public ClassAdapter create(ClassVisitor visitor, ClassLoader loader) {
32: return new UnsafeAdapter(visitor, loader);
33: }
34:
35: public final void visit(int version, int access, String name,
36: String signature, String super Name, String[] interfaces) {
37: if (UNSAFE_CLASS_SLASH.equals(name)) {
38: access = ~Modifier.FINAL & access;
39: }
40:
41: super .visit(version, access, name, signature, super Name,
42: interfaces);
43: }
44:
45: public MethodVisitor visitMethod(int access, String name,
46: String desc, String signature, String[] exceptions) {
47: access = access & ~Modifier.FINAL;
48:
49: // DSOUnsafe.<init> will get verify error calling super() if cstr() is not accessible
50: if ("<init>".equals(name)) {
51: access = access & ~Modifier.PRIVATE;
52: access |= Modifier.PROTECTED;
53: }
54:
55: if ("<clinit>".equals(name)) {
56: return new UnsafeMethodAdapter(super .visitMethod(access,
57: name, desc, signature, exceptions));
58: } else {
59: return super .visitMethod(access, name, desc, signature,
60: exceptions);
61: }
62: }
63:
64: private class UnsafeMethodAdapter extends MethodAdapter implements
65: Opcodes {
66: public UnsafeMethodAdapter(MethodVisitor mv) {
67: super (mv);
68: }
69:
70: public void visitMethodInsn(int opcode, String className,
71: String methodName, String desc) {
72: if (Opcodes.INVOKESPECIAL == opcode
73: && UNSAFE_CLASS_SLASH.equals(className)
74: && "<init>".equals(methodName)) {
75: super .visitMethodInsn(opcode, DSOUnsafe.CLASS_SLASH,
76: methodName, desc);
77: } else {
78: super .visitMethodInsn(opcode, className, methodName,
79: desc);
80: }
81: }
82:
83: public void visitTypeInsn(int opcode, String desc) {
84: if (NEW == opcode && UNSAFE_CLASS_SLASH.equals(desc)) {
85: super.visitTypeInsn(NEW, DSOUnsafe.CLASS_SLASH);
86: } else {
87: super.visitTypeInsn(opcode, desc);
88: }
89: }
90: }
91: }
|