Java中的cglib原理解析

 更新时间:2023年10月23日 09:46:58   作者:bluespacezero  
这篇文章主要介绍了Java中的cglib原理解析,由于代理类继承了被代理类,所以调用sayHello()方法时会直接调用代理类的sayHello()方法,而在代理类的方法中,调用了Callback的逻辑,需要的朋友可以参考下

cglib原理解析

先放上示例代码:

//Person.java
public class Person {
    public void sayHello() {
        System.out.println("Hello!");
    }
}

//Test.java
import org.objectweb.asm.ClassWriter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.io.FileOutputStream;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws Exception {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Person.class);
        //设置生成的类不继承Factory接口
        enhancer.setUseFactory(false);
        enhancer.setCallback(new MethodInterceptor() {
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("Intercept!");
                methodProxy.invokeSuper(o, objects);
                return null;
            }
        });

        Person person = (Person) enhancer.create();
        person.sayHello();

        //以下代码是为了输出cglib生成的类的class文件
        ClassWriter cw = new ClassWriter(0);
        enhancer.generateClass(cw);
        byte[] klass = cw.toByteArray();
        FileOutputStream fileOutputStream = new FileOutputStream(person.getClass().getName()+".class");
        fileOutputStream.write(klass);
        fileOutputStream.close();
    }
}

使用cglib生成代码的过程很简单:

  • new 一个Enhancer对象
  • 调用它的setSuperclass()方法设置需要代理的类
  • 调用它的setCallback()方法设置拦截被代理类方法时的逻辑

为了简单起见,我们还调用了setUseFactory(false)方法,使得生成的代理类不实现Factory接口(该接口提供了一些生成新对象的和设置对象的Callback的方法)。并且没有使用CallbackFilter。

cglib生成对象时主要的逻辑在Enchancer类的generateClass方法里面:

public void generateClass(ClassVisitor v) throws Exception {
        Class sc = (superclass == null) ? Object.class : superclass;

        //如果被代理类是final类型的,抛出异常
        if (TypeUtils.isFinal(sc.getModifiers()))
            throw new IllegalArgumentException("Cannot subclass final class " + sc.getName());
        //以下两行代码获得被代理类的非私有构造函数
        List constructors = new ArrayList(Arrays.asList(sc.getDeclaredConstructors()));
        filterConstructors(sc, constructors);

        // Order is very important: must add superclass, then
        // its superclass chain, then each interface and
        // its superinterfaces.
        List actualMethods = new ArrayList();
        List interfaceMethods = new ArrayList();
        final Set forcePublic = new HashSet();
        //得到被代理类及其父类、接口的非final、非static、非private方法。这些方法是需要代理的(在不考虑CallbackFilter的情况下)
        getMethods(sc, interfaces, actualMethods, interfaceMethods, forcePublic);

        //获得需要代理的方法的方法信息
        List methods = CollectionUtils.transform(actualMethods, new Transformer() {
            public Object transform(Object value) {
                Method method = (Method)value;
                int modifiers = Constants.ACC_FINAL
                    | (method.getModifiers()
                       & ~Constants.ACC_ABSTRACT
                       & ~Constants.ACC_NATIVE
                       & ~Constants.ACC_SYNCHRONIZED);
                if (forcePublic.contains(MethodWrapper.create(method))) {
                    modifiers = (modifiers & ~Constants.ACC_PROTECTED) | Constants.ACC_PUBLIC;
                }
                return ReflectUtils.getMethodInfo(method, modifiers);
            }
        });

        ClassEmitter e = new ClassEmitter(v);
        if (currentData == null) {
            //生成class文件的版本号、类的访问描述符、类名、父类名、接口,并生成SourceFile属性
            e.begin_class(Constants.V1_2,
                      Constants.ACC_PUBLIC,
                      getClassName(),
                      Type.getType(sc),
                      (useFactory ?
                       TypeUtils.add(TypeUtils.getTypes(interfaces), FACTORY) :
                       TypeUtils.getTypes(interfaces)),
                      Constants.SOURCE_FILE);
        } else {
            e.begin_class(Constants.V1_2,
                    Constants.ACC_PUBLIC,
                    getClassName(),
                    null,
                    new Type[]{FACTORY},
                    Constants.SOURCE_FILE);
        }
        List constructorInfo = CollectionUtils.transform(constructors, MethodInfoTransformer.getInstance());

        //以下生成代理类的字段,可以对照下面的反编译结果观看
        //生成private boolean CGLIB$BOUND字段
        e.declare_field(Constants.ACC_PRIVATE, BOUND_FIELD, Type.BOOLEAN_TYPE, null);
        //生成public static Object CGLIB$FACTORY_DATA字段
        e.declare_field(Constants.ACC_PUBLIC | Constants.ACC_STATIC, FACTORY_DATA_FIELD, OBJECT_TYPE, null);
        if (!interceptDuringConstruction) {
            e.declare_field(Constants.ACC_PRIVATE, CONSTRUCTED_FIELD, Type.BOOLEAN_TYPE, null);
        }
        //生成private static final ThreadLocal CGLIB$THREAD_CALLBACKS字段
        e.declare_field(Constants.PRIVATE_FINAL_STATIC, THREAD_CALLBACKS_FIELD, THREAD_LOCAL, null);
        //生成private static final Callback[] CGLIB$STATIC_CALLBACKS字段
        e.declare_field(Constants.PRIVATE_FINAL_STATIC, STATIC_CALLBACKS_FIELD, CALLBACK_ARRAY, null);
        if (serialVersionUID != null) {
            e.declare_field(Constants.PRIVATE_FINAL_STATIC, Constants.SUID_FIELD_NAME, Type.LONG_TYPE, serialVersionUID);
        }

        //以下是生成Callback字段,可以看到,对应每个设置的Callback,都会在代理类中生成一个对应具体类型的字段
        for (int i = 0; i < callbackTypes.length; i++) {
            //在本例中生成的是private MethodInterceptor CGLIB$CALLBACK_0字段,后缀_0代表是在Enhancer中设置的第0个Callback
            e.declare_field(Constants.ACC_PRIVATE, getCallbackField(i), callbackTypes[i], null);
        }
        // This is declared private to avoid "public field" pollution
        //生成private static Object CGLIB$CALLBACK_FILTER字段。如果Enhancer只设置了一个Callback并且没有设置CallbackFilter,那么会默认对该Enhancer对象添加一个指向第0个Callback的CallbackFilter
        e.declare_field(Constants.ACC_PRIVATE | Constants.ACC_STATIC, CALLBACK_FILTER_FIELD, OBJECT_TYPE, null);

        if (currentData == null) {
            //这个函数生成需要代理类的方法
            emitMethods(e, methods, actualMethods);
            //生成构造函数
            emitConstructors(e, constructorInfo);
        } else {
            emitDefaultConstructor(e);
        }
        //以下三行代码分别生成代理类的CGLIB$SET_THREAD_CALLBACKS、CGLIB$SET_STATIC_CALLBACKS和CGLIB$BIND_CALLBACKS函数
        emitSetThreadCallbacks(e);
        emitSetStaticCallbacks(e);
        emitBindCallbacks(e);

        if (useFactory || currentData != null) {
            int[] keys = getCallbackKeys();
            //如果useFactory为true就会生成几个newInstance方法,还有几个操作代理类的Callback的方法
            emitNewInstanceCallbacks(e);
            emitNewInstanceCallback(e);
            emitNewInstanceMultiarg(e, constructorInfo);
            emitGetCallback(e, keys);
            emitSetCallback(e, keys);
            emitGetCallbacks(e);
            emitSetCallbacks(e);
        }

        e.end_class();
    }

使用Bytecode Viewer的CFR反编译生成的代理类如下:

/*
 * Decompiled with CFR 0_125.
 * 
 * Could not load the following classes:
 *  Person
 *  Person$$EnhancerByCGLIB$$34b905e4
 *  net.sf.cglib.core.ReflectUtils
 *  net.sf.cglib.core.Signature
 *  net.sf.cglib.proxy.Callback
 *  net.sf.cglib.proxy.MethodInterceptor
 *  net.sf.cglib.proxy.MethodProxy
 */
import java.lang.reflect.Method;
import net.sf.cglib.core.ReflectUtils;
import net.sf.cglib.core.Signature;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
/*
 * Exception performing whole class analysis ignored.
 */
public class Person$$EnhancerByCGLIB$$34b905e4
extends Person {
    private boolean CGLIB$BOUND;
    public static Object CGLIB$FACTORY_DATA;
    private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
    private static final Callback[] CGLIB$STATIC_CALLBACKS;
    private MethodInterceptor CGLIB$CALLBACK_0;
    private static Object CGLIB$CALLBACK_FILTER;
    private static final Method CGLIB$sayHello$0$Method;
    private static final MethodProxy CGLIB$sayHello$0$Proxy;
    private static final Object[] CGLIB$emptyArgs;
    private static final Method CGLIB$equals$1$Method;
    private static final MethodProxy CGLIB$equals$1$Proxy;
    private static final Method CGLIB$toString$2$Method;
    private static final MethodProxy CGLIB$toString$2$Proxy;
    private static final Method CGLIB$hashCode$3$Method;
    private static final MethodProxy CGLIB$hashCode$3$Proxy;
    private static final Method CGLIB$clone$4$Method;
    private static final MethodProxy CGLIB$clone$4$Proxy;
    static void CGLIB$STATICHOOK2() {
        CGLIB$THREAD_CALLBACKS = new ThreadLocal();
        CGLIB$emptyArgs = new Object[0];
        Class class_ = Class.forName("Person$$EnhancerByCGLIB$$34b905e4");
        Class class_2 = Class.forName("java.lang.Object");
        Method[] arrmethod = ReflectUtils.findMethods((String[])new String[]{"equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (Method[])class_2.getDeclaredMethods());
        CGLIB$equals$1$Method = arrmethod[0];
        CGLIB$equals$1$Proxy = MethodProxy.create(class_2, class_, (String)"(Ljava/lang/Object;)Z", (String)"equals", (String)"CGLIB$equals$1");
        CGLIB$toString$2$Method = arrmethod[1];
        CGLIB$toString$2$Proxy = MethodProxy.create(class_2, class_, (String)"()Ljava/lang/String;", (String)"toString", (String)"CGLIB$toString$2");
        CGLIB$hashCode$3$Method = arrmethod[2];
        CGLIB$hashCode$3$Proxy = MethodProxy.create(class_2, class_, (String)"()I", (String)"hashCode", (String)"CGLIB$hashCode$3");
        CGLIB$clone$4$Method = arrmethod[3];
        CGLIB$clone$4$Proxy = MethodProxy.create(class_2, class_, (String)"()Ljava/lang/Object;", (String)"clone", (String)"CGLIB$clone$4");
        class_2 = Class.forName("Person");
        CGLIB$sayHello$0$Method = ReflectUtils.findMethods((String[])new String[]{"sayHello", "()V"}, (Method[])class_2.getDeclaredMethods())[0];
        CGLIB$sayHello$0$Proxy = MethodProxy.create(class_2, class_, (String)"()V", (String)"sayHello", (String)"CGLIB$sayHello$0");
    }
    final void CGLIB$sayHello$0() {
        super.sayHello();
    }
    public final void sayHello() {
        MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
        if (methodInterceptor == null) {
            Person$$EnhancerByCGLIB$$34b905e4.CGLIB$BIND_CALLBACKS((Object)this);
            methodInterceptor = this.CGLIB$CALLBACK_0;
        }
        if (methodInterceptor != null) {
            Object object = methodInterceptor.intercept((Object)this, CGLIB$sayHello$0$Method, CGLIB$emptyArgs, CGLIB$sayHello$0$Proxy);
            return;
        }
        super.sayHello();
    }
    final boolean CGLIB$equals$1(Object object) {
        return super.equals(object);
    }
    public final boolean equals(Object object) {
        MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
        if (methodInterceptor == null) {
            Person$$EnhancerByCGLIB$$34b905e4.CGLIB$BIND_CALLBACKS((Object)this);
            methodInterceptor = this.CGLIB$CALLBACK_0;
        }
        if (methodInterceptor == null) return super.equals(object);
        Object object2 = methodInterceptor.intercept((Object)this, CGLIB$equals$1$Method, new Object[]{object}, CGLIB$equals$1$Proxy);
        if (object2 == null) {
            return false;
        }
        boolean bl = (Boolean)object2;
        return bl;
    }
    final String CGLIB$toString$2() {
        return super.toString();
    }
    public final String toString() {
        MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
        if (methodInterceptor == null) {
            Person$$EnhancerByCGLIB$$34b905e4.CGLIB$BIND_CALLBACKS((Object)this);
            methodInterceptor = this.CGLIB$CALLBACK_0;
        }
        if (methodInterceptor == null) return super.toString();
        return (String)methodInterceptor.intercept((Object)this, CGLIB$toString$2$Method, CGLIB$emptyArgs, CGLIB$toString$2$Proxy);
    }
    final int CGLIB$hashCode$3() {
        return super.hashCode();
    }
    public final int hashCode() {
        MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
        if (methodInterceptor == null) {
            Person$$EnhancerByCGLIB$$34b905e4.CGLIB$BIND_CALLBACKS((Object)this);
            methodInterceptor = this.CGLIB$CALLBACK_0;
        }
        if (methodInterceptor == null) return super.hashCode();
        Object object = methodInterceptor.intercept((Object)this, CGLIB$hashCode$3$Method, CGLIB$emptyArgs, CGLIB$hashCode$3$Proxy);
        if (object == null) {
            return 0;
        }
        int n = ((Number)object).intValue();
        return n;
    }
    final Object CGLIB$clone$4() throws CloneNotSupportedException {
        return super.clone();
    }
    protected final Object clone() throws CloneNotSupportedException {
        MethodInterceptor methodInterceptor = this.CGLIB$CALLBACK_0;
        if (methodInterceptor == null) {
            Person$$EnhancerByCGLIB$$34b905e4.CGLIB$BIND_CALLBACKS((Object)this);
            methodInterceptor = this.CGLIB$CALLBACK_0;
        }
        if (methodInterceptor == null) return super.clone();
        return methodInterceptor.intercept((Object)this, CGLIB$clone$4$Method, CGLIB$emptyArgs, CGLIB$clone$4$Proxy);
    }
    public static MethodProxy CGLIB$findMethodProxy(Signature signature) {
        String string = signature.toString();
        switch (string.hashCode()) {
            case -508378822: {
                if (!string.equals("clone()Ljava/lang/Object;")) return null;
                return CGLIB$clone$4$Proxy;
            }
            case 1535311470: {
                if (!string.equals("sayHello()V")) return null;
                return CGLIB$sayHello$0$Proxy;
            }
            case 1826985398: {
                if (!string.equals("equals(Ljava/lang/Object;)Z")) return null;
                return CGLIB$equals$1$Proxy;
            }
            case 1913648695: {
                if (!string.equals("toString()Ljava/lang/String;")) return null;
                return CGLIB$toString$2$Proxy;
            }
            case 1984935277: {
                if (!string.equals("hashCode()I")) return null;
                return CGLIB$hashCode$3$Proxy;
            }
        }
        return null;
    }
    public Person$$EnhancerByCGLIB$$34b905e4() {
        Person$$EnhancerByCGLIB$$34b905e4 person$$EnhancerByCGLIB$$34b905e4 = this;
        Person$$EnhancerByCGLIB$$34b905e4.CGLIB$BIND_CALLBACKS((Object)person$$EnhancerByCGLIB$$34b905e4);
    }
    public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] arrcallback) {
        CGLIB$THREAD_CALLBACKS.set(arrcallback);
    }
    public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] arrcallback) {
        CGLIB$STATIC_CALLBACKS = arrcallback;
    }
    private static final void CGLIB$BIND_CALLBACKS(Object object) {
        Person$$EnhancerByCGLIB$$34b905e4 person$$EnhancerByCGLIB$$34b905e4 = (Person$$EnhancerByCGLIB$$34b905e4)object;
        if (person$$EnhancerByCGLIB$$34b905e4.CGLIB$BOUND) return;
        person$$EnhancerByCGLIB$$34b905e4.CGLIB$BOUND = true;
        Object t = CGLIB$THREAD_CALLBACKS.get();
        if (t == null && (v783 = CGLIB$STATIC_CALLBACKS) == null) {
            return;
        }
        person$$EnhancerByCGLIB$$34b905e4.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])t)[0];
    }
    static {
        Person$$EnhancerByCGLIB$$34b905e4.CGLIB$STATICHOOK2();
    }
}

可以看到,在创建一个代理类的对象的时候,会先加载这个类,也就调用了类的静态方法CGLIB$STATICHOOK2(),在这个函数中对类的其它静态字段进行了初始化,比如对应每个需要代理的方法都生成两个字段,一个是用于反射的Method对象,另一个是用于加快方法调用、避免反射带来的开销的MethodProxy对象。然后在构造函数里面调用了CGLIB$BIND_CALLBACKS(this),在CGLIB$BIND_CALLBACKS方法中会把private MethodInterceptor CGLIB$CALLBACK_0字段和设置的静态变量ThreadLocal CGLIB$THREAD_CALLBACKS进行绑定,也就是说,设置的Callback并不是硬编码在生成的代理类里面的,而是可以动态地设置。

由于代理类继承了被代理类,所以调用sayHello()方法时会直接调用代理类的sayHello()方法,而在代理类的方法中,调用了Callback的逻辑。

到此这篇关于Java中的cglib原理解析的文章就介绍到这了,更多相关cglib原理解析内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 解决SpringBoot中使用@Transactional注解遇到的问题

    解决SpringBoot中使用@Transactional注解遇到的问题

    这篇文章主要介绍了SpringBoot中使用@Transactional注解遇到的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Spring Boot 静态资源处理方式

    Spring Boot 静态资源处理方式

    这篇文章主要介绍了Spring Boot 静态资源处理方式,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-09-09
  • SpringBoot18 redis的配置方法

    SpringBoot18 redis的配置方法

    本文介绍在SpringBoot项目中集成和使用Redis的方法,包括添加依赖、配置文件、自定义序列化方式、使用方式、实际使用示例、常见操作总结以及注意事项,特别强调了配置类的作用,即使用JSON序列化以提高数据的可读性、节省空间和跨语言兼容性,感兴趣的朋友跟随小编一起看看吧
    2025-11-11
  • RocketMQ producer同步发送单向发送源码解析

    RocketMQ producer同步发送单向发送源码解析

    这篇文章主要为大家介绍了RocketMQ producer同步发送单向发送源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Java动态线程池插件dynamic-tp集成过程浅析

    Java动态线程池插件dynamic-tp集成过程浅析

    这篇文章主要介绍了Java动态线程池插件dynamic-tp集成过程,dynamic-tp是一个轻量级的动态线程池插件,它是一个基于配置中心的动态线程池,线程池的参数可以通过配置中心配置进行动态的修改
    2023-03-03
  • Java分布式事务实现原理与解决方案详解

    Java分布式事务实现原理与解决方案详解

    分布式事务是分布式系统中的核心挑战之一,它确保跨多个独立服务或数据源的操作能够保持原子性、一致性、隔离性和持久性(ACID),本文给大家介绍Java生态中分布式事务的实现原理、主流解决方案及其适用场景,感兴趣的朋友一起看看吧
    2025-09-09
  • Spring mvc拦截器实现原理解析

    Spring mvc拦截器实现原理解析

    这篇文章主要介绍了Spring mvc拦截器实现原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • JAVA时间戳-Calendar类使用(包括set,get,add方法)

    JAVA时间戳-Calendar类使用(包括set,get,add方法)

    这篇文章主要介绍了JAVA时间戳-Calendar类使用(包括set,get,add方法),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • Java编码辅助工具Lombok用法详解

    Java编码辅助工具Lombok用法详解

    这篇文章主要介绍了Java编码辅助工具Lombok用法详解,可以使用Lombok来避免这种重复的操作,减少非核心代码的臃肿,提高编码效率,需要的朋友可以参考下
    2019-06-06
  • SpringBoot集成ECDH密钥交换的方法

    SpringBoot集成ECDH密钥交换的方法

    ECDH密钥交换算法通过椭圆曲线和Diffie-Hellman方法生成共享密钥,用于前端和后端之间的AES加密通信,前端使用elliptic.js生成密钥对,后端使用crypto-js.min.js进行AES加密,本文给大家介绍SpringBoot集成ECDH密钥交换的相关知识,感兴趣的朋友一起看看吧
    2025-01-01

最新评论