com.aliyun.openservices.ons.api.exactlyonce.manager.util.AopUtil Maven / Gradle / Ivy
package com.aliyun.openservices.ons.api.exactlyonce.manager.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.support.AopUtils;
/**
* @author gongshi
*/
public class AopUtil {
public static Class> findTargetClass(Object proxy) throws Exception {
if (AopUtils.isAopProxy(proxy)) {
AdvisedSupport advised = getAdvisedSupport(proxy);
Object target = advised.getTargetSource().getTarget();
return findTargetClass(target);
} else {
return proxy.getClass();
}
}
public static AdvisedSupport getAdvisedSupport(Object proxy) throws Exception {
Field h;
if (AopUtils.isJdkDynamicProxy(proxy)) {
h = proxy.getClass().getSuperclass().getDeclaredField("h");
} else {
h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0");
}
h.setAccessible(true);
Object dynamicAdvisedInterceptor = h.get(proxy);
Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
advised.setAccessible(true);
return (AdvisedSupport) advised.get(dynamicAdvisedInterceptor);
}
public static String formatMethod(Method method) {
StringBuilder sb = new StringBuilder();
String mehodName = method.getName();
Class>[] params = method.getParameterTypes();
sb.append(mehodName);
sb.append("(");
int paramPos = 0;
for (Class> claz : params) {
sb.append(claz.getName());
if (++paramPos < params.length) {
sb.append(",");
}
}
sb.append(")");
return sb.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy