Example usage for java.lang.reflect Method getParameterTypes

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

Introduction

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

Prototype

@Override
public Class<?>[] getParameterTypes() 

Source Link

Usage

From source file:com.swtxml.util.reflector.MethodQuery.java

public MethodQuery parameters(final Class<?>... signature) {
    filters.add(new IFilter<Method>() {
        public boolean match(Method method) {
            if (method.getParameterTypes().length != signature.length) {
                return false;
            }//from  w ww .  j ava 2  s .c  om
            for (int i = 0; i < signature.length; i++) {
                if (AnyType.class.isAssignableFrom(signature[i])) {
                    continue;
                } else if (!signature[i].isAssignableFrom(method.getParameterTypes()[i])) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public String toString() {
            return "parameters=\"" + Arrays.toString(signature) + "\"";
        }
    });
    return this;
}

From source file:com.glaf.core.util.ReflectUtils.java

private static Method findMethod(Class<? extends Object> clazz, String methodName, Object[] args) {
    for (Method method : clazz.getDeclaredMethods()) {
        if (method.getName().equals(methodName) && matches(method.getParameterTypes(), args)) {
            return method;
        }/*w  w  w  . j  ava  2 s.  c o  m*/
    }
    Class<?> superClass = clazz.getSuperclass();
    if (superClass != null) {
        return findMethod(superClass, methodName, args);
    }
    return null;
}

From source file:com.beinformed.framework.osgi.osgitest.base.TestSuiteBase.java

protected void initialize() {
    // Adds all methods that have a name that starts with 'test' and have only one argument of type TestMonitor
    for (Method method : this.getClass().getMethods()) {
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length == 1 && parameterTypes[0] == TestMonitor.class
                && method.getName().startsWith("test")) {
            addTest(method.getName());/*w  ww . ja  va 2 s.  com*/
        }
    }
}

From source file:com.bfd.harpc.config.spring.HarpcBeanDefinitionParser.java

/**
 * {@link#parse}//w w  w .jav a  2s .  co  m
 * <p>
 * 
 * @param element
 * @param parserContext
 * @param clazz
 * @return {@link BeanDefinition}
 */
private BeanDefinition parse(Element element, ParserContext parserContext, Class<?> clazz) {
    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(clazz);

    Method[] methods = clazz.getMethods();
    String id = StringUtils.EMPTY;
    for (Method method : methods) {
        if (method.getName().length() > 3 && method.getName().startsWith("set")
                && method.getParameterTypes().length == 1) {
            String attribute = method.getName().substring(3);
            char ch = attribute.charAt(0);
            attribute = Character.toLowerCase(ch) + attribute.substring(1);

            String value = element.getAttribute(attribute);

            if (StringUtils.isNotEmpty(value)) {
                Type type = method.getParameterTypes()[0];
                if (type == boolean.class) {
                    beanDefinition.getPropertyValues().addPropertyValue(attribute, Boolean.valueOf(value));
                } else {
                    if ("ref".equals(attribute) && parserContext.getRegistry().containsBeanDefinition(value)) {
                        beanDefinition.getPropertyValues().addPropertyValue(attribute,
                                new RuntimeBeanReference(value));
                    } else {
                        beanDefinition.getPropertyValues().addPropertyValue(attribute, value);
                        if ("id".equals(attribute)) {
                            id = value;
                        }
                    }
                }
            }
        }
    }
    parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);

    return beanDefinition;
}

From source file:eu.codesketch.scriba.rest.analyser.domain.service.introspector.jackson.JsonPropertyAnnotationIntrospector.java

private Property extractProperty(Descriptor descriptor) {
    JsonProperty jsonProperty = descriptor.getWrappedAnnotationAs(JsonProperty.class);
    String propertyName = jsonProperty.value();
    Class<?> parameterType;
    Field field = descriptor.annotatedElementAs(Field.class);
    if (null != field) {
        parameterType = field.getType();
        propertyName = "".equals(propertyName) ? field.getName() : propertyName;
    } else {/* w  ww  .j av  a 2s. c o  m*/
        Method method = descriptor.annotatedElementAs(Method.class);
        if (isSetter(method)) {
            parameterType = method.getParameterTypes()[0];
        } else {
            parameterType = method.getReturnType();
        }
        propertyName = "".equals(propertyName) ? getPropertyNamefromSetterOrGetter(method.getName())
                : propertyName;
    }
    LOGGER.debug("JsonProperty annotation has defined property name {}", propertyName);
    if (!isPrimitiveOrWrapper(parameterType)) {
        Property property = new Property(null, propertyName);
        for (Descriptor innerDescriptor : getDescriptorsForAnnotation(parameterType, JsonProperty.class)) {
            property.addProperty(extractProperty(innerDescriptor));
        }
        return property;
    } else {
        return new Property(parameterType.getName(), propertyName);
    }
}

From source file:com.glaf.core.util.ReflectUtils.java

/**
 * get method desc. "(I)I", "()V", "(Ljava/lang/String;Z)V"
 * // www . j  a v a  2 s  .co  m
 * @param m
 *            method.
 * @return desc.
 */
public static String getDescWithoutMethodName(Method m) {
    StringBuilder ret = new StringBuilder();
    ret.append('(');
    Class<?>[] parameterTypes = m.getParameterTypes();
    for (int i = 0; i < parameterTypes.length; i++)
        ret.append(getDesc(parameterTypes[i]));
    ret.append(')').append(getDesc(m.getReturnType()));
    return ret.toString();
}

From source file:com.github.carlomicieli.rest.ValidatorAspect.java

@Before("execution(* *(@com.github.carlomicieli.rest.Valid(*)))")
public void validate(JoinPoint jp) throws NoSuchMethodException {

    Set<ConstraintViolation<?>> violations = new HashSet<ConstraintViolation<?>>();

    Method methodSignature = ((MethodSignature) jp.getSignature()).getMethod();

    Method targetMethod = jp.getTarget().getClass().getMethod(methodSignature.getName(),
            methodSignature.getParameterTypes());

    Annotation[][] annotationParameters = targetMethod.getParameterAnnotations();

    for (int i = 0; i < annotationParameters.length; i++) {
        Annotation[] annotations = annotationParameters[i];
        for (Annotation a : annotations) {
            if (a.annotationType().equals(Valid.class)) {
                Object arg = jp.getArgs()[i];
                violations.addAll(validator.validate(arg));
            }//from   ww  w.  j  a va2 s. c o m
        }
    }

    if (!violations.isEmpty()) {
        throw new ConstraintViolationException(violations);
    }
}

From source file:com.glaf.core.util.ReflectUtils.java

/**
 * get method desc. int do(int arg1) => "do(I)I" void do(String arg1,boolean
 * arg2) => "do(Ljava/lang/String;Z)V"
 * // w  w  w .j av a2  s  .co  m
 * @param m
 *            method.
 * @return desc.
 */
public static String getDesc(final Method m) {
    StringBuilder ret = new StringBuilder(m.getName()).append('(');
    Class<?>[] parameterTypes = m.getParameterTypes();
    for (int i = 0; i < parameterTypes.length; i++)
        ret.append(getDesc(parameterTypes[i]));
    ret.append(')').append(getDesc(m.getReturnType()));
    return ret.toString();
}

From source file:com.googlecode.android_scripting.rpc.MethodDescriptorTest.java

private void assertDefault(Object expected, Method m, int param) {
    assertEquals(expected,/* www  .  ja v  a2 s  .  c o  m*/
            MethodDescriptor.getDefaultValue(m.getParameterTypes()[param], m.getParameterAnnotations()[param]));
}

From source file:in.hatimi.nosh.support.CommandExecutor.java

public void execute(String[] params, List<CommandSetup> setupList) {
    Object cmdObj = null;/*ww  w .  ja  v  a  2s  . co  m*/
    CmdLineManager clm = new CmdLineManager(cmdCls);
    try {
        cmdObj = cmdCls.newInstance();
        if (setupList != null) {
            for (CommandSetup cs : setupList) {
                cs.setup(cmdObj);
            }
        }
        if (!clm.applyCmdLine(cmdObj, params)) {
            clm.printUsage(createSyntax());
            return;
        }
    } catch (Exception exep) {
        exep.printStackTrace();
        LOGGER.error("unable to create/setup command", exep);
        clm.printUsage(createSyntax());
        return;
    }

    try {
        if (cmdObj instanceof CommandOutputAware) {
            ((CommandOutputAware) cmdObj).setCommandOutput(cmdOut);
        }
        Method mainMthd = findMainMethod();
        Class<?>[] paramTypes = mainMthd.getParameterTypes();
        if (paramTypes != null && paramTypes.length > 0) {
            Object args = clm.getLeftoverArgs();
            mainMthd.invoke(cmdObj, args);
        } else {
            mainMthd.invoke(cmdObj);
        }
    } catch (Exception exep) {
        LOGGER.error("execution error", exep);
    }
}