1 5 package com.tc.weblogic.transform; 6 7 import com.tc.asm.ClassAdapter; 8 import com.tc.asm.ClassVisitor; 9 import com.tc.asm.Label; 10 import com.tc.asm.MethodVisitor; 11 import com.tc.asm.Opcodes; 12 import com.tc.object.bytecode.ByteCodeUtil; 13 import com.tc.object.bytecode.ClassAdapterFactory; 14 15 import java.lang.reflect.Modifier ; 16 17 public class ServerAdapter extends ClassAdapter implements Opcodes, ClassAdapterFactory { 18 19 public ServerAdapter() { 20 super(null); 21 } 22 23 private ServerAdapter(ClassVisitor cv, ClassLoader caller) { 24 super(cv); 25 } 26 27 public ClassAdapter create(ClassVisitor visitor, ClassLoader loader) { 28 return new ServerAdapter(visitor, loader); 29 } 30 31 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String [] exceptions) { 32 if ("main".equals(name) && "([Ljava/lang/String;)V".equals(desc) && Modifier.isStatic(access) 33 && Modifier.isPublic(access)) { 34 name = ByteCodeUtil.TC_METHOD_PREFIX + name; 35 } 36 37 return super.visitMethod(access, name, desc, signature, exceptions); 38 } 39 40 public void visitEnd() { 41 addMainWrapper(); 42 super.visitEnd(); 43 } 44 45 private void addMainWrapper() { 46 MethodVisitor mv = super.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); 47 mv.visitCode(); 48 49 mv.visitLdcInsn("weblogic.Server"); 50 mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;"); 51 mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getClassLoader", "()Ljava/lang/ClassLoader;"); 52 mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/hook/impl/SessionsHelper", "injectClasses", 53 "(Ljava/lang/ClassLoader;)V"); 54 55 Label l1 = new Label(); 56 mv.visitLabel(l1); 57 mv.visitVarInsn(ALOAD, 0); 58 mv.visitMethodInsn(INVOKESTATIC, "weblogic/Server", ByteCodeUtil.TC_METHOD_PREFIX + "main", 59 "([Ljava/lang/String;)V"); 60 Label l2 = new Label(); 61 mv.visitJumpInsn(GOTO, l2); 62 Label l3 = new Label(); 63 mv.visitLabel(l3); 64 mv.visitVarInsn(ASTORE, 2); 65 Label l4 = new Label(); 66 mv.visitJumpInsn(JSR, l4); 67 mv.visitVarInsn(ALOAD, 2); 68 mv.visitInsn(ATHROW); 69 mv.visitLabel(l4); 70 mv.visitVarInsn(ASTORE, 1); 71 Label l6 = new Label(); 72 mv.visitLabel(l6); 73 mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/hook/impl/ClassProcessorHelper", "shutdown", "()V"); 74 Label l7 = new Label(); 75 mv.visitJumpInsn(GOTO, l7); 76 Label l8 = new Label(); 77 mv.visitLabel(l8); 78 mv.visitVarInsn(ASTORE, 3); 79 mv.visitVarInsn(ALOAD, 3); 80 mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Throwable", "printStackTrace", "()V"); 81 mv.visitLabel(l7); 82 mv.visitVarInsn(RET, 1); 83 mv.visitLabel(l2); 84 mv.visitJumpInsn(JSR, l4); 85 Label l10 = new Label(); 86 mv.visitLabel(l10); 87 mv.visitInsn(RETURN); 88 mv.visitTryCatchBlock(l1, l3, l3, null); 89 mv.visitTryCatchBlock(l2, l10, l3, null); 90 mv.visitTryCatchBlock(l6, l8, l8, "java/lang/Throwable"); 91 mv.visitMaxs(0, 0); 92 mv.visitEnd(); 93 } 94 95 } 96 | Popular Tags |