1.修改类名

2. 修改方法入参
3.增加测试用例
This commit is contained in:
ascrutae 2016-06-13 14:35:21 +08:00
parent 4f37cb7d08
commit 3a31c5bb6a
9 changed files with 66 additions and 57 deletions

View File

@ -1,14 +1,13 @@
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.any;
public class AnyMethodMatcher extends ExclusiveObjectDefaultMethodMatcher {
public class AnyMethodsMatcher extends ExclusiveObjectDefaultMethodsMatcher {
public AnyMethodMatcher() {
public AnyMethodsMatcher() {
super("any method");
}

View File

@ -1,44 +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 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

@ -0,0 +1,51 @@
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.not;
public abstract class ExclusiveObjectDefaultMethodsMatcher extends MethodMatcher {
private static final MethodMatcher[] EXCLUSIVE_DEFAULT_METHOD_NAME = new MethodMatcher[]{
new SimpleMethodMatcher(Modifier.Public, "finalize", 0),
new SimpleMethodMatcher(Modifier.Public, "wait", long.class, int.class),
new SimpleMethodMatcher(Modifier.Public, "wait", long.class),
new SimpleMethodMatcher(Modifier.Public, "wait", 0),
new SimpleMethodMatcher(Modifier.Public, "equals", Object.class),
new SimpleMethodMatcher(Modifier.Public, "toString", 0),
new SimpleMethodMatcher(Modifier.Public, "hashCode", 0),
new SimpleMethodMatcher(Modifier.Public, "getClass", 0),
new SimpleMethodMatcher(Modifier.Public, "clone", 0),
new SimpleMethodMatcher(Modifier.Public, "notify", 0),
new SimpleMethodMatcher(Modifier.Public, "notifyAll", 0)
};
public ExclusiveObjectDefaultMethodsMatcher(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 (MethodMatcher methodMatcher : EXCLUSIVE_DEFAULT_METHOD_NAME) {
if (exclusiveMatcher == null) {
exclusiveMatcher = methodMatcher.buildMatcher();
continue;
}
exclusiveMatcher = exclusiveMatcher.or(methodMatcher.buildMatcher());
}
return not(exclusiveMatcher);
}
public abstract ElementMatcher.Junction<MethodDescription> match();
}

View File

@ -16,7 +16,7 @@ public class MethodRegexMatcher extends MethodMatcher {
super(methodMatchDescribe, argNum);
}
public MethodRegexMatcher(String methodMatchDescribe, Class<?>[] argTypeArray) {
public MethodRegexMatcher(String methodMatchDescribe, Class<?>... argTypeArray) {
super(methodMatchDescribe, argTypeArray);
}
@ -28,7 +28,7 @@ public class MethodRegexMatcher extends MethodMatcher {
super(modifier, methodMatchDescribe, argNum);
}
public MethodRegexMatcher(Modifier modifier, String methodMatchDescribe, Class<?>[] argTypeArray) {
public MethodRegexMatcher(Modifier modifier, String methodMatchDescribe, Class<?>... argTypeArray) {
super(modifier, methodMatchDescribe, argTypeArray);
}

View File

@ -10,10 +10,7 @@ import java.util.List;
import static net.bytebuddy.matcher.ElementMatchers.not;
/**
* Created by xin on 16-6-8.
*/
public class MethodsExclusiveMatcher extends ExclusiveObjectDefaultMethodMatcher {
public class MethodsExclusiveMatcher extends ExclusiveObjectDefaultMethodsMatcher {
private List<MethodMatcher> matchers = new ArrayList<MethodMatcher>();

View File

@ -29,7 +29,7 @@ public class SimpleMethodMatcher extends MethodMatcher {
super(modifier, methodMatchDescribe, argNum);
}
public SimpleMethodMatcher(Modifier modifier, String methodMatchDescribe, Class<?>[] argTypeArray) {
public SimpleMethodMatcher(Modifier modifier, String methodMatchDescribe, Class<?>... argTypeArray) {
super(modifier, methodMatchDescribe, argTypeArray);
}

View File

@ -6,7 +6,7 @@ import com.ai.cloud.skywalking.plugin.PluginBootstrap;
public class ExclusionMatcherTest extends TestCase{
public void testMatcher() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
public void testMatcher() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InterruptedException {
new PluginBootstrap().start();
TestMatcherClass testMatcherClass = (TestMatcherClass) Class.forName("test.ai.cloud.matcher.TestMatcherClass").newInstance();
@ -15,6 +15,7 @@ public class ExclusionMatcherTest extends TestCase{
testMatcherClass.get("a");
testMatcherClass.find();
System.out.println(testMatcherClass.toString());
testMatcherClass.equals(new TestMatcherClass());
}
}

View File

@ -30,4 +30,10 @@ public class TestMatcherClass {
public String toString() {
return "Call toString()";
}
@Override
public boolean equals(Object obj) {
System.out.println("equals(Object obj)");
return true;
}
}

View File

@ -3,7 +3,6 @@ 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;
@ -26,7 +25,7 @@ public class TestMatcherDefine implements InterceptorDefine {
};
//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 AnyMethodsMatcher()};
//return new MethodMatcher[]{new MethodsExclusiveMatcher(new SimpleMethodMatcher("set"), new SimpleMethodMatcher(MethodMatcher.Modifier.Public,"get"))};
}