public static boolean hasPossibleStaticMethod ( ClassNode cNode , String name , Expression arguments , boolean trySpread ) { int count = 0 ; boolean foundSpread = false ; if ( arguments instanceof TupleExpression ) { TupleExpression tuple = ( TupleExpression ) arguments ; for ( Expression arg : tuple . getExpressions ( ) ) { if ( arg instanceof SpreadExpression ) { foundSpread = true ; } else { count ++ ; } } } else if ( arguments instanceof MapExpression ) { count = 1 ; } for ( MethodNode method : cNode . getMethods ( name ) ) { if ( method . isStatic ( ) ) { Parameter [ ] parameters = method . getParameters ( ) ; // do fuzzy match for spread case: count will be number of non-spread args if ( trySpread && foundSpread && parameters . length >= count ) return true ; if ( parameters . length == count ) return true ; // handle varargs case if ( parameters . length > 0 && parameters [ parameters . length - 1 ] . getType ( ) . isArray ( ) ) { if ( count >= parameters . length - 1 ) return true ; // fuzzy match any spread to a varargs if ( trySpread && foundSpread ) return true ; } // handle parameters with default values int nonDefaultParameters = 0 ; for ( Parameter parameter : parameters ) { if ( ! parameter . hasInitialExpression ( ) ) { nonDefaultParameters ++ ; } } if ( count < parameters . length && nonDefaultParameters <= count ) { return true ; } // TODO handle spread with nonDefaultParams? } } return false ; }
Returns true if the given method has a possibly matching static method with the given name and arguments . Handles default arguments and optionally spread expressions .