增强核心逻辑

This commit is contained in:
kazusa 2025-07-23 15:09:43 +08:00
parent 9ec0eb009f
commit 12e30dfe34
4 changed files with 6 additions and 0 deletions

View File

@ -125,6 +125,7 @@ public abstract class AbstractClassEnhancePluginDefine {
*/
protected DynamicType.Builder<?> enhance(TypeDescription typeDescription, DynamicType.Builder<?> newClassBuilder,
ClassLoader classLoader, EnhanceContext context) throws PluginException {
// 增强静态方法
newClassBuilder = this.enhanceClass(typeDescription, newClassBuilder, classLoader);
newClassBuilder = this.enhanceInstance(typeDescription, newClassBuilder, classLoader, context);

View File

@ -114,6 +114,7 @@ public abstract class ClassEnhancePluginDefine extends AbstractClassEnhancePlugi
/**
* 2. enhance constructors
*/
//构造器增强
if (existedConstructorInterceptPoint) {
for (ConstructorInterceptPoint constructorInterceptPoint : constructorInterceptPoints) {
String constructorInterceptor = constructorInterceptPoint.getConstructorInterceptor();
@ -157,6 +158,7 @@ public abstract class ClassEnhancePluginDefine extends AbstractClassEnhancePlugi
newClassBuilder = newClassBuilder.method(junction)
.intercept(MethodDelegation.withDefaultConfiguration()
.withBinders(Morph.Binder.install(OverrideCallable.class))
// 进行方法增强的核心MethodDelegation.to()将目标方法执行委托给拦截器
.to(new InstMethodsInterWithOverrideArgs(getPluginName(), interceptor, classLoader), delegateNamingResolver.resolve(instanceMethodsInterceptPoint)));
}
} else {

View File

@ -70,6 +70,7 @@ public class InstMethodsInterWithOverrideArgs {
* @throws Exception only throw exception because of zuper.call() or unexpected exception in sky-walking ( This is a
* bug, if anything triggers this condition ).
*/
// 通过@RuntimeType注解,能够动态匹配目标类的所有方法,从而进行拦截
@RuntimeType
public Object intercept(@This Object obj, @AllArguments Object[] allArguments, @Origin Method method,
@Morph OverrideCallable zuper) throws Throwable {

View File

@ -111,6 +111,7 @@ public class SkyWalkingAgent {
}
try {
// 3.启动上报等核心服务
ServiceManager.INSTANCE.boot();
} catch (Exception e) {
LOGGER.error(e, "Skywalking agent boot failure.");
@ -150,6 +151,7 @@ public class SkyWalkingAgent {
}
agentBuilder.type(pluginFinder.buildMatch())
// 注入增强字节码
.transform(new Transformer(pluginFinder))
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.with(new RedefinitionListener())