From e617ac315485e488d96311cf722b658427be4037 Mon Sep 17 00:00:00 2001 From: ascrutae Date: Mon, 13 Jun 2016 13:24:44 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=94=B9MethodMatcher=E6=8A=BD?= =?UTF-8?q?=E8=B1=A1=E6=96=B9=E6=B3=95=E7=9A=84=E5=90=8D=E5=AD=97=202.=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B1=8F=E8=94=BDObject=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=9A=84Matcher=E5=9F=BA=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interceptor/EnhanceClazz4Interceptor.java | 42 +++++++++++------- .../plugin/interceptor/MethodMatcher.java | 2 +- .../interceptor/matcher/AnyMethodMatcher.java | 4 +- .../ExclusiveObjectDefaultMethodMatcher.java | 44 +++++++++++++++++++ .../matcher/MethodRegexMatcher.java | 2 +- .../matcher/MethodsExclusiveMatcher.java | 9 ++-- .../matcher/PrivateMethodMatcher.java | 2 +- .../matcher/SimpleMethodMatcher.java | 2 +- .../cloud/matcher/ExclusionMatcherTest.java | 1 + .../ai/cloud/matcher/TestMatcherClass.java | 13 ++++-- .../ai/cloud/matcher/TestMatcherDefine.java | 9 ++-- .../src/test/resources/sky-walking.auth | 1 + .../src/test/resources/skywalking-plugin.def | 3 +- 13 files changed, 98 insertions(+), 36 deletions(-) create mode 100644 skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/ExclusiveObjectDefaultMethodMatcher.java diff --git a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/EnhanceClazz4Interceptor.java b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/EnhanceClazz4Interceptor.java index 4e31f2b9c..49b570bf7 100644 --- a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/EnhanceClazz4Interceptor.java +++ b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/EnhanceClazz4Interceptor.java @@ -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.
* @@ -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. diff --git a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/MethodMatcher.java b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/MethodMatcher.java index 1c85806dd..2375b270c 100644 --- a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/MethodMatcher.java +++ b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/MethodMatcher.java @@ -45,7 +45,7 @@ public abstract class MethodMatcher { this.modifier = modifier; } - public abstract ElementMatcher.Junction builderMatcher(); + public abstract ElementMatcher.Junction buildMatcher(); protected String getMethodMatchDescribe() { return methodMatchDescribe; diff --git a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/AnyMethodMatcher.java b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/AnyMethodMatcher.java index 03608b354..8e6a31d45 100644 --- a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/AnyMethodMatcher.java +++ b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/AnyMethodMatcher.java @@ -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 builderMatcher() { + public ElementMatcher.Junction match() { return any(); } diff --git a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/ExclusiveObjectDefaultMethodMatcher.java b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/ExclusiveObjectDefaultMethodMatcher.java new file mode 100644 index 000000000..f0caa55c6 --- /dev/null +++ b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/ExclusiveObjectDefaultMethodMatcher.java @@ -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 buildMatcher() { + return this.match().and(excludeObjectDefaultMethod()); + } + + protected ElementMatcher.Junction excludeObjectDefaultMethod() { + ElementMatcher.Junction 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 match(); + + +} diff --git a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodRegexMatcher.java b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodRegexMatcher.java index 22df3b18d..61876eef3 100644 --- a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodRegexMatcher.java +++ b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodRegexMatcher.java @@ -34,7 +34,7 @@ public class MethodRegexMatcher extends MethodMatcher { @Override - public ElementMatcher.Junction builderMatcher() { + public ElementMatcher.Junction buildMatcher() { ElementMatcher.Junction matcher = nameMatches(getMethodMatchDescribe()); return mergeArgumentsIfNecessary(matcher); } diff --git a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodsExclusiveMatcher.java b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodsExclusiveMatcher.java index d3bc20ceb..55b544f54 100644 --- a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodsExclusiveMatcher.java +++ b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodsExclusiveMatcher.java @@ -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 matchers = new ArrayList(); @@ -30,17 +30,16 @@ public class MethodsExclusiveMatcher extends MethodMatcher { } @Override - public ElementMatcher.Junction builderMatcher() { - + public ElementMatcher.Junction match() { ElementMatcher.Junction 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); diff --git a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/PrivateMethodMatcher.java b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/PrivateMethodMatcher.java index 374415c63..2d7f414b4 100644 --- a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/PrivateMethodMatcher.java +++ b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/PrivateMethodMatcher.java @@ -13,7 +13,7 @@ public class PrivateMethodMatcher extends MethodMatcher { } @Override - public ElementMatcher.Junction builderMatcher() { + public ElementMatcher.Junction buildMatcher() { return any().and(ElementMatchers.isPrivate()); } diff --git a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/SimpleMethodMatcher.java b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/SimpleMethodMatcher.java index cf2496f09..6189d332e 100644 --- a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/SimpleMethodMatcher.java +++ b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/SimpleMethodMatcher.java @@ -35,7 +35,7 @@ public class SimpleMethodMatcher extends MethodMatcher { @Override - public ElementMatcher.Junction builderMatcher() { + public ElementMatcher.Junction buildMatcher() { ElementMatcher.Junction matcher = named(getMethodMatchDescribe()); return mergeArgumentsIfNecessary(matcher); } diff --git a/skywalking-api/src/test/java/test/ai/cloud/matcher/ExclusionMatcherTest.java b/skywalking-api/src/test/java/test/ai/cloud/matcher/ExclusionMatcherTest.java index f22cd7cf0..7f2334f47 100644 --- a/skywalking-api/src/test/java/test/ai/cloud/matcher/ExclusionMatcherTest.java +++ b/skywalking-api/src/test/java/test/ai/cloud/matcher/ExclusionMatcherTest.java @@ -14,6 +14,7 @@ public class ExclusionMatcherTest extends TestCase{ testMatcherClass.seta("a"); testMatcherClass.get("a"); testMatcherClass.find(); + System.out.println(testMatcherClass.toString()); } } diff --git a/skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherClass.java b/skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherClass.java index 9a72887c6..f4b2a86af 100644 --- a/skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherClass.java +++ b/skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherClass.java @@ -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()"; + } } diff --git a/skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherDefine.java b/skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherDefine.java index 332640cf2..27cf7c78b 100644 --- a/skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherDefine.java +++ b/skywalking-api/src/test/java/test/ai/cloud/matcher/TestMatcherDefine.java @@ -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 diff --git a/skywalking-api/src/test/resources/sky-walking.auth b/skywalking-api/src/test/resources/sky-walking.auth index 95aa1d976..edfeed871 100644 --- a/skywalking-api/src/test/resources/sky-walking.auth +++ b/skywalking-api/src/test/resources/sky-walking.auth @@ -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 diff --git a/skywalking-api/src/test/resources/skywalking-plugin.def b/skywalking-api/src/test/resources/skywalking-plugin.def index dcfaaef7d..d1a115ada 100644 --- a/skywalking-api/src/test/resources/skywalking-plugin.def +++ b/skywalking-api/src/test/resources/skywalking-plugin.def @@ -1 +1,2 @@ -test.ai.cloud.plugin.TestInterceptorDefine \ No newline at end of file +test.ai.cloud.plugin.TestInterceptorDefine +test.ai.cloud.matcher.TestMatcherDefine \ No newline at end of file