1 4 package com.tc.tomcat.transform; 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.tc.object.bytecode.ClassAdapterFactory; 12 import com.tc.object.loaders.NamedClassLoader; 13 14 18 public class BootstrapAdapter extends ClassAdapter implements ClassAdapterFactory { 19 20 public BootstrapAdapter() { 21 super(null); 22 } 23 24 private BootstrapAdapter(ClassVisitor cv, ClassLoader caller) { 25 super(cv); 26 } 27 28 public ClassAdapter create(ClassVisitor visitor, ClassLoader loader) { 29 return new BootstrapAdapter(visitor, loader); 30 } 31 32 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String [] exceptions) { 33 MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); 34 35 if ("initClassLoaders".equals(name)) { 36 mv = new InitClassLoadersAdatper(mv); 37 } 38 39 return mv; 40 } 41 42 private static class InitClassLoadersAdatper extends MethodAdapter implements Opcodes { 43 44 public InitClassLoadersAdatper(MethodVisitor mv) { 45 super(mv); 46 } 47 48 private void nameAndRegisterLoader(String fieldName, String loaderName) { 49 mv.visitVarInsn(ALOAD, 0); 50 mv.visitFieldInsn(GETFIELD, "org/apache/catalina/startup/Bootstrap", fieldName, "Ljava/lang/ClassLoader;"); 51 mv.visitTypeInsn(CHECKCAST, NamedClassLoader.CLASS); 52 mv.visitInsn(DUP); 53 mv.visitFieldInsn(GETSTATIC, "com/tc/object/loaders/Namespace", "TOMCAT_NAMESPACE", "Ljava/lang/String;"); 54 mv.visitLdcInsn(loaderName); 55 mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/loaders/Namespace", "createLoaderName", 56 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"); 57 mv.visitMethodInsn(INVOKEINTERFACE, NamedClassLoader.CLASS, "__tc_setClassLoaderName", "(Ljava/lang/String;)V"); 58 mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/hook/impl/ClassProcessorHelper", "registerGlobalLoader", 59 "(" + NamedClassLoader.TYPE + ")V"); 60 } 61 62 public void visitInsn(int opcode) { 63 if (opcode == RETURN) { 64 nameAndRegisterLoader("commonLoader", "common"); 65 nameAndRegisterLoader("catalinaLoader", "catalina"); 66 nameAndRegisterLoader("sharedLoader", "shared"); 67 } 68 super.visitInsn(opcode); 69 } 70 } 71 72 } 73 | Popular Tags |