public static Method findMethod ( Class clazz , String methodName ) { final List < Method > methods = getDeclaredMethodsRecursively ( clazz ) . get ( methodName ) ; if ( methods . isEmpty ( ) ) { throw new IllegalArgumentException ( clazz . getName ( ) + "::" + methodName + " not found" ) ; } final List < Method > specificMethods ; if ( methods . size ( ) > 1 ) { specificMethods = methods . stream ( ) . filter ( m -> m . getReturnType ( ) != Object . class ) . collect ( Collectors . toList ( ) ) ; } else { specificMethods = methods ; } if ( specificMethods . size ( ) != 1 ) { throw new IllegalArgumentException ( clazz . getName ( ) + "::" + methodName + " more then one method found, can not decide which one to use. " + methods ) ; } return specificMethods . get ( 0 ) ; }
Finds method with name throws exception if no method found or there are many of them .