daimingzhi commented on issue #7440:
URL: https://github.com/apache/skywalking/issues/7440#issuecomment-897351911


   > Because the delegate mechanism only supports to do once
   
   I didn't fully grasp the meaning of this。but,I tested the byteBuddy API like 
this
   
   - code of agent
   
   ```java
   public class ByteBuddyAgent {
   
       public static void premain(String agentArgs, Instrumentation inst) {
           // install first time
           new AgentBuilder.Default()
               .type(named("com.easy4coding.bytebuddy.Bar"))
               .transform(new AgentBuilder.Transformer() {
                   @Override
                   public DynamicType.Builder transform(DynamicType.Builder 
builder,
                                                        TypeDescription 
typeDescription,
                                                        ClassLoader classloader,
                                                        JavaModule javaModule) {
                       return builder.method(named("m"))
                           .intercept(MethodDelegation.to(new 
BarInterceptorFirst()));
                   }
               }).installOn(inst);
                
           // install second time
           new AgentBuilder.Default()
               .type(named("com.easy4coding.bytebuddy.Bar"))
               .transform(new AgentBuilder.Transformer() {
                   @Override
                   public DynamicType.Builder transform(DynamicType.Builder 
builder,
                                                        TypeDescription 
typeDescription,
                                                        ClassLoader classloader,
                                                        JavaModule javaModule) {
                       return builder.method(named("m"))
                           .intercept(MethodDelegation.to(new 
BarInterceptorSecond()));
                   }
               }).installOn(inst);
       }
   }
   
   public class BarInterceptorFirst {
   
        @RuntimeType
        public Object intercept(@This Object obj, @AllArguments Object[] 
allArguments, @SuperCall Callable<?> zuper,
                                @Origin Method method) {
   
                System.out.println("before method first");
                Object ret = null;
                try {
                        ret = zuper.call();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                System.out.println("after method first");
   
                return ret;
        }
   
   }
   
   public class BarInterceptorSecond {
   
        @RuntimeType
        public Object intercept(@This Object obj, @AllArguments Object[] 
allArguments, @SuperCall Callable<?> zuper,
                                @Origin Method method) {
   
                System.out.println("before method Second");
                Object ret = null;
                try {
                        ret = zuper.call();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                System.out.println("after method Second");
   
                return ret;
        }
   
   }
   ```
   
   - testDemo
   
   ```java
   public class DuplicateInstallDemo {
        public static void main(String[] args) {
                new Bar().m();
        }
   }
   ```
   
   The program output is as follows:
   
   ```shell
   before method 2
   before method
   method named m invoked
   after method
   after method 2
   ```
   
   What puzzles me most about the first one is, how do I group pluginDefine?
   
   What I did in my company was adjust the directory structure of the project 
and extend the AgentClassLoader。
   
   like this:
   
   
![image-20210812130038336](https://gitee.com/easy4coding/blogImage/raw/master/image/image-20210812130038336.png)
   
   But, this way is not suitable for skywaling obviously。
   
   > You missed one important port, `EnhancedInstance#dynamicField`, which has 
only one value.
   
   I'm not really familiar with this piece of logic now.I will spend more time 
learning about it,Also, I might have to learn more about ByteBuddy


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to