1 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 ; 14 15 public class UnsafeAdapter extends ClassAdapter implements Opcodes, ClassAdapterFactory { 16 private final static String UNSAFE_CLASS_SLASH = "sun/misc/Unsafe"; 17 public final static String TC_UNSAFE_FIELD_NAME = ByteCodeUtil.TC_FIELD_PREFIX + "theUnsafe"; 18 19 public UnsafeAdapter() { 20 super(null); 21 } 22 23 private UnsafeAdapter(ClassVisitor cv, ClassLoader caller) { 24 super(cv); 25 } 26 27 public ClassAdapter create(ClassVisitor visitor, ClassLoader loader) { 28 return new UnsafeAdapter(visitor, loader); 29 } 30 31 public final void visit(int version, int access, String name, String signature, String superName, String [] 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 name, String desc, String signature, String [] 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 className, String methodName, String 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 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 |