All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xyz.erupt.core.proxy.AnnotationProxy Maven / Gradle / Ivy

There is a newer version: 1.12.17
Show newest version
package xyz.erupt.core.proxy;

import lombok.SneakyThrows;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory;

/**
 * 注解代理
 *
 * @author YuePeng
 * date 2022/2/5 14:20
 */
public abstract class AnnotationProxy {

    // 原始注解
    public A rawAnnotation;

    // 代理后新注解
    public A proxyAnnotation;

    // 向上引用
    protected AnnotationProxy parent;

    protected abstract Object invocation(MethodInvocation invocation);

    public A newProxy(A annotation) {
        return this.newProxy(annotation, null);
    }

    //创建注解注解代理类
    public A newProxy(A annotation, AnnotationProxy parent) {
        this.parent = parent;
        this.rawAnnotation = annotation;
        ProxyFactory proxyFactory = new ProxyFactory(annotation);
        MethodInterceptor interceptor = this::invocation;
        proxyFactory.addAdvice(interceptor);
        this.proxyAnnotation = (A) proxyFactory.getProxy(this.getClass().getClassLoader());
        return this.proxyAnnotation;
    }

    // annotation method invoke
    @SneakyThrows
    public Object invoke(MethodInvocation invocation) {
        return invocation.getMethod().invoke(invocation.getThis());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy