提交 c48d7563 编写于 作者: wu-sheng's avatar wu-sheng

1.修复日志描述,使MethodMatcher的类增强描述可读性提高。并格式化。

上级 f4d20579
......@@ -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);
......
......@@ -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(
......
......@@ -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());
}
}
......
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<MethodDescription> builderMatcher() {
ElementMatcher.Junction<MethodDescription> 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();
}
}
......@@ -18,14 +18,14 @@ public class MethodsExclusiveMatcher extends MethodMatcher {
private List<MethodMatcher> matchers = new ArrayList<MethodMatcher>();
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();
......
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);
......
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();
......
......@@ -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
......
......@@ -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();
......
test.ai.cloud.plugin.TestInterceptorDefine
\ No newline at end of file
test.ai.cloud.matcher.TestMatcherDefine
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册