You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by lu...@apache.org on 2023/04/14 01:36:13 UTC

[skywalking-java] 01/01: exclude synethetic methods for WitnessMethod

This is an automated email from the ASF dual-hosted git repository.

lujiajing pushed a commit to branch exclude-synthetic-witness-methods
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git

commit f622b106205060ec2ea35e1b5065c8808c9ade41
Author: Megrez Lu <lu...@gmail.com>
AuthorDate: Fri Apr 14 09:35:58 2023 +0800

    exclude synethetic methods for WitnessMethod
    
    Signed-off-by: Megrez Lu <lu...@gmail.com>
---
 CHANGES.md                                           |  1 +
 .../apm/agent/core/plugin/WitnessMethod.java         | 10 ++++++++--
 .../apm/agent/core/plugin/witness/WitnessTest.java   | 20 ++++++++++++++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index f3ea698b8b..f910d6321e 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -5,6 +5,7 @@ Release Notes.
 8.16.0
 ------------------
 
+* Exclude `synthetic` methods for the WitnessMethod mechanism
 
 #### Documentation
 
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/WitnessMethod.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/WitnessMethod.java
index d1b508695c..73c9379101 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/WitnessMethod.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/WitnessMethod.java
@@ -18,17 +18,19 @@
 
 package org.apache.skywalking.apm.agent.core.plugin;
 
+import com.google.common.base.Preconditions;
 import lombok.Getter;
-import lombok.RequiredArgsConstructor;
 import lombok.ToString;
 import net.bytebuddy.description.method.MethodDescription;
 import net.bytebuddy.matcher.ElementMatcher;
 
+import static net.bytebuddy.matcher.ElementMatchers.isSynthetic;
+import static net.bytebuddy.matcher.ElementMatchers.not;
+
 /**
  * Witness Method for plugin activation
  */
 @ToString
-@RequiredArgsConstructor
 public class WitnessMethod {
 
     /**
@@ -42,4 +44,8 @@ public class WitnessMethod {
     @Getter
     private final ElementMatcher<? super MethodDescription.InDefinedShape> elementMatcher;
 
+    public WitnessMethod(String declaringClassName, ElementMatcher.Junction<? super MethodDescription.InDefinedShape> elementMatcher) {
+        this.declaringClassName = declaringClassName;
+        this.elementMatcher = Preconditions.checkNotNull(elementMatcher).and(not(isSynthetic()));
+    }
 }
diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/plugin/witness/WitnessTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/plugin/witness/WitnessTest.java
index 6a5a751e77..4eab65d4a8 100644
--- a/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/plugin/witness/WitnessTest.java
+++ b/apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/plugin/witness/WitnessTest.java
@@ -21,6 +21,7 @@ package org.apache.skywalking.apm.agent.core.plugin.witness;
 import net.bytebuddy.description.method.MethodDescription;
 import net.bytebuddy.matcher.ElementMatcher;
 import net.bytebuddy.matcher.ElementMatchers;
+import net.bytebuddy.pool.TypePool;
 import org.apache.skywalking.apm.agent.core.plugin.WitnessFinder;
 import org.apache.skywalking.apm.agent.core.plugin.WitnessMethod;
 import org.junit.Assert;
@@ -60,8 +61,27 @@ public class WitnessTest {
         Assert.assertTrue(finder.exist(witnessMethod, this.getClass().getClassLoader()));
     }
 
+    @Test
+    public void testSyntheticMethods() {
+        // public void org.apache.skywalking.apm.agent.core.plugin.witness.WitnessTest$Child.foo()
+        Assert.assertEquals(Child.class.getDeclaredMethods().length, 1);
+        // with an additional synthetic constructor
+        Assert.assertEquals(TypePool.Default.ofSystemLoader().describe(className + "$Child").resolve().getDeclaredMethods().size(), 2);
+        WitnessMethod witnessMethod = new WitnessMethod(className + "$Child", ElementMatchers.named("foo"));
+        Assert.assertTrue(finder.exist(className + "$Child", this.getClass().getClassLoader()));
+        Assert.assertFalse(finder.exist(witnessMethod, this.getClass().getClassLoader()));
+    }
+
     public List<Map<String, Object>> foo(List<Map<String, Object>> param, String s) {
         return null;
     }
 
+    public static class Child extends Base {
+
+    }
+
+    static class Base {
+        public void foo() {
+        }
+    }
 }