From c48d7563fad7b3d6424b4b1e839e25e3e5847f66 Mon Sep 17 00:00:00 2001 From: wusheng Date: Thu, 9 Jun 2016 20:57:23 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E5=A4=8D=E6=97=A5=E5=BF=97=E6=8F=8F?= =?UTF-8?q?=E8=BF=B0=EF=BC=8C=E4=BD=BFMethodMatcher=E7=9A=84=E7=B1=BB?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E6=8F=8F=E8=BF=B0=E5=8F=AF=E8=AF=BB=E6=80=A7?= =?UTF-8?q?=E6=8F=90=E9=AB=98=E3=80=82=E5=B9=B6=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../skywalking/conf/ConfigInitializer.java | 2 +- .../interceptor/EnhanceClazz4Interceptor.java | 9 ++- .../plugin/interceptor/MethodMatcher.java | 16 +++-- .../matcher/MethodExclusiveMatcher.java | 65 ------------------- .../matcher/MethodsExclusiveMatcher.java | 15 ++--- .../matcher/SimpleMethodMatcher.java | 5 +- .../cloud/matcher/ExclusionMatcherTest.java | 7 +- .../ai/cloud/matcher/TestMatcherDefine.java | 4 ++ .../test/ai/cloud/plugin/PluginMainTest.java | 3 +- .../src/test/resources/skywalking-plugin.def | 2 +- 10 files changed, 38 insertions(+), 90 deletions(-) delete mode 100644 skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodExclusiveMatcher.java diff --git a/skywalking-api/src/main/java/com/ai/cloud/skywalking/conf/ConfigInitializer.java b/skywalking-api/src/main/java/com/ai/cloud/skywalking/conf/ConfigInitializer.java index 18f5bc61eb..4ef8067197 100644 --- a/skywalking-api/src/main/java/com/ai/cloud/skywalking/conf/ConfigInitializer.java +++ b/skywalking-api/src/main/java/com/ai/cloud/skywalking/conf/ConfigInitializer.java @@ -26,7 +26,7 @@ public class ConfigInitializer { properties.load(inputStream); initNextLevel(properties, Config.class, new ConfigDesc()); AuthDesc.isAuth = Boolean.valueOf(System.getenv(AUTH_SYSTEM_ENV_NAME)); - logger.info("sky-walking auth check : " + AuthDesc.isAuth); + logger.info("sky-walking system-env auth : " + AuthDesc.isAuth); if(!AuthDesc.isAuth && AUTH_OVERRIDE){ AuthDesc.isAuth = AUTH_OVERRIDE; logger.info("sky-walking auth override: " + AuthDesc.isAuth); 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 a4493cb10e..97e39e2bdc 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 @@ -119,9 +119,16 @@ 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"); + int ruleIdx = 1; + for (MethodMatcher methodMatcher : methodMatchers) { + enhanceRules.append("\t" + ruleIdx++ + ". " + methodMatcher + "\n"); + } + logger.debug(enhanceRules); for (MethodMatcher methodMatcher : methodMatchers) { - logger.debug("prepare to enhance class {} {}", + logger.debug("enhance class {} by rule: {}", enhanceOriginClassName, methodMatcher); newClassBuilder = newClassBuilder.method( methodMatcher.builderMatcher()).intercept( 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 b9feacf62f..1c85806dd5 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 @@ -92,18 +92,24 @@ public abstract class MethodMatcher { @Override public String toString() { - StringBuilder stringBuilder = new StringBuilder(" the method named " + getMethodMatchDescribe()); + StringBuilder stringBuilder = new StringBuilder("method name=" + getMethodMatchDescribe()); if (getModifier() != null) { - stringBuilder.append(" with " + getModifier() + " modifier"); + stringBuilder.insert(0, getModifier() + " "); } if (getArgNum() > -1) { - stringBuilder.append("," + getArgNum() + " arguments"); + stringBuilder.append(", argnum=" + getArgNum()); } if (getArgTypeArray() != null) { - stringBuilder.append(",argument type are "); - for (Class argType : getArgTypeArray()) { + stringBuilder.append(", types of arguments are "); + boolean isFirst = true; + for (Class argType : getArgTypeArray()) { + if(isFirst){ + isFirst = false; + }else{ + stringBuilder.append(","); + } stringBuilder.append(argType.getName()); } } diff --git a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodExclusiveMatcher.java b/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodExclusiveMatcher.java deleted file mode 100644 index 6a2ef12494..0000000000 --- a/skywalking-api/src/main/java/com/ai/cloud/skywalking/plugin/interceptor/matcher/MethodExclusiveMatcher.java +++ /dev/null @@ -1,65 +0,0 @@ -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 class MethodExclusiveMatcher extends MethodMatcher { - - public MethodExclusiveMatcher(String methodMatchDescribe) { - super(methodMatchDescribe); - } - - public MethodExclusiveMatcher(String methodMatchDescribe, int argNum) { - super(methodMatchDescribe, argNum); - } - - public MethodExclusiveMatcher(String methodMatchDescribe, Class[] argTypeArray) { - super(methodMatchDescribe, argTypeArray); - } - - public MethodExclusiveMatcher(Modifier modifier, String methodMatchDescribe) { - super(modifier, methodMatchDescribe); - } - - public MethodExclusiveMatcher(Modifier modifier, String methodMatchDescribe, int argNum) { - super(modifier, methodMatchDescribe, argNum); - } - - public MethodExclusiveMatcher(Modifier modifier, String methodMatchDescribe, Class[] argTypeArray) { - super(modifier, methodMatchDescribe, argTypeArray); - } - - @Override - public ElementMatcher.Junction builderMatcher() { - ElementMatcher.Junction matcher = named(getMethodMatchDescribe()); - return not(mergeArgumentsIfNecessary(matcher)); - } - - @Override - public String toString() { - StringBuilder stringBuilder = new StringBuilder("any method exclude "); - - stringBuilder.append(" method named " + getMethodMatchDescribe()); - - if (getModifier() != null) { - stringBuilder.append(getModifier()); - } - - if (getArgNum() > -1) { - stringBuilder.append(" with " + getArgNum() + " arguments"); - } - - if (getArgTypeArray() != null) { - stringBuilder.append(" with "); - for (Class argType : getArgTypeArray()) { - stringBuilder.append(argType.getName()); - } - } - - return stringBuilder.toString(); - } -} 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 f15b85b45b..d3bc20cebe 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 @@ -18,14 +18,14 @@ public class MethodsExclusiveMatcher extends MethodMatcher { private List matchers = new ArrayList(); public MethodsExclusiveMatcher(String... methodNames) { - super("Exclusive method name: " + methodNames.toString()); + super("exclude method name: " + methodNames.toString()); for (String methodName : methodNames) { matchers.add(new SimpleMethodMatcher(methodName)); } } public MethodsExclusiveMatcher(MethodMatcher... matchers) { - super("Exclusive methods description :" + matchers.toString()); + super("exclude methods description :" + matchers.toString()); this.matchers.addAll(Arrays.asList(matchers)); } @@ -48,15 +48,10 @@ public class MethodsExclusiveMatcher extends MethodMatcher { @Override public String toString() { - StringBuilder stringBuilder = new StringBuilder("any method exclude the method(s) as follow:\n "); - int i = 0; + StringBuilder stringBuilder = new StringBuilder("exclude following method(s): "); + int idx = 1; for (MethodMatcher methodMatcher : matchers) { - if (i == 0) { - stringBuilder.append(methodMatcher.toString() + " or "); - i++; - } else { - stringBuilder.append(methodMatcher.toString()); - } + stringBuilder.append(idx++ + "." + methodMatcher.toString() + ". "); } return stringBuilder.toString(); 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 612a48c1f2..cf2496f095 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 @@ -1,11 +1,12 @@ package com.ai.cloud.skywalking.plugin.interceptor.matcher; +import static net.bytebuddy.matcher.ElementMatchers.named; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.matcher.ElementMatcher; -import static net.bytebuddy.matcher.ElementMatchers.named; +import com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher; -public class SimpleMethodMatcher extends com.ai.cloud.skywalking.plugin.interceptor.MethodMatcher { +public class SimpleMethodMatcher extends MethodMatcher { public SimpleMethodMatcher(String methodName) { super(methodName); 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 949dc0a911..f22cd7cf09 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 @@ -1,11 +1,12 @@ package test.ai.cloud.matcher; -import com.ai.cloud.skywalking.plugin.PluginBootstrap; +import junit.framework.TestCase; -public class ExclusionMatcherTest { +import com.ai.cloud.skywalking.plugin.PluginBootstrap; +public class ExclusionMatcherTest extends TestCase{ - public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException { + public void testMatcher() throws ClassNotFoundException, IllegalAccessException, InstantiationException { new PluginBootstrap().start(); TestMatcherClass testMatcherClass = (TestMatcherClass) Class.forName("test.ai.cloud.matcher.TestMatcherClass").newInstance(); 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 90bad5df4d..332640cf22 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 @@ -23,6 +23,10 @@ public class TestMatcherDefine implements InterceptorDefine { new MethodsExclusiveMatcher(new SimpleMethodMatcher("set"), new SimpleMethodMatcher(MethodMatcher.Modifier.Public,"get")), 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"))}; } @Override diff --git a/skywalking-api/src/test/java/test/ai/cloud/plugin/PluginMainTest.java b/skywalking-api/src/test/java/test/ai/cloud/plugin/PluginMainTest.java index ed30c53be6..9cff3731f7 100644 --- a/skywalking-api/src/test/java/test/ai/cloud/plugin/PluginMainTest.java +++ b/skywalking-api/src/test/java/test/ai/cloud/plugin/PluginMainTest.java @@ -12,10 +12,9 @@ public class PluginMainTest { .main(new String[] { "test.ai.cloud.plugin.PluginMainTest" }); } - public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException { + public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { long start = System.currentTimeMillis(); - BeInterceptedClass inst = (BeInterceptedClass) Class.forName("test.ai.cloud.plugin.BeInterceptedClass").newInstance(); inst.printabc(); long end = System.currentTimeMillis(); diff --git a/skywalking-api/src/test/resources/skywalking-plugin.def b/skywalking-api/src/test/resources/skywalking-plugin.def index dcfaaef7dc..3f730e2d3f 100644 --- a/skywalking-api/src/test/resources/skywalking-plugin.def +++ b/skywalking-api/src/test/resources/skywalking-plugin.def @@ -1 +1 @@ -test.ai.cloud.plugin.TestInterceptorDefine \ No newline at end of file +test.ai.cloud.matcher.TestMatcherDefine \ No newline at end of file -- GitLab