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.mawujun.util.AnnotationUtils.java

/**
 * <p>Generate a string representation of an Annotation, as suggested by
 * {@link Annotation#toString()}.</p>
 *
 * @param a the annotation of which a string representation is desired
 * @return the standard string representation of an annotation, not
 * {@code null}/*from ww  w  .  j a va2  s . co  m*/
 */
public static String toString(final Annotation a) {
    ToStringBuilder builder = new ToStringBuilder(a, TO_STRING_STYLE);
    for (Method m : a.annotationType().getDeclaredMethods()) {
        if (m.getParameterTypes().length > 0) {
            continue; //wtf?
        }
        try {
            builder.append(m.getName(), m.invoke(a));
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
    return builder.build();
}

From source file:com.qmetry.qaf.automation.testng.pro.DataProviderUtil.java

@SuppressWarnings("unchecked")
@DataProvider(name = "isfw_property")
public static final Object[][] getDataFromProp(Method method) {
    List<Object[]> mapData = getDataSetAsMap(getParameters(method).get(params.KEY.name()));
    Class<?> types[] = method.getParameterTypes();
    if ((types.length == 1) && Map.class.isAssignableFrom(types[0])) {
        return mapData.toArray(new Object[][] {});
    } else {/*from  w  w  w.  j  a  v a  2s . c o  m*/
        List<Object[]> data = new ArrayList<Object[]>();
        Iterator<Object[]> mapDataIter = mapData.iterator();
        while (mapDataIter.hasNext()) {
            Map<String, String> map = (Map<String, String>) mapDataIter.next()[0];
            data.add(map.values().toArray());
        }
        return data.toArray(new Object[][] {});
    }
}

From source file:org.synyx.hades.util.ClassUtils.java

/**
 * Returns the number of occurences of the given type in the given
 * {@link Method}s parameters.//  ww  w  . j a v a  2  s  . com
 * 
 * @param method
 * @param type
 * @return
 */
public static int getNumberOfOccurences(Method method, Class<?> type) {

    int result = 0;
    for (Class<?> clazz : method.getParameterTypes()) {
        if (type.equals(clazz)) {
            result++;
        }
    }

    return result;
}

From source file:com.google.api.server.spi.request.ServletRequestParamReader.java

protected static List<String> getParameterNames(EndpointMethod endpointMethod)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    List<String> parameterNames = endpointMethod.getParameterNames();
    if (parameterNames == null) {
        Method method = endpointMethod.getMethod();
        parameterNames = new ArrayList<>();
        for (int parameter = 0; parameter < method.getParameterTypes().length; parameter++) {
            Annotation annotation = AnnotationUtil.getNamedParameter(method, parameter, Named.class);
            if (annotation == null) {
                parameterNames.add(null);
            } else {
                parameterNames.add((String) annotation.getClass().getMethod("value").invoke(annotation));
            }/*from  ww w .j  a  va  2  s .  c o  m*/
        }
        endpointMethod.setParameterNames(parameterNames);
    }
    return parameterNames;
}

From source file:com.github.cherimojava.data.mongo.entity.EntityUtils.java

/**
 * returns the getter method belonging to the given setter method
 *
 * @param m setter method for which a matching getter method shall be found
 * @return getter method belonging to the given Setter method
 * @throws java.lang.IllegalArgumentException if the given method has no matching adder method
 *//*from   ww  w.  j a  v a 2  s  .  co  m*/
static Method getGetterFromSetter(Method m) {
    try {
        Method getter = m.getDeclaringClass().getMethod(m.getName().replaceFirst("s", "g"));
        checkArgument(getter.getReturnType().equals(m.getParameterTypes()[0]),
                "You can only declare setter methods if there's a matching getter. Found %s without getter",
                m.getName());
        return getter;
    } catch (NoSuchMethodException e) {
        try {
            // check if there's a is method available
            Method isser = m.getDeclaringClass().getMethod(m.getName().replaceFirst("set", "is"));
            // check that both have boolean as type
            checkArgument(boolean.class.equals(Primitives.unwrap(isser.getReturnType()))
                    && boolean.class.equals(Primitives.unwrap(m.getParameterTypes()[0])));
            return isser;
        } catch (NoSuchMethodException e1) {
            // if neither get nor is can be found throw an exception
            throw new IllegalArgumentException(
                    format("Method %s has no corresponding get/is method", m.getName()));
        }
    }
}

From source file:com.mawujun.util.AnnotationUtils.java

/**
 * <p>Checks if two annotations are equal using the criteria for equality
 * presented in the {@link Annotation#equals(Object)} API docs.</p>
 *
 * @param a1 the first Annotation to compare, {@code null} returns
 * {@code false} unless both are {@code null}
 * @param a2 the second Annotation to compare, {@code null} returns
 * {@code false} unless both are {@code null}
 * @return {@code true} if the two annotations are {@code equal} or both
 * {@code null}/*from w w w  . j  av  a 2s.c o  m*/
 */
public static boolean equals(Annotation a1, Annotation a2) {
    if (a1 == a2) {
        return true;
    }
    if (a1 == null || a2 == null) {
        return false;
    }
    Class<? extends Annotation> type = a1.annotationType();
    Class<? extends Annotation> type2 = a2.annotationType();
    Validate.notNull(type, "Annotation %s with null annotationType()", a1);
    Validate.notNull(type2, "Annotation %s with null annotationType()", a2);
    if (!type.equals(type2)) {
        return false;
    }
    try {
        for (Method m : type.getDeclaredMethods()) {
            if (m.getParameterTypes().length == 0 && isValidAnnotationMemberType(m.getReturnType())) {
                Object v1 = m.invoke(a1);
                Object v2 = m.invoke(a2);
                if (!memberEquals(m.getReturnType(), v1, v2)) {
                    return false;
                }
            }
        }
    } catch (IllegalAccessException ex) {
        return false;
    } catch (InvocationTargetException ex) {
        return false;
    }
    return true;
}

From source file:se.trillian.goodies.spring.DomainObjectFactoryFactoryBean.java

private static Constructor<?> findMatchingConstructor(Class<?> clazz, Method m) {
    LinkedList<Constructor<?>> constructors = new LinkedList<Constructor<?>>();
    Constructor<?> directMatch = null;
    for (Constructor<?> c : clazz.getDeclaredConstructors()) {
        if (isParameterTypesPrefix(m.getParameterTypes(), c.getParameterTypes())) {
            constructors.add(c);//  w  ww  . j av a 2  s. c  om
            if (directMatch == null && isParameterTypesPrefix(c.getParameterTypes(), m.getParameterTypes())) {
                directMatch = c;
            }
        }
    }
    if (constructors.isEmpty()) {
        throw new IllegalArgumentException("No matching constructor found in " + "implementation class '"
                + clazz + "' for factory method '" + m + "'");
    }
    if (constructors.size() > 1) {
        if (directMatch != null) {
            return directMatch;
        }
        throw new IllegalArgumentException("More than 1 matching constructor "
                + "found in implementation class '" + clazz + "' for factory method '" + m + "'");
    }
    return constructors.getFirst();
}

From source file:Main.java

private static Method findGetter(Method[] methods, String name, Class<?> paramType) {
    String getterName = "get" + name;
    String isGetterName = "is" + name;
    for (Method method : methods) {
        if (Modifier.isStatic(method.getModifiers())) {
            continue;
        }// ww  w .  j av  a  2  s .  co m
        String methodName = method.getName();
        if (!methodName.equals(getterName) && !methodName.equals(isGetterName)) {
            continue;
        }
        if (!method.getReturnType().equals(paramType)) {
            continue;
        }
        if (method.getParameterTypes().length == 0) {
            return method;
        }
    }
    return null;
}

From source file:com.all.shared.util.JsonConverterPrimitiveCompliance.java

private static boolean isSetter(Method method) {
    if (!method.getName().startsWith("set")) {
        return false;
    }//from  w ww . j av  a 2  s.  com
    if (method.getParameterTypes().length != 1) {
        return false;
    }
    if (!void.class.equals(method.getReturnType())) {
        return false;
    }
    return true;
}

From source file:de.iteratec.iteraplan.businesslogic.exchange.xmi.importer.XmiHelper.java

/**
 * Invokes the setMethod for the given EStructuralFeature
 * // w  w w . j  av a  2  s . com
 * @param instance the IdEntity instance on which the method is invoked
 * @param value the given value which is set
 * @param esf the EStructuralFeature corresponding to the Method
 * @param sessionHelper the helper for creating the {@link Clob} if needed
 */
public static void invokeWriteMethod(IdEntity instance, Object value, EStructuralFeature esf,
        SessionHelper sessionHelper) {
    Method m = getWriteMethod(esf, instance.getClass());
    if (m == null || value == null) {
        return;
    }
    try {
        Object argument = value;
        if (m.getParameterTypes()[0].equals(Clob.class)) {
            argument = sessionHelper.createClob(value.toString());
        }

        m.invoke(instance, argument);
    } catch (IllegalArgumentException e) {
        LOGGER.error(e);
    } catch (IllegalAccessException e) {
        LOGGER.error(e);
    } catch (InvocationTargetException e) {
        LOGGER.error(e);
    }
}