This commit is contained in:
zhangxin 2016-12-06 11:42:16 +08:00
parent 72da98fbf9
commit a9c14f85e8
2 changed files with 49 additions and 6 deletions

View File

@ -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.<MethodDescription>any())
.intercept(SuperMethodCall.INSTANCE.andThen(
MethodDelegation.to(new DefaultClassConstructorInterceptor(constructorInterceptPoint.getConstructorInterceptor()))
.appendParameterBinder(FieldProxy.Binder.install(FieldGetter.class, FieldSetter.class))));
}
}

View File

@ -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);
}
}
}