1.修改MethodMatcher抽象方法的名字

2. 增加屏蔽Object公共方法的Matcher基类
This commit is contained in:
ascrutae 2016-06-13 13:24:44 +08:00
parent 0abd415403
commit e617ac3154
13 changed files with 98 additions and 36 deletions

View File

@ -1,9 +1,7 @@
package com.ai.cloud.skywalking.plugin.interceptor;
import static net.bytebuddy.matcher.ElementMatchers.any;
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.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
@ -11,14 +9,17 @@ import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.implementation.bind.annotation.FieldProxy;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
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;
import static net.bytebuddy.matcher.ElementMatchers.not;
public class EnhanceClazz4Interceptor {
private static Logger logger = LogManager
@ -74,10 +75,10 @@ public class EnhanceClazz4Interceptor {
/**
* find origin class source code for interceptor
*/
DynamicType.Builder<?> newClassBuilder = new ByteBuddy()
DynamicType.Builder<?> newClassBuilder = new ByteBuddy()
.rebase(resolution.resolve(),
ClassFileLocator.ForClassLoader.ofClassPath());
/**
* alter class source code.<br/>
*
@ -108,22 +109,31 @@ public class EnhanceClazz4Interceptor {
MethodMatcher[] methodMatchers = define.getBeInterceptedMethodsMatchers();
ClassMethodInterceptor classMethodInterceptor = new ClassMethodInterceptor(
interceptor);
StringBuilder enhanceRules = new StringBuilder("\nprepare to enhance class [" + enhanceOriginClassName + "] as following rules:\n");
StringBuilder enhanceRules = new StringBuilder("\nprepare to enhance class [" + enhanceOriginClassName + "] as following rules:\n");
int ruleIdx = 1;
for (MethodMatcher methodMatcher : methodMatchers) {
enhanceRules.append("\t" + ruleIdx++ + ". " + methodMatcher + "\n");
enhanceRules.append("\t" + ruleIdx++ + ". " + methodMatcher + "\n");
}
logger.debug(enhanceRules);
ElementMatcher.Junction matcher = null;
for (MethodMatcher methodMatcher : methodMatchers) {
logger.debug("enhance class {} by rule: {}",
enhanceOriginClassName, methodMatcher);
newClassBuilder = newClassBuilder.method(
methodMatcher.builderMatcher()).intercept(
MethodDelegation.to(classMethodInterceptor));
if (matcher == null) {
matcher = methodMatcher.buildMatcher();
continue;
}
matcher = matcher.or(methodMatcher.buildMatcher());
}
// TODO: not support static method now. will enhance it.
matcher = matcher.and(not(ElementMatchers.isStatic()));
newClassBuilder = newClassBuilder.method(matcher).intercept(
MethodDelegation.to(classMethodInterceptor));
/**
* naming class as origin class name, make and load class to
* classloader.

View File

@ -45,7 +45,7 @@ public abstract class MethodMatcher {
this.modifier = modifier;
}
public abstract ElementMatcher.Junction<MethodDescription> builderMatcher();
public abstract ElementMatcher.Junction<MethodDescription> buildMatcher();
protected String getMethodMatchDescribe() {
return methodMatchDescribe;

View File

@ -6,14 +6,14 @@ import net.bytebuddy.matcher.ElementMatcher;
import static net.bytebuddy.matcher.ElementMatchers.any;
public class AnyMethodMatcher extends MethodMatcher {
public class AnyMethodMatcher extends ExclusiveObjectDefaultMethodMatcher {
public AnyMethodMatcher() {
super("any method");
}
@Override
public ElementMatcher.Junction<MethodDescription> builderMatcher() {
public ElementMatcher.Junction<MethodDescription> match() {
return any();
}

View File

@ -0,0 +1,44 @@
package com.ai.cloud.skywalking.plugin.interceptor.matcher;
import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;
public abstract class ExclusiveObjectDefaultMethodMatcher extends MethodMatcher {
private static final String[] EXCLUSIVE_DEFAULT_METHOD_NAME = new String[]{
"finalize", "wait", "equals",
"toString", "hashCode", "getClass",
"clone", "notify", "notifyAll"
};
public ExclusiveObjectDefaultMethodMatcher(String methodMatchDescribe) {
super(methodMatchDescribe);
}
@Override
public ElementMatcher.Junction<MethodDescription> buildMatcher() {
return this.match().and(excludeObjectDefaultMethod());
}
protected ElementMatcher.Junction<MethodDescription> excludeObjectDefaultMethod() {
ElementMatcher.Junction<MethodDescription> exclusiveMatcher = null;
for (String methodName : EXCLUSIVE_DEFAULT_METHOD_NAME) {
if (exclusiveMatcher == null) {
exclusiveMatcher = named(methodName);
continue;
}
exclusiveMatcher = exclusiveMatcher.or(named(methodName));
}
return not(exclusiveMatcher);
}
public abstract ElementMatcher.Junction<MethodDescription> match();
}

View File

@ -34,7 +34,7 @@ public class MethodRegexMatcher extends MethodMatcher {
@Override
public ElementMatcher.Junction<MethodDescription> builderMatcher() {
public ElementMatcher.Junction<MethodDescription> buildMatcher() {
ElementMatcher.Junction<MethodDescription> matcher = nameMatches(getMethodMatchDescribe());
return mergeArgumentsIfNecessary(matcher);
}

View File

@ -13,7 +13,7 @@ import static net.bytebuddy.matcher.ElementMatchers.not;
/**
* Created by xin on 16-6-8.
*/
public class MethodsExclusiveMatcher extends MethodMatcher {
public class MethodsExclusiveMatcher extends ExclusiveObjectDefaultMethodMatcher {
private List<MethodMatcher> matchers = new ArrayList<MethodMatcher>();
@ -30,17 +30,16 @@ public class MethodsExclusiveMatcher extends MethodMatcher {
}
@Override
public ElementMatcher.Junction<MethodDescription> builderMatcher() {
public ElementMatcher.Junction<MethodDescription> match() {
ElementMatcher.Junction<MethodDescription> result = null;
for (MethodMatcher matcher : matchers) {
if (result == null) {
result = matcher.builderMatcher();
result = matcher.buildMatcher();
continue;
}
result = result.or(matcher.builderMatcher());
result = result.or(matcher.buildMatcher());
}
return not(result);

View File

@ -13,7 +13,7 @@ public class PrivateMethodMatcher extends MethodMatcher {
}
@Override
public ElementMatcher.Junction<MethodDescription> builderMatcher() {
public ElementMatcher.Junction<MethodDescription> buildMatcher() {
return any().and(ElementMatchers.<MethodDescription>isPrivate());
}

View File

@ -35,7 +35,7 @@ public class SimpleMethodMatcher extends MethodMatcher {
@Override
public ElementMatcher.Junction<MethodDescription> builderMatcher() {
public ElementMatcher.Junction<MethodDescription> buildMatcher() {
ElementMatcher.Junction<MethodDescription> matcher = named(getMethodMatchDescribe());
return mergeArgumentsIfNecessary(matcher);
}

View File

@ -14,6 +14,7 @@ public class ExclusionMatcherTest extends TestCase{
testMatcherClass.seta("a");
testMatcherClass.get("a");
testMatcherClass.find();
System.out.println(testMatcherClass.toString());
}
}

View File

@ -6,23 +6,28 @@ package test.ai.cloud.matcher;
public class TestMatcherClass {
public void set() {
System.out.println("set()");
System.out.println("public set()");
}
public void seta(String a) {
System.out.println("public seta(String a)");
set(a);
}
private void set(String a) {
System.out.println("set(String a)");
System.out.println("private set(String a)");
}
public void get(String a) {
System.out.println("get(String a)");
System.out.println("public get(String a)");
}
public void find() {
System.out.println("find()");
System.out.println("public find()");
}
@Override
public String toString() {
return "Call toString()";
}
}

View File

@ -3,6 +3,7 @@ package test.ai.cloud.matcher;
import com.ai.cloud.skywalking.plugin.interceptor.IAroundInterceptor;
import com.ai.cloud.skywalking.plugin.interceptor.InterceptorDefine;
import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.AnyMethodMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.MethodsExclusiveMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.PrivateMethodMatcher;
import com.ai.cloud.skywalking.plugin.interceptor.matcher.SimpleMethodMatcher;
@ -20,13 +21,13 @@ public class TestMatcherDefine implements InterceptorDefine {
public MethodMatcher[] getBeInterceptedMethodsMatchers() {
return new MethodMatcher[]{
new PrivateMethodMatcher(),
new MethodsExclusiveMatcher(new SimpleMethodMatcher("set"), new SimpleMethodMatcher(MethodMatcher.Modifier.Public,"get")),
new MethodsExclusiveMatcher(new SimpleMethodMatcher("set")),
new SimpleMethodMatcher(MethodMatcher.Modifier.Private, "set", 1)
};
//return new MethodMatcher[] { new SimpleMethodMatcher(Modifier.Public, "printabc", new Class[]{String.class, String.class}) };
//return new MethodMatcher[] { new PrivateMethodMatcher()};
//return new MethodMatcher[]{new AnyMethodMatcher()};
//return new MethodMatcher[]{new MethodsExclusiveMatcher(new SimpleMethodMatcher("set"), new SimpleMethodMatcher(MethodMatcher.Modifier.Public,"get"))};
//return new MethodMatcher[] { new PrivateMethodMatcher()};
//return new MethodMatcher[]{new AnyMethodMatcher()};
//return new MethodMatcher[]{new MethodsExclusiveMatcher(new SimpleMethodMatcher("set"), new SimpleMethodMatcher(MethodMatcher.Modifier.Public,"get"))};
}
@Override

View File

@ -6,6 +6,7 @@ skywalking.application_code=test
skywalking.auth_system_env_name=SKYWALKING_RUN
#skywalking数据编码
skywalking.charset=UTF-8
skywalking.auth_override=true
#是否打印数据
buriedpoint.printf=false

View File

@ -1 +1,2 @@
test.ai.cloud.plugin.TestInterceptorDefine
test.ai.cloud.plugin.TestInterceptorDefine
test.ai.cloud.matcher.TestMatcherDefine