Skip to content

Commit e08c458

Browse files
Exclude synthetic methods for WitnessMethod (#504)
1 parent 7a6334d commit e08c458

4 files changed

Lines changed: 36 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Release Notes.
55
8.16.0
66
------------------
77

8+
* Exclude `synthetic` methods for the WitnessMethod mechanism
89

910
#### Documentation
1011

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/WitnessMethod.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@
1818

1919
package org.apache.skywalking.apm.agent.core.plugin;
2020

21+
import com.google.common.base.Preconditions;
2122
import lombok.Getter;
22-
import lombok.RequiredArgsConstructor;
2323
import lombok.ToString;
2424
import net.bytebuddy.description.method.MethodDescription;
2525
import net.bytebuddy.matcher.ElementMatcher;
2626

27+
import static net.bytebuddy.matcher.ElementMatchers.isSynthetic;
28+
import static net.bytebuddy.matcher.ElementMatchers.not;
29+
2730
/**
2831
* Witness Method for plugin activation
2932
*/
3033
@ToString
31-
@RequiredArgsConstructor
3234
public class WitnessMethod {
3335

3436
/**
@@ -42,4 +44,8 @@ public class WitnessMethod {
4244
@Getter
4345
private final ElementMatcher<? super MethodDescription.InDefinedShape> elementMatcher;
4446

47+
public WitnessMethod(String declaringClassName, ElementMatcher.Junction<? super MethodDescription.InDefinedShape> elementMatcher) {
48+
this.declaringClassName = declaringClassName;
49+
this.elementMatcher = Preconditions.checkNotNull(elementMatcher).and(not(isSynthetic()));
50+
}
4551
}

apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/plugin/witness/WitnessTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import net.bytebuddy.description.method.MethodDescription;
2222
import net.bytebuddy.matcher.ElementMatcher;
2323
import net.bytebuddy.matcher.ElementMatchers;
24+
import net.bytebuddy.pool.TypePool;
2425
import org.apache.skywalking.apm.agent.core.plugin.WitnessFinder;
2526
import org.apache.skywalking.apm.agent.core.plugin.WitnessMethod;
2627
import org.junit.Assert;
@@ -60,8 +61,27 @@ public void testWitnessMethodOnlyUsingName() {
6061
Assert.assertTrue(finder.exist(witnessMethod, this.getClass().getClassLoader()));
6162
}
6263

64+
@Test
65+
public void testSyntheticMethods() {
66+
// public void org.apache.skywalking.apm.agent.core.plugin.witness.WitnessTest$Child.foo()
67+
Assert.assertEquals(Child.class.getDeclaredMethods().length, 1);
68+
// with an additional synthetic constructor
69+
Assert.assertEquals(TypePool.Default.ofSystemLoader().describe(className + "$Child").resolve().getDeclaredMethods().size(), 2);
70+
WitnessMethod witnessMethod = new WitnessMethod(className + "$Child", ElementMatchers.named("foo"));
71+
Assert.assertTrue(finder.exist(className + "$Child", this.getClass().getClassLoader()));
72+
Assert.assertFalse(finder.exist(witnessMethod, this.getClass().getClassLoader()));
73+
}
74+
6375
public List<Map<String, Object>> foo(List<Map<String, Object>> param, String s) {
6476
return null;
6577
}
6678

79+
public static class Child extends Base {
80+
81+
}
82+
83+
static class Base {
84+
public void foo() {
85+
}
86+
}
6787
}

test/plugin/scenarios/shenyu-2.4.x-sofarpc-scenario/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@
6363
<type>pom</type>
6464
<scope>import</scope>
6565
</dependency>
66+
<dependency>
67+
<groupId>io.grpc</groupId>
68+
<artifactId>grpc-bom</artifactId>
69+
<version>1.28.1</version>
70+
<type>pom</type>
71+
<scope>import</scope>
72+
</dependency>
6673
<dependency>
6774
<groupId>org.apache.shenyu</groupId>
6875
<artifactId>shenyu-spring-boot-starter-gateway</artifactId>
@@ -103,7 +110,6 @@
103110
</dependencies>
104111
</dependencyManagement>
105112

106-
107113
<build>
108114
<finalName>shenyu-2.4.x-sofarpc-scenario</finalName>
109115
<plugins>

0 commit comments

Comments
 (0)