From a9c14f85e834da07e5d8fa99cc24a04e2e07b68d Mon Sep 17 00:00:00 2001 From: zhangxin Date: Tue, 6 Dec 2016 11:42:16 +0800 Subject: [PATCH] fix #69 --- .../enhance/ClassEnhancePluginDefine.java | 18 ++++++--- .../DefaultClassConstructorInterceptor.java | 37 +++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/enhance/DefaultClassConstructorInterceptor.java diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/enhance/ClassEnhancePluginDefine.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/enhance/ClassEnhancePluginDefine.java index 3b981d5a1..265e29abd 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/enhance/ClassEnhancePluginDefine.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/enhance/ClassEnhancePluginDefine.java @@ -1,9 +1,5 @@ package com.a.eye.skywalking.plugin.interceptor.enhance; -import static net.bytebuddy.jar.asm.Opcodes.ACC_PRIVATE; -import static net.bytebuddy.matcher.ElementMatchers.any; -import static net.bytebuddy.matcher.ElementMatchers.not; - import com.a.eye.skywalking.logging.api.ILog; import com.a.eye.skywalking.logging.api.LogManager; import com.a.eye.skywalking.plugin.AbstractClassEnhancePluginDefine; @@ -18,6 +14,9 @@ import net.bytebuddy.implementation.bind.annotation.FieldProxy; import net.bytebuddy.matcher.ElementMatcher; import net.bytebuddy.matcher.ElementMatchers; +import static net.bytebuddy.jar.asm.Opcodes.ACC_PRIVATE; +import static net.bytebuddy.matcher.ElementMatchers.not; + public abstract class ClassEnhancePluginDefine extends AbstractClassEnhancePluginDefine { private static ILog logger = LogManager.getLogger(ClassEnhancePluginDefine.class); @@ -70,8 +69,15 @@ public abstract class ClassEnhancePluginDefine extends AbstractClassEnhancePlugi for (ConstructorInterceptPoint constructorInterceptPoint : constructorInterceptPoints) { newClassBuilder = newClassBuilder.constructor(constructorInterceptPoint.getConstructorMatcher()) .intercept(SuperMethodCall.INSTANCE.andThen( - MethodDelegation.to(new ClassConstructorInterceptor(constructorInterceptPoint.getConstructorInterceptor())) - .appendParameterBinder(FieldProxy.Binder.install(FieldGetter.class, FieldSetter.class)))); + MethodDelegation.to(new ClassConstructorInterceptor(constructorInterceptPoint.getConstructorInterceptor())) + .appendParameterBinder(FieldProxy.Binder.install(FieldGetter.class, FieldSetter.class)))); + } + } else { + for (ConstructorInterceptPoint constructorInterceptPoint : constructorInterceptPoints) { + newClassBuilder = newClassBuilder.constructor(ElementMatchers.any()) + .intercept(SuperMethodCall.INSTANCE.andThen( + MethodDelegation.to(new DefaultClassConstructorInterceptor(constructorInterceptPoint.getConstructorInterceptor())) + .appendParameterBinder(FieldProxy.Binder.install(FieldGetter.class, FieldSetter.class)))); } } diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/enhance/DefaultClassConstructorInterceptor.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/enhance/DefaultClassConstructorInterceptor.java new file mode 100644 index 000000000..9f497572d --- /dev/null +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/enhance/DefaultClassConstructorInterceptor.java @@ -0,0 +1,37 @@ +package com.a.eye.skywalking.plugin.interceptor.enhance; + +import com.a.eye.skywalking.logging.api.ILog; +import com.a.eye.skywalking.logging.api.LogManager; +import com.a.eye.skywalking.plugin.interceptor.EnhancedClassInstanceContext; +import net.bytebuddy.implementation.bind.annotation.AllArguments; +import net.bytebuddy.implementation.bind.annotation.FieldProxy; +import net.bytebuddy.implementation.bind.annotation.RuntimeType; +import net.bytebuddy.implementation.bind.annotation.This; + +/** + * Created by xin on 2016/12/6. + */ +public class DefaultClassConstructorInterceptor { + private static ILog logger = LogManager + .getLogger(ClassConstructorInterceptor.class); + + private String instanceMethodsAroundInterceptorClassName; + + public DefaultClassConstructorInterceptor(String instanceMethodsAroundInterceptorClassName) { + this.instanceMethodsAroundInterceptorClassName = instanceMethodsAroundInterceptorClassName; + } + + @RuntimeType + public void intercept( + @This Object obj, + @FieldProxy(ClassEnhancePluginDefine.contextAttrName) FieldSetter accessor, + @AllArguments Object[] allArguments) { + try { + EnhancedClassInstanceContext context = new EnhancedClassInstanceContext(); + accessor.setValue(context); + } catch (Throwable t) { + logger.error("ClassConstructorInterceptor failure.", t); + } + + } +}