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.haulmont.cuba.core.app.AbstractBeansMetadata.java

protected List<MethodParameterInfo> getMethodParameters(Method method) {
    ArrayList<MethodParameterInfo> params = new ArrayList<>();

    Class<?>[] parameterTypes = method.getParameterTypes();

    LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
    String[] parameterNames = parameterNameDiscoverer.getParameterNames(method);

    if (parameterTypes != null) {
        for (int i = 0; i < parameterTypes.length; i++) {
            String parameterName = parameterNames != null ? parameterNames[i] : "arg" + i;
            MethodParameterInfo parameterInfo = new MethodParameterInfo(parameterTypes[i].getName(),
                    parameterName, null);
            params.add(parameterInfo);//from w w w  .ja  v  a2  s .c o m
        }
    }
    return params;
}

From source file:candr.yoclip.option.OptionPropertiesSetter.java

/**
 * Initializes the method descriptor to create an association between the bean setter {@code Method} and the
 * {@code OptionProperties} annotation.//from  w w  w. j  a  v  a2  s  . c  om
 *
 * @param optionProperties The annotation associated with the bean setter.
 * @param setter           The bean setter method annotated with {@code OptionProperties}.
 * @throws OptionsBadTypeException if the setter is not assignable to the Java {@code Map} interface.
 * @throws OptionsBadNameException if the {@link OptionProperties#name() name} is empty.
 */
protected OptionPropertiesSetter(final OptionProperties optionProperties, final Method setter) {

    super(setter);

    final Class<?>[] parameterTypes = setter.getParameterTypes();
    if (2 != parameterTypes.length) {
        throw new UnsupportedOperationException(setter.toGenericString() + " must have 2 parameters.");
    }
    if (!String.class.isAssignableFrom(parameterTypes[0])
            || !String.class.isAssignableFrom(parameterTypes[1])) {
        throw new IllegalArgumentException(setter.toGenericString() + " parameters must both be String");
    }
    if (StringUtils.isEmpty(optionProperties.name())) {
        throw new OptionsBadNameException(setter.getName() + " option name is empty.");
    }

    this.optionProperties = optionProperties;
}

From source file:com.bstek.dorado.data.method.MethodAutoMatchingUtils.java

public static String[] getParameterNames(Method method) throws SecurityException, NoSuchMethodException {
    Class<?> declaringClass = method.getDeclaringClass();
    if (ProxyBeanUtils.isProxy(declaringClass)) {
        Class<?> targetType = ProxyBeanUtils.getProxyTargetType(declaringClass);
        method = targetType.getMethod(method.getName(), method.getParameterTypes());
    }//from  w w  w .  j  a  v  a  2  s.c  o m
    return paranamer.lookupParameterNames(method);
}

From source file:com.alfresco.orm.repository.RepositoryBeanPostProcessor.java

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    Class clazz = bean.getClass();
    List<Method> repositoryMethod = ReflectionUtil.setterMethodForAnnotation(clazz,
            AlfrescoRepositoryBean.class);
    for (Method method : repositoryMethod) {
        try {/* ww  w  . j a va2s  .c om*/
            method.invoke(bean, AlfrescoRespositoryProxyFactoryBean.getAlfrescoRespositoryProxyFactoryBean()
                    .getObject(method.getParameterTypes()[0]));
        } catch (IllegalArgumentException e) {
            throw new BeansException(e.getMessage(), e) {
            };
        } catch (IllegalAccessException e) {
            throw new BeansException(e.getMessage(), e) {
            };
        } catch (InvocationTargetException e) {
            throw new BeansException(e.getMessage(), e) {
            };
        } catch (InstantiationException e) {
            throw new BeansException(e.getMessage(), e) {
            };
        }
    }

    return bean;
}

From source file:org.openmrs.module.ejemplomedellin.advice.LimitPatientSearchResultsAdvisor.java

/**
 * @see org.springframework.aop.MethodMatcher#matches(java.lang.reflect.Method, java.lang.Class)
 *//*  w  ww  .  j a  v  a2s.  c o m*/
@Override
public boolean matches(Method method, Class targetClass) {
    // only match getPatients(String, Integer, Integer)
    if (!method.getName().equals("getPatients"))
        return false;
    Class[] argTypes = method.getParameterTypes();
    return argTypes.length == 3 && argTypes[0].equals(String.class) && argTypes[1].equals(Integer.class)
            && argTypes[2].equals(Integer.class);
}

From source file:edu.asu.ser.jsonrpc.parameters.PositionalParams.java

/**
 * Converts JSONObjects from Request to Java Types  
 * @param method : method for which parameters are to be converted
 * @return Object[]: Objects converted from JSON to Java Objects
 * @exception IllegalArgumentException when request params cannot be casted to method parameters
 * @throws JsonRpcException /*  w w w. j  a  va  2 s.  c  o  m*/
 */
public Object[] getObjectsFromJSONArray(Method m) throws JsonRpcException {
    Object[] objects = new Object[params.length()];
    Class<?>[] paramClasses = m.getParameterTypes();

    //System.out.println(paramClasses[0].toString() + paramClasses[1].toString());
    for (int i = 0; i < params.length(); i++) {
        try {
            if (paramClasses[i].isPrimitive()) {
                objects[i] = params.get(i);
            } else if (PRIMITIVE_TYPES.contains(paramClasses[i])) {
                objects[i] = paramClasses[i].cast(params.get(i));
            } else {

                JSONObject jsonObj = new JSONObject(params.get(i));
                objects[i] = paramClasses[i].getConstructor(JSONObject.class).newInstance(jsonObj);
            }
        } catch (ClassCastException | JSONException | NoSuchMethodException | IllegalArgumentException
                | InstantiationException | IllegalAccessException | InvocationTargetException
                | SecurityException ex) {
            ex.printStackTrace();
            throw new JsonRpcException(RPCError.INVALID_PARAMS_ERROR);

        }

    }

    return objects;

}

From source file:org.traccar.web.CsvBuilder.java

public void addLine(Object object) {

    SortedSet<Method> methods = getSortedMethods(object);

    for (Method method : methods) {
        if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
            try {
                if (method.getReturnType().equals(boolean.class)) {
                    builder.append((Boolean) method.invoke(object));
                    addSeparator();/*from w w w .  j  ava 2 s. c o m*/
                } else if (method.getReturnType().equals(int.class)) {
                    builder.append((Integer) method.invoke(object));
                    addSeparator();
                } else if (method.getReturnType().equals(long.class)) {
                    builder.append((Long) method.invoke(object));
                    addSeparator();
                } else if (method.getReturnType().equals(double.class)) {
                    builder.append((Double) method.invoke(object));
                    addSeparator();
                } else if (method.getReturnType().equals(String.class)) {
                    builder.append((String) method.invoke(object));
                    addSeparator();
                } else if (method.getReturnType().equals(Date.class)) {
                    Date value = (Date) method.invoke(object);
                    builder.append(DATE_FORMAT.print(new DateTime(value)));
                    addSeparator();
                } else if (method.getReturnType().equals(Map.class)) {
                    Map value = (Map) method.invoke(object);
                    if (value != null) {
                        try {
                            String map = Context.getObjectMapper().writeValueAsString(value);
                            map = map.replaceAll("[\\{\\}\"]", "");
                            map = map.replaceAll(",", " ");
                            builder.append(map);
                            addSeparator();
                        } catch (JsonProcessingException e) {
                            Log.warning(e);
                        }
                    }
                }
            } catch (IllegalAccessException | InvocationTargetException error) {
                Log.warning(error);
            }
        }
    }
    addLineEnding();
}

From source file:de.matzefratze123.heavyspleef.core.event.EventListenerMethod.java

@SuppressWarnings("unchecked")
public EventListenerMethod(Object instance, Method method) {
    this.instance = instance;
    this.method = method;

    if (!method.isAccessible()) {
        method.setAccessible(true);/*  www  .j  a  v a2 s .  co m*/
    }

    Class<?>[] parameters = method.getParameterTypes();
    Validate.isTrue(parameters.length == 1,
            "method must have only one parameter which must be a subtype of GameEvent");

    Class<?> eventClass = parameters[0];
    Validate.isTrue(GameEvent.class.isAssignableFrom(eventClass),
            "First parameter of method must be a subtype of GameEvent");

    this.eventClass = (Class<? extends GameEvent>) eventClass;
}

From source file:com.github.dozermapper.core.converters.JAXBElementConverter.java

/**
 * Resolve the beanId associated to destination field name
 *
 * @return bean id//from   w w  w. j av  a  2 s .  c  om
 */
public String getBeanId() {
    Class<?> factoryClass = objectFactory(destObjClass, beanContainer).getClass();
    Class<?> destClass = null;
    String methodName = "create" + destObjClass.substring(destObjClass.lastIndexOf(".") + 1)
            + StringUtils.capitalize(destFieldName);

    try {
        Method method = ReflectionUtils.findAMethod(factoryClass, methodName, beanContainer);
        Class<?>[] parameterTypes = method.getParameterTypes();
        for (Class<?> parameterClass : parameterTypes) {
            destClass = parameterClass;
            break;
        }
    } catch (NoSuchMethodException e) {
        MappingUtils.throwMappingException(e);
    }

    return (destClass != null) ? destClass.getCanonicalName() : null;
}