提交 55592615 编写于 作者: A ascrutae

重构拦截点的代码,将拦截点改为继承式

上级 a5c9cd0c
package com.ai.cloud.skywalking.plugin.interceptor;
import static net.bytebuddy.matcher.ElementMatchers.any;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import java.util.List;
import com.ai.cloud.skywalking.plugin.PluginCfg;
import com.ai.cloud.skywalking.util.StringUtil;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
......@@ -16,12 +13,12 @@ import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.implementation.bind.annotation.FieldProxy;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.pool.TypePool.Resolution;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.ai.cloud.skywalking.plugin.PluginCfg;
import com.ai.cloud.skywalking.util.StringUtil;
import java.util.List;
import static net.bytebuddy.matcher.ElementMatchers.any;
public class EnhanceClazz4Interceptor {
private static Logger logger = LogManager
......@@ -58,7 +55,7 @@ public class EnhanceClazz4Interceptor {
interceptorDefineClassName).newInstance();
String enhanceOriginClassName = define.getBeInterceptedClassName();
if(StringUtil.isEmpty(enhanceOriginClassName)){
if (StringUtil.isEmpty(enhanceOriginClassName)) {
logger.warn("classname of being intercepted is not defined by {}.",
interceptorDefineClassName);
return;
......@@ -68,7 +65,7 @@ public class EnhanceClazz4Interceptor {
enhanceOriginClassName, interceptorDefineClassName);
Resolution resolution = typePool.describe(enhanceOriginClassName);
if(!resolution.isResolved()){
if (!resolution.isResolved()) {
logger.warn("class {} can't be resolved, enhance by {} failue.",
enhanceOriginClassName, interceptorDefineClassName);
return;
......@@ -100,7 +97,7 @@ public class EnhanceClazz4Interceptor {
* required by interceptorDefineClass. <br/>
*/
IAroundInterceptor interceptor = define.instance();
if(interceptor == null){
if (interceptor == null) {
throw new EnhanceException("no IAroundInterceptor instance. ");
}
......@@ -118,31 +115,16 @@ public class EnhanceClazz4Interceptor {
FieldGetter.class,
FieldSetter.class))));
InterceptPoint[] methodNameList = define.getBeInterceptedMethods();
MethodNameMatcher[] methodNameList = define.getBeInterceptedMethods();
ClassMethodInterceptor classMethodInterceptor = new ClassMethodInterceptor(
interceptor);
for (InterceptPoint method : methodNameList) {
for (MethodNameMatcher method : methodNameList) {
logger.debug("prepare to enhance class {} method [{}] ",
enhanceOriginClassName, method.getMethodName());
if (method.getArgTypeArray() != null) {
newClassBuilder = newClassBuilder.method(
named(method.getMethodName()).and(
takesArguments(method.getArgTypeArray()))).intercept(
MethodDelegation.to(classMethodInterceptor));
} else if (method.getArgNum() > -1) {
enhanceOriginClassName, method.getMethodMatchDescribe());
newClassBuilder = newClassBuilder.method(
named(method.getMethodName()).and(
takesArguments(method.getArgNum()))).intercept(
method.<MethodDescription>builderMatcher()).intercept(
MethodDelegation.to(classMethodInterceptor));
} else if("*".equals(method.getMethodName())){
newClassBuilder = newClassBuilder.method(any()).intercept(
MethodDelegation.to(classMethodInterceptor));
} else {
newClassBuilder = newClassBuilder.method(
named(method.getMethodName())).intercept(
MethodDelegation.to(classMethodInterceptor));
}
}
/**
......
......@@ -13,7 +13,7 @@ public interface InterceptorDefine {
*
* @return
*/
public InterceptPoint[] getBeInterceptedMethods();
public MethodNameMatcher[] getBeInterceptedMethods();
/**
* 返回增强拦截器的实现<br/>
......
package com.ai.cloud.skywalking.plugin.interceptor;
import net.bytebuddy.matcher.ElementMatcher;
public abstract class MethodNameMatcher<T> {
private String methodMatchDescribe;
private int argNum = -1;
private Class<?>[] argTypeArray;
public MethodNameMatcher(String methodMatchDescribe) {
this.methodMatchDescribe = methodMatchDescribe;
}
public MethodNameMatcher(String methodMatchDescribe, int argNum) {
this.methodMatchDescribe = methodMatchDescribe;
this.argNum = argNum;
}
public MethodNameMatcher(String methodMatchDescribe, Class<?>[] argTypeArray) {
this.argTypeArray = argTypeArray;
this.methodMatchDescribe = methodMatchDescribe;
}
public abstract <T> ElementMatcher<T> builderMatcher();
protected String getMethodMatchDescribe() {
return methodMatchDescribe;
}
protected int getArgNum() {
return argNum;
}
protected Class<?>[] getArgTypeArray() {
return argTypeArray;
}
}
package com.ai.cloud.skywalking.plugin.interceptor.matcher;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import net.bytebuddy.matcher.ElementMatcher;
import static net.bytebuddy.matcher.ElementMatchers.any;
public class AnyMethodMatcher extends MethodNameMatcher {
public AnyMethodMatcher() {
super("*");
}
@Override
public ElementMatcher builderMatcher() {
return any();
}
}
package com.ai.cloud.skywalking.plugin.interceptor.matcher;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import net.bytebuddy.matcher.ElementMatcher;
import static net.bytebuddy.matcher.ElementMatchers.*;
public class ExclusionNameMatcher extends MethodNameMatcher {
public ExclusionNameMatcher(String methodMatchDescribe) {
super(methodMatchDescribe);
}
public ExclusionNameMatcher(String methodMatchDescribe, int argNum) {
super(methodMatchDescribe, argNum);
}
public ExclusionNameMatcher(String methodMatchDescribe, Class[] argTypeArray) {
super(methodMatchDescribe, argTypeArray);
}
@Override
public ElementMatcher builderMatcher() {
ElementMatcher.Junction matcher = not(named(getMethodMatchDescribe()));
if (getArgTypeArray() != null) {
matcher.and(takesArguments(getArgTypeArray()));
}
if (getArgNum() > -1) {
matcher.and(takesArguments(getArgNum()));
}
return matcher;
}
}
package com.ai.cloud.skywalking.plugin.interceptor.matcher;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import net.bytebuddy.matcher.ElementMatcher;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
public class FullNameMatcher extends MethodNameMatcher {
public FullNameMatcher(String methodName) {
super(methodName);
}
public FullNameMatcher(String methodName, int argNum) {
super(methodName, argNum);
}
public FullNameMatcher(String methodName, Class<?>... args) {
super(methodName, args);
}
@Override
public ElementMatcher builderMatcher() {
ElementMatcher.Junction matcher = named(getMethodMatchDescribe());
if (getArgTypeArray() != null) {
matcher.and(takesArguments(getArgTypeArray()));
}
if (getArgNum() > -1) {
matcher.and(takesArguments(getArgNum()));
}
return matcher;
}
}
package com.ai.cloud.skywalking.plugin.interceptor.matcher;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import net.bytebuddy.matcher.ElementMatcher;
import static net.bytebuddy.matcher.ElementMatchers.nameMatches;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
public class RegexNameMatcher extends MethodNameMatcher {
public RegexNameMatcher(String methodMatchDescribe) {
super(methodMatchDescribe);
}
public RegexNameMatcher(String methodMatchDescribe, int argNum) {
super(methodMatchDescribe, argNum);
}
public RegexNameMatcher(String methodMatchDescribe, Class[] argTypeArray) {
super(methodMatchDescribe, argTypeArray);
}
@Override
public ElementMatcher builderMatcher() {
ElementMatcher.Junction matcher = nameMatches(getMethodMatchDescribe());
if (getArgTypeArray() != null) {
matcher.and(takesArguments(getArgTypeArray()));
}
if (getArgNum() > -1) {
matcher.and(takesArguments(getArgNum()));
}
return matcher;
}
}
......@@ -3,6 +3,8 @@ package test.ai.cloud.plugin;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorDefine;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.FullNameMatcher;
public class TestInterceptorDefine implements InterceptorDefine {
......@@ -12,8 +14,8 @@ public class TestInterceptorDefine implements InterceptorDefine {
}
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[] { new InterceptPoint("printabc") };
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[] { new FullNameMatcher("printabc") };
}
@Override
......
package org.skywalking.httpClient.v4.plugin.dubbox.rest.attachment;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import org.skywalking.httpClient.v4.plugin.HttpClientExecuteInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
......@@ -20,7 +21,7 @@ public class DubboxRestHeadSetterAttachment implements InterceptorDefine {
}
@Override
public InterceptPoint[] getBeInterceptedMethods() {
public MethodNameMatcher[] getBeInterceptedMethods() {
return null;
}
......
package org.skywalking.httpClient.v4.plugin.define;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.FullNameMatcher;
public class AbstractHttpClientPluginDefine extends HttpClientPluginDefine {
......@@ -16,8 +18,8 @@ public class AbstractHttpClientPluginDefine extends HttpClientPluginDefine {
*
*/
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[] {
new InterceptPoint("doExecute")};
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[] {
new FullNameMatcher("doExecute")};
}
}
package org.skywalking.httpClient.v4.plugin.define;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.FullNameMatcher;
public class DefaultRequestDirectorPluginDefine extends HttpClientPluginDefine {
/**
......@@ -14,9 +16,9 @@ public class DefaultRequestDirectorPluginDefine extends HttpClientPluginDefine {
}
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[] {
new InterceptPoint("execute")};
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[] {
new FullNameMatcher("execute")};
}
}
package org.skywalking.httpClient.v4.plugin.define;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.FullNameMatcher;
public class InternalHttpClientPluginDefine extends HttpClientPluginDefine {
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[]{new InterceptPoint("doExecute")};
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[]{new FullNameMatcher("doExecute")};
}
@Override
......
package org.skywalking.httpClient.v4.plugin.define;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.FullNameMatcher;
public class MinimalHttpClientPluginDefine extends HttpClientPluginDefine {
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[]{new InterceptPoint("doExecute")};
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[]{new FullNameMatcher("doExecute")};
}
@Override
......
package org.skywalking.jedis.v2.plugin.define;
import org.skywalking.jedis.v2.plugin.JedisInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorDefine;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.ExclusionNameMatcher;
import org.skywalking.jedis.v2.plugin.JedisInterceptor;
public class JedisPluginDefine implements InterceptorDefine {
......@@ -14,8 +15,10 @@ public class JedisPluginDefine implements InterceptorDefine {
}
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[] { new InterceptPoint("*") };
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[]{
new ExclusionNameMatcher("set"),
};
}
@Override
......
......@@ -3,6 +3,8 @@ package com.ai.cloud.skywalking.plugin.mysql;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptPoint;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorDefine;
import com.ai.cloud.skywalking.plugin.interceptor.MethodNameMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.FullNameMatcher;
public class ConnectionPluginDefine implements InterceptorDefine {
......@@ -12,12 +14,12 @@ public class ConnectionPluginDefine implements InterceptorDefine {
}
@Override
public InterceptPoint[] getBeInterceptedMethods() {
return new InterceptPoint[] { new InterceptPoint("createStatement", 2),
new InterceptPoint("prepareStatement", 3),
new InterceptPoint("prepareCall", 3),
new InterceptPoint("commit"), new InterceptPoint("rollback"),
new InterceptPoint("close") };
public MethodNameMatcher[] getBeInterceptedMethods() {
return new MethodNameMatcher[] { new FullNameMatcher("createStatement", 2),
new FullNameMatcher("prepareStatement", 3),
new FullNameMatcher("prepareCall", 3),
new FullNameMatcher("commit"), new FullNameMatcher("rollback"),
new FullNameMatcher("close") };
}
@Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册