66import net .bytebuddy .asm .AsmVisitorWrapper ;
77import net .bytebuddy .description .modifier .Ownership ;
88import net .bytebuddy .description .modifier .Visibility ;
9+ import net .bytebuddy .description .type .TypeDescription ;
910import net .bytebuddy .dynamic .DynamicType ;
1011import net .bytebuddy .implementation .FixedValue ;
1112
1213import java .util .Collections ;
1314
14- import static net .bytebuddy .matcher .ElementMatchers .named ;
15+ import static net .bytebuddy .matcher .ElementMatchers .* ;
1516
1617/**
1718 * @author ReaJason
2021public 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