ni-ze closed pull request #1851: Correct overriding parent class methods,when 
method parameters have annotations.More details to see the issue #1740
URL: https://github.com/apache/incubator-skywalking/pull/1851
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/AbstractClassEnhancePluginDefine.java
 
b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/AbstractClassEnhancePluginDefine.java
index ed975e500..6d33f8fb9 100644
--- 
a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/AbstractClassEnhancePluginDefine.java
+++ 
b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/AbstractClassEnhancePluginDefine.java
@@ -19,6 +19,7 @@
 
 package org.apache.skywalking.apm.agent.core.plugin;
 
+import net.bytebuddy.description.type.TypeDescription;
 import net.bytebuddy.dynamic.DynamicType;
 import 
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassEnhancePluginDefine;
 import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
@@ -38,22 +39,22 @@
     /**
      * Main entrance of enhancing the class.
      *
-     * @param transformClassName target class.
+     * @param typeDescription The description of the type currently being 
instrumented.
      * @param builder byte-buddy's builder to manipulate target class's 
bytecode.
      * @param classLoader load the given transformClass
      * @return the new builder, or <code>null</code> if not be enhanced.
      * @throws PluginException when set builder failure.
      */
-    public DynamicType.Builder<?> define(String transformClassName,
-        DynamicType.Builder<?> builder, ClassLoader classLoader, 
EnhanceContext context) throws PluginException {
+    public DynamicType.Builder<?> define(TypeDescription typeDescription,
+                                         DynamicType.Builder<?> builder, 
ClassLoader classLoader, EnhanceContext context) throws PluginException {
         String interceptorDefineClassName = this.getClass().getName();
 
-        if (StringUtil.isEmpty(transformClassName)) {
+        if (StringUtil.isEmpty(typeDescription.getTypeName())) {
             logger.warn("classname of being intercepted is not defined by 
{}.", interceptorDefineClassName);
             return null;
         }
 
-        logger.debug("prepare to enhance class {} by {}.", transformClassName, 
interceptorDefineClassName);
+        logger.debug("prepare to enhance class {} by {}.", 
typeDescription.getTypeName(), interceptorDefineClassName);
 
         /**
          * find witness classes for enhance class
@@ -62,7 +63,7 @@
         if (witnessClasses != null) {
             for (String witnessClass : witnessClasses) {
                 if (!WitnessClassFinder.INSTANCE.exist(witnessClass, 
classLoader)) {
-                    logger.warn("enhance class {} by plugin {} is not working. 
Because witness class {} is not existed.", transformClassName, 
interceptorDefineClassName,
+                    logger.warn("enhance class {} by plugin {} is not working. 
Because witness class {} is not existed.", typeDescription.getTypeName(), 
interceptorDefineClassName,
                         witnessClass);
                     return null;
                 }
@@ -72,15 +73,15 @@
         /**
          * find origin class source code for interceptor
          */
-        DynamicType.Builder<?> newClassBuilder = 
this.enhance(transformClassName, builder, classLoader, context);
+        DynamicType.Builder<?> newClassBuilder = this.enhance(typeDescription, 
builder, classLoader, context);
 
         context.initializationStageCompleted();
-        logger.debug("enhance class {} by {} completely.", transformClassName, 
interceptorDefineClassName);
+        logger.debug("enhance class {} by {} completely.", 
typeDescription.getTypeName(), interceptorDefineClassName);
 
         return newClassBuilder;
     }
 
-    protected abstract DynamicType.Builder<?> enhance(String 
enhanceOriginClassName,
+    protected abstract DynamicType.Builder<?> enhance(TypeDescription 
typeDefinition,
         DynamicType.Builder<?> newClassBuilder, ClassLoader classLoader, 
EnhanceContext context) throws PluginException;
 
     /**
diff --git 
a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java
 
b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java
index 654461f58..a7ce40eca 100644
--- 
a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java
+++ 
b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java
@@ -19,10 +19,12 @@
 
 package org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance;
 
+import net.bytebuddy.description.type.TypeDescription;
 import net.bytebuddy.dynamic.DynamicType;
 import net.bytebuddy.implementation.FieldAccessor;
 import net.bytebuddy.implementation.MethodDelegation;
 import net.bytebuddy.implementation.SuperMethodCall;
+import net.bytebuddy.implementation.attribute.MethodAttributeAppender;
 import net.bytebuddy.implementation.bind.annotation.Morph;
 import 
org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine;
 import org.apache.skywalking.apm.agent.core.plugin.EnhanceContext;
@@ -39,6 +41,7 @@
 import static net.bytebuddy.jar.asm.Opcodes.ACC_VOLATILE;
 import static net.bytebuddy.matcher.ElementMatchers.isStatic;
 import static net.bytebuddy.matcher.ElementMatchers.not;
+import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy;
 
 /**
  * This class controls all enhance operations, including enhance constructors, 
instance methods and static methods. All
@@ -61,17 +64,17 @@
      * Begin to define how to enhance class.
      * After invoke this method, only means definition is finished.
      *
-     * @param enhanceOriginClassName target class name
+     * @param typeDescription The description of the type currently being 
instrumented.
      * @param newClassBuilder byte-buddy's builder to manipulate class 
bytecode.
      * @return new byte-buddy's builder for further manipulation.
      */
     @Override
-    protected DynamicType.Builder<?> enhance(String enhanceOriginClassName,
-        DynamicType.Builder<?> newClassBuilder, ClassLoader classLoader,
-        EnhanceContext context) throws PluginException {
-        newClassBuilder = this.enhanceClass(enhanceOriginClassName, 
newClassBuilder, classLoader);
+    protected DynamicType.Builder<?> enhance(TypeDescription typeDescription,
+                                             DynamicType.Builder<?> 
newClassBuilder, ClassLoader classLoader,
+                                             EnhanceContext context) throws 
PluginException {
+        newClassBuilder = this.enhanceClass(typeDescription, newClassBuilder, 
classLoader);
 
-        newClassBuilder = this.enhanceInstance(enhanceOriginClassName, 
newClassBuilder, classLoader, context);
+        newClassBuilder = this.enhanceInstance(typeDescription, 
newClassBuilder, classLoader, context);
 
         return newClassBuilder;
     }
@@ -79,13 +82,13 @@
     /**
      * Enhance a class to intercept constructors and class instance methods.
      *
-     * @param enhanceOriginClassName target class name
+     * @param typeDescription The description of the type currently being 
instrumented.
      * @param newClassBuilder byte-buddy's builder to manipulate class 
bytecode.
      * @return new byte-buddy's builder for further manipulation.
      */
-    private DynamicType.Builder<?> enhanceInstance(String 
enhanceOriginClassName,
-        DynamicType.Builder<?> newClassBuilder, ClassLoader classLoader,
-        EnhanceContext context) throws PluginException {
+    private DynamicType.Builder<?> enhanceInstance(TypeDescription 
typeDescription,
+                                                   DynamicType.Builder<?> 
newClassBuilder, ClassLoader classLoader,
+                                                   EnhanceContext context) 
throws PluginException {
         ConstructorInterceptPoint[] constructorInterceptPoints = 
getConstructorsInterceptPoints();
         InstanceMethodsInterceptPoint[] instanceMethodsInterceptPoints = 
getInstanceMethodsInterceptPoints();
 
@@ -117,8 +120,8 @@
          */
         if (!context.isObjectExtended()) {
             newClassBuilder = newClassBuilder.defineField(CONTEXT_ATTR_NAME, 
Object.class, ACC_PRIVATE | ACC_VOLATILE)
-                .implement(EnhancedInstance.class)
-                .intercept(FieldAccessor.ofField(CONTEXT_ATTR_NAME));
+                    .implement(EnhancedInstance.class)
+                    .intercept(FieldAccessor.ofField(CONTEXT_ATTR_NAME));
             context.extendObjectCompleted();
         }
 
@@ -128,9 +131,9 @@
         if (existedConstructorInterceptPoint) {
             for (ConstructorInterceptPoint constructorInterceptPoint : 
constructorInterceptPoints) {
                 newClassBuilder = 
newClassBuilder.constructor(constructorInterceptPoint.getConstructorMatcher()).intercept(SuperMethodCall.INSTANCE
-                    .andThen(MethodDelegation.withDefaultConfiguration()
-                        .to(new 
ConstructorInter(constructorInterceptPoint.getConstructorInterceptor(), 
classLoader))
-                    )
+                        .andThen(MethodDelegation.withDefaultConfiguration()
+                                .to(new 
ConstructorInter(constructorInterceptPoint.getConstructorInterceptor(), 
classLoader))
+                        )
                 );
             }
         }
@@ -142,26 +145,26 @@
             for (InstanceMethodsInterceptPoint instanceMethodsInterceptPoint : 
instanceMethodsInterceptPoints) {
                 String interceptor = 
instanceMethodsInterceptPoint.getMethodsInterceptor();
                 if (StringUtil.isEmpty(interceptor)) {
-                    throw new EnhanceException("no 
InstanceMethodsAroundInterceptor define to enhance class " + 
enhanceOriginClassName);
+                    throw new EnhanceException("no 
InstanceMethodsAroundInterceptor define to enhance class " + 
typeDescription.getTypeName());
                 }
 
                 if (instanceMethodsInterceptPoint.isOverrideArgs()) {
-                    newClassBuilder =
-                        
newClassBuilder.method(not(isStatic()).and(instanceMethodsInterceptPoint.getMethodsMatcher()))
-                            .intercept(
-                                MethodDelegation.withDefaultConfiguration()
-                                    .withBinders(
-                                        
Morph.Binder.install(OverrideCallable.class)
-                                    )
-                                    .to(new 
InstMethodsInterWithOverrideArgs(interceptor, classLoader))
-                            );
+                    newClassBuilder = 
newClassBuilder.method(isDeclaredBy(typeDescription)
+                            
.and(not(isStatic()).and(instanceMethodsInterceptPoint.getMethodsMatcher())))
+                            
.intercept(MethodDelegation.withDefaultConfiguration().withBinders(Morph.Binder.install(OverrideCallable.class)).to(new
 InstMethodsInterWithOverrideArgs(interceptor, classLoader)))
+
+                            .method(not(isDeclaredBy(typeDescription))
+                                    
.and(not(isStatic()).and(instanceMethodsInterceptPoint.getMethodsMatcher())))
+                            
.intercept(MethodDelegation.withDefaultConfiguration().withBinders(Morph.Binder.install(OverrideCallable.class)).to(new
 InstMethodsInterWithOverrideArgs(interceptor, classLoader)))
+                            
.attribute(MethodAttributeAppender.ForInstrumentedMethod.EXCLUDING_RECEIVER);
                 } else {
-                    newClassBuilder =
-                        
newClassBuilder.method(not(isStatic()).and(instanceMethodsInterceptPoint.getMethodsMatcher()))
-                            .intercept(
-                                MethodDelegation.withDefaultConfiguration()
-                                    .to(new InstMethodsInter(interceptor, 
classLoader))
-                            );
+                    newClassBuilder = 
newClassBuilder.method(isDeclaredBy(typeDescription)
+                            
.and(not(isStatic()).and(instanceMethodsInterceptPoint.getMethodsMatcher())))
+                            
.intercept(MethodDelegation.withDefaultConfiguration().to(new 
InstMethodsInter(interceptor, classLoader)))
+                            .method(not(isDeclaredBy(typeDescription))
+                                    
.and(not(isStatic()).and(instanceMethodsInterceptPoint.getMethodsMatcher())))
+                            
.intercept(MethodDelegation.withDefaultConfiguration().to(new 
InstMethodsInter(interceptor, classLoader)))
+                            
.attribute(MethodAttributeAppender.ForInstrumentedMethod.EXCLUDING_RECEIVER);
                 }
             }
         }
@@ -186,12 +189,12 @@
     /**
      * Enhance a class to intercept class static methods.
      *
-     * @param enhanceOriginClassName target class name
+     * @param typeDescription The description of the type currently being 
instrumented.
      * @param newClassBuilder byte-buddy's builder to manipulate class 
bytecode.
      * @return new byte-buddy's builder for further manipulation.
      */
-    private DynamicType.Builder<?> enhanceClass(String enhanceOriginClassName,
-        DynamicType.Builder<?> newClassBuilder, ClassLoader classLoader) 
throws PluginException {
+    private DynamicType.Builder<?> enhanceClass(TypeDescription 
typeDescription,
+                                                DynamicType.Builder<?> 
newClassBuilder, ClassLoader classLoader) throws PluginException {
         StaticMethodsInterceptPoint[] staticMethodsInterceptPoints = 
getStaticMethodsInterceptPoints();
 
         if (staticMethodsInterceptPoints == null || 
staticMethodsInterceptPoints.length == 0) {
@@ -201,24 +204,26 @@
         for (StaticMethodsInterceptPoint staticMethodsInterceptPoint : 
staticMethodsInterceptPoints) {
             String interceptor = 
staticMethodsInterceptPoint.getMethodsInterceptor();
             if (StringUtil.isEmpty(interceptor)) {
-                throw new EnhanceException("no StaticMethodsAroundInterceptor 
define to enhance class " + enhanceOriginClassName);
+                throw new EnhanceException("no StaticMethodsAroundInterceptor 
define to enhance class " + typeDescription.getTypeName());
             }
 
             if (staticMethodsInterceptPoint.isOverrideArgs()) {
-                newClassBuilder = 
newClassBuilder.method(isStatic().and(staticMethodsInterceptPoint.getMethodsMatcher()))
-                    .intercept(
-                        MethodDelegation.withDefaultConfiguration()
-                            .withBinders(
-                                Morph.Binder.install(OverrideCallable.class)
-                            )
-                            .to(new 
StaticMethodsInterWithOverrideArgs(interceptor))
-                    );
+                newClassBuilder = 
newClassBuilder.method(not(isDeclaredBy(typeDescription))
+                        
.and(isStatic().and(staticMethodsInterceptPoint.getMethodsMatcher())))
+                        .intercept(
+                                MethodDelegation.withDefaultConfiguration()
+                                        .withBinders(
+                                                
Morph.Binder.install(OverrideCallable.class)
+                                        )
+                                        .to(new 
StaticMethodsInterWithOverrideArgs(interceptor))
+                        
).attribute(MethodAttributeAppender.ForInstrumentedMethod.EXCLUDING_RECEIVER);
             } else {
-                newClassBuilder = 
newClassBuilder.method(isStatic().and(staticMethodsInterceptPoint.getMethodsMatcher()))
-                    .intercept(
-                        MethodDelegation.withDefaultConfiguration()
-                            .to(new StaticMethodsInter(interceptor))
-                    );
+                newClassBuilder = 
newClassBuilder.method(not(isDeclaredBy(typeDescription))
+                        
.and(isStatic().and(staticMethodsInterceptPoint.getMethodsMatcher())))
+                        .intercept(
+                                MethodDelegation.withDefaultConfiguration()
+                                        .to(new 
StaticMethodsInter(interceptor))
+                        
).attribute(MethodAttributeAppender.ForInstrumentedMethod.EXCLUDING_RECEIVER);
             }
 
         }
diff --git 
a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java
 
b/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java
index c6ca2ca20..be5720b33 100644
--- 
a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java
+++ 
b/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java
@@ -114,7 +114,7 @@ public static void premain(String agentArgs, 
Instrumentation instrumentation) th
                 DynamicType.Builder<?> newBuilder = builder;
                 EnhanceContext context = new EnhanceContext();
                 for (AbstractClassEnhancePluginDefine define : pluginDefines) {
-                    DynamicType.Builder<?> possibleNewBuilder = 
define.define(typeDescription.getTypeName(), newBuilder, classLoader, context);
+                    DynamicType.Builder<?> possibleNewBuilder = 
define.define(typeDescription, newBuilder, classLoader, context);
                     if (possibleNewBuilder != null) {
                         newBuilder = possibleNewBuilder;
                     }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to