Example usage for java.lang.reflect Method getParameterCount

List of usage examples for java.lang.reflect Method getParameterCount

Introduction

In this page you can find the example usage for java.lang.reflect Method getParameterCount.

Prototype

public int getParameterCount() 

Source Link

Usage

From source file:net.radai.beanz.util.ReflectionUtil.java

public static String prettyPrint(Method method) {
    StringBuilder sb = new StringBuilder();
    Class<?> declaredOn = method.getDeclaringClass();
    sb.append(prettyPrint(declaredOn)).append(".").append(method.getName()).append("(");
    if (method.getParameterCount() > 0) {
        for (Type paramType : method.getGenericParameterTypes()) {
            sb.append(prettyPrint(paramType)).append(", ");
        }//from  w w w . jav a  2 s .c  o  m
        sb.delete(sb.length() - 2, sb.length());
    }
    sb.append(")");
    return sb.toString();
}

From source file:org.springframework.beans.BeanUtils.java

/**
 * Find a method with the given method name and minimal parameters (best case: none)
 * in the given list of methods.//w  w  w . j a  v  a 2 s .  c o m
 * @param methods the methods to check
 * @param methodName the name of the method to find
 * @return the Method object, or {@code null} if not found
 * @throws IllegalArgumentException if methods of the given name were found but
 * could not be resolved to a unique method with minimal parameters
 */
@Nullable
public static Method findMethodWithMinimalParameters(Method[] methods, String methodName)
        throws IllegalArgumentException {

    Method targetMethod = null;
    int numMethodsFoundWithCurrentMinimumArgs = 0;
    for (Method method : methods) {
        if (method.getName().equals(methodName)) {
            int numParams = method.getParameterCount();
            if (targetMethod == null || numParams < targetMethod.getParameterCount()) {
                targetMethod = method;
                numMethodsFoundWithCurrentMinimumArgs = 1;
            } else if (!method.isBridge() && targetMethod.getParameterCount() == numParams) {
                if (targetMethod.isBridge()) {
                    // Prefer regular method over bridge...
                    targetMethod = method;
                } else {
                    // Additional candidate with same length
                    numMethodsFoundWithCurrentMinimumArgs++;
                }
            }
        }
    }
    if (numMethodsFoundWithCurrentMinimumArgs > 1) {
        throw new IllegalArgumentException("Cannot resolve method '" + methodName
                + "' to a unique method. Attempted to resolve to overloaded method with "
                + "the least number of parameters but there were " + numMethodsFoundWithCurrentMinimumArgs
                + " candidates.");
    }
    return targetMethod;
}

From source file:org.springframework.cloud.stream.reactive.StreamEmitterAnnotationBeanPostProcessor.java

private static void validateStreamEmitterMethod(Method method, int outputAnnotationCount,
        String methodAnnotatedOutboundName) {

    if (StringUtils.hasText(methodAnnotatedOutboundName)) {
        Assert.isTrue(outputAnnotationCount == 0, StreamEmitterErrorMessages.INVALID_OUTPUT_METHOD_PARAMETERS);
    } else {//from ww w. jav a  2s.  co m
        Assert.isTrue(outputAnnotationCount > 0, StreamEmitterErrorMessages.NO_OUTPUT_SPECIFIED);
    }

    if (!method.getReturnType().equals(Void.TYPE)) {
        Assert.isTrue(StringUtils.hasText(methodAnnotatedOutboundName),
                StreamEmitterErrorMessages.RETURN_TYPE_NO_OUTBOUND_SPECIFIED);
        Assert.isTrue(method.getParameterCount() == 0, StreamEmitterErrorMessages.RETURN_TYPE_METHOD_ARGUMENTS);
    } else {
        if (!StringUtils.hasText(methodAnnotatedOutboundName)) {
            int methodArgumentsLength = method.getParameterTypes().length;
            for (int parameterIndex = 0; parameterIndex < methodArgumentsLength; parameterIndex++) {
                MethodParameter methodParameter = new MethodParameter(method, parameterIndex);
                if (methodParameter.hasParameterAnnotation(Output.class)) {
                    String outboundName = (String) AnnotationUtils
                            .getValue(methodParameter.getParameterAnnotation(Output.class));
                    Assert.isTrue(StringUtils.hasText(outboundName),
                            StreamEmitterErrorMessages.INVALID_OUTBOUND_NAME);
                } else {
                    throw new IllegalArgumentException(
                            StreamEmitterErrorMessages.OUTPUT_ANNOTATION_MISSING_ON_METHOD_PARAMETERS_VOID_RETURN_TYPE);
                }
            }
        }
    }
}

From source file:org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.java

private static UriComponents applyContributors(UriComponentsBuilder builder, Method method, Object... args) {
    CompositeUriComponentsContributor contributor = getConfiguredUriComponentsContributor();
    if (contributor == null) {
        logger.debug("Using default CompositeUriComponentsContributor");
        contributor = defaultUriComponentsContributor;
    }/*from   w ww .ja  v a2s . c om*/

    int paramCount = method.getParameterCount();
    int argCount = args.length;
    if (paramCount != argCount) {
        throw new IllegalArgumentException("Number of method parameters " + paramCount
                + " does not match number of argument values " + argCount);
    }

    final Map<String, Object> uriVars = new HashMap<>();
    for (int i = 0; i < paramCount; i++) {
        MethodParameter param = new SynthesizingMethodParameter(method, i);
        param.initParameterNameDiscovery(parameterNameDiscoverer);
        contributor.contributeMethodArgument(param, args[i], builder, uriVars);
    }

    // We may not have all URI var values, expand only what we have
    return builder.build().expand(new UriComponents.UriTemplateVariables() {
        @Override
        public Object getValue(@Nullable String name) {
            return uriVars.containsKey(name) ? uriVars.get(name)
                    : UriComponents.UriTemplateVariables.SKIP_VALUE;
        }
    });
}

From source file:org.springframework.boot.test.context.assertj.AssertProviderApplicationContextInvocationHandler.java

private boolean isCloseMethod(Method method) {
    return ("close".equals(method.getName()) && method.getParameterCount() == 0);
}

From source file:org.springframework.boot.test.context.assertj.AssertProviderApplicationContextInvocationHandler.java

private boolean isToString(Method method) {
    return ("toString".equals(method.getName()) && method.getParameterCount() == 0);
}

From source file:com.haulmont.cuba.gui.xml.DeclarativeColumnGenerator.java

protected Method findGeneratorMethod(Class cls, String methodName) {
    Method exactMethod = MethodUtils.getAccessibleMethod(cls, methodName, Entity.class);
    if (exactMethod != null) {
        return exactMethod;
    }/*from   w  w w .  jav  a2  s.c o  m*/

    // search through all methods
    Method[] methods = cls.getMethods();
    for (Method availableMethod : methods) {
        if (availableMethod.getName().equals(methodName)) {
            if (availableMethod.getParameterCount() == 1
                    && Component.class.isAssignableFrom(availableMethod.getReturnType())) {
                if (Entity.class.isAssignableFrom(availableMethod.getParameterTypes()[0])) {
                    // get accessible version of method
                    return MethodUtils.getAccessibleMethod(availableMethod);
                }
            }
        }
    }
    return null;
}

From source file:org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.java

private void invokeHandlerMethod(MethodInvocation mi, Throwable ex, Method method) throws Throwable {
    Object[] handlerArgs;// w ww . j  a va  2  s  .  c o m
    if (method.getParameterCount() == 1) {
        handlerArgs = new Object[] { ex };
    } else {
        handlerArgs = new Object[] { mi.getMethod(), mi.getArguments(), mi.getThis(), ex };
    }
    try {
        method.invoke(this.throwsAdvice, handlerArgs);
    } catch (InvocationTargetException targetEx) {
        throw targetEx.getTargetException();
    }
}

From source file:org.springframework.boot.test.context.assertj.AssertProviderApplicationContextInvocationHandler.java

private boolean isAssertThat(Method method) {
    return ("assertThat".equals(method.getName()) && method.getParameterCount() == 0);
}

From source file:com.googlecode.jdbcproc.daofactory.impl.block.service.OutputParametersGetterBlockServiceImpl.java

private int findEntityParameterIndex(Method daoMethod) {
    int entityParameterIndex = -1;
    for (int i = 0; i < daoMethod.getParameterCount(); i++) {
        if (!BlockFactoryUtils.isSimpleOrListType(daoMethod.getParameterTypes()[i])) {
            entityParameterIndex = i;//from   www . j a  v a2  s .co  m
            break;
        }
    }
    Assert.isTrue(entityParameterIndex >= 0,
            "Did not find an entity parameter, although this is an entity (probably with lists)");
    return entityParameterIndex;
}