Skip to content

Commit 45b4688

Browse files
committed
refactor: use TypeDescription
1 parent 0d5eea2 commit 45b4688

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

generator/src/main/java/com/reajason/javaweb/memshell/generator/ListenerGenerator.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import net.bytebuddy.asm.AsmVisitorWrapper;
77
import net.bytebuddy.description.modifier.Ownership;
88
import net.bytebuddy.description.modifier.Visibility;
9+
import net.bytebuddy.description.type.TypeDescription;
910
import net.bytebuddy.dynamic.DynamicType;
1011
import net.bytebuddy.implementation.FixedValue;
1112

1213
import java.util.Collections;
1314

14-
import static net.bytebuddy.matcher.ElementMatchers.named;
15+
import static net.bytebuddy.matcher.ElementMatchers.*;
1516

1617
/**
1718
* @author ReaJason
@@ -20,13 +21,6 @@
2021
public class ListenerGenerator {
2122

2223
public static DynamicType.Builder<?> build(DynamicType.Builder<?> builder, Class<?> implInterceptor, Class<?> targetClass, String newClassName) {
23-
boolean needAddGetFieldValue = false;
24-
try {
25-
targetClass.getMethod("getFieldValue", Object.class, String.class);
26-
} catch (NoSuchMethodException e) {
27-
needAddGetFieldValue = true;
28-
}
29-
3024
builder = builder.visit(new AsmVisitorWrapper.ForDeclaredMethods()
3125
.method(named("getResponseFromRequest"),
3226
new MethodCallReplaceVisitorWrapper(
@@ -36,7 +30,13 @@ public static DynamicType.Builder<?> build(DynamicType.Builder<?> builder, Class
3630
)
3731
.visit(Advice.to(implInterceptor).on(named("getResponseFromRequest")));
3832

39-
if (needAddGetFieldValue) {
33+
boolean methodNotFound = TypeDescription.ForLoadedType.of(targetClass)
34+
.getDeclaredMethods()
35+
.filter(named("getFieldValue")
36+
.and(takesArguments(Object.class, String.class)))
37+
.isEmpty();
38+
39+
if (methodNotFound) {
4040
builder = builder.defineMethod("getFieldValue", Object.class, Visibility.PUBLIC, Ownership.STATIC)
4141
.withParameters(Object.class, String.class)
4242
.intercept(FixedValue.nullValue())

0 commit comments

Comments
 (0)