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:edu.umich.flowfence.common.QMDescriptor.java

public static QMDescriptor forMethod(Context context, Method method) {
    Class<?> definingClass = method.getDeclaringClass();
    String methodName = method.getName();
    Class<?>[] paramTypes = method.getParameterTypes();
    if ((method.getModifiers() & Modifier.STATIC) != 0) {
        return forStatic(context, definingClass, methodName, paramTypes);
    } else {//from  w  w  w.jav  a2 s  . co  m
        return forInstance(context, definingClass, methodName, paramTypes);
    }
}

From source file:edu.umich.oasis.common.SodaDescriptor.java

public static SodaDescriptor forMethod(Context context, Method method) {
    Class<?> definingClass = method.getDeclaringClass();
    String methodName = method.getName();
    Class<?>[] paramTypes = method.getParameterTypes();
    if ((method.getModifiers() & Modifier.STATIC) != 0) {
        return forStatic(context, definingClass, methodName, paramTypes);
    } else {//w w w  .  j  a va2s.  c  o  m
        return forInstance(context, definingClass, methodName, paramTypes);
    }
}

From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java

public static Map<String, Map<Class<?>, Method>> findUnaryMethods(Class<?> clazz) {
    Map<String, Map<Class<?>, Method>> result = new TreeMap<>();

    Method[] methods = clazz.getMethods();

    for (Method method : methods) {
        int modified = method.getModifiers();
        if (Modifier.isPublic(modified) && method.getParameterTypes().length == 1) {
            Map<Class<?>, Method> methodMap = result.get(method.getName());
            if (methodMap == null) {
                methodMap = new HashMap<>();
                result.put(method.getName(), methodMap);
            }/*w w  w  .j a v a 2  s . c o  m*/
            methodMap.put(method.getParameterTypes()[0], method);
        }
    }

    return result;
}

From source file:it.tidalwave.northernwind.aspect.DebugProfilingAspect.java

@Nonnull
private static <T extends Annotation> T getAnnotation(final @Nonnull ProceedingJoinPoint pjp,
        final @Nonnull Class<T> annotationClass) throws NoSuchMethodException {
    final MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
    Method method = methodSignature.getMethod();

    if (method.getDeclaringClass().isInterface()) // FIXME && annotation inheritance -- FIXME also ancestor class
    {/*from w w w  . j  a  v  a 2  s  . c o  m*/
        final String methodName = pjp.getSignature().getName();
        method = pjp.getTarget().getClass().getDeclaredMethod(methodName, method.getParameterTypes());
    }

    return method.getAnnotation(annotationClass);
}

From source file:com.enonic.cms.business.portal.datasource.methodcall.MethodCallFactory.java

public static MethodCall create(final DatasourceExecutorContext context, final Datasource datasource) {
    String methodName = datasource.getMethodName();
    if (methodName == null) {
        return null;
    }/*  w w w . j  ava2 s  .c  o m*/

    String pluginName = resolvePluginName(methodName);

    FunctionLibrary pluginObject = pluginName != null ? getPluginObject(pluginName) : null;

    Object targetObject = pluginObject != null ? pluginObject.getTarget() : context.getDataSourceService();
    Class targetClass = targetObject.getClass();
    boolean useContext = pluginObject == null;

    List parameterEl = datasource.getParameterElements();
    int paramCount = parameterEl.size() + (useContext ? 1 : 0);

    Method method = resolveMethod(targetClass, methodName, paramCount, useContext);
    Class[] paramTypes = method.getParameterTypes();
    MethodCallParameter[] parameters = new MethodCallParameter[paramCount];

    if (useContext) {
        DataSourceContext dataSourceContext = createDataSourceContext(context);
        parameters[0] = new MethodCallParameter("__context__", dataSourceContext, "false",
                DataSourceContext.class);
    }

    int paramOffset = useContext ? 1 : 0;
    for (int i = 0; i < parameterEl.size(); i++) {

        Element paramEl = (Element) parameterEl.get(i);
        String paramName = paramEl.getAttributeValue("name");
        try {
            int paramterIndex = i + paramOffset;

            parameters[paramterIndex] = createParameter(paramEl, paramTypes[paramterIndex], context);
        } catch (Exception e) {
            StringBuffer msg = new StringBuffer();
            msg.append("Method [").append(methodName).append("]");
            msg.append(" has correct number of parameters [").append(paramCount).append("]");
            msg.append(", but parameter number ").append(i + 1).append(" with name [").append(paramName)
                    .append("]");
            msg.append(" is possibly wrong. Please check documentation.");
            throw new IllegalArgumentException(msg.toString(), e);
        }
    }

    boolean isCacheable = datasource.isCacheable();

    return new MethodCall(context.getInvocationCache(), targetObject, parameters, method, isCacheable);
}

From source file:com.netspective.sparx.util.HttpUtils.java

public static void assignParamToInstance(HttpServletRequest req, XmlDataModelSchema schema, Object instance,
        String paramName, String defaultValue)
        throws IllegalAccessException, InvocationTargetException, DataModelException {
    boolean required = false;
    if (paramName.endsWith("!")) {
        required = true;//w w w. j a v  a 2s .  c o  m
        paramName = paramName.substring(0, paramName.length() - 1);
    }

    Method method = (Method) schema.getAttributeSetterMethods().get(paramName);
    if (method != null) {
        Class[] args = method.getParameterTypes();
        if (args.length == 1) {
            Class arg = args[0];
            if (java.lang.String.class.equals(arg) && arg.isArray()) {
                String[] paramValues = req.getParameterValues(paramName);
                if ((paramValues == null || paramValues.length == 0) && required)
                    throw new ServletParameterRequiredException(
                            "Servlet parameter list '" + paramName + "' is required but not available.");
                method.invoke(instance, new Object[] { paramValues });
            } else {
                XmlDataModelSchema.AttributeSetter as = (XmlDataModelSchema.AttributeSetter) schema
                        .getAttributeSetters().get(paramName);
                String paramValue = req.getParameter(paramName);
                if (paramValue == null) {
                    if (required)
                        throw new ServletParameterRequiredException(
                                "Servlet parameter '" + paramName + "' is required but not available.");

                    paramValue = defaultValue;
                }
                as.set(null, instance, paramValue);
            }
        } else if (log.isDebugEnabled())
            log.debug("Attempting to assign '" + paramName + "' to a method in '" + instance.getClass()
                    + "' but the method has more than one argument.");
    } else if (log.isDebugEnabled())
        log.debug("Attempting to assign '" + paramName + "' to a method in '" + instance.getClass()
                + "' but there is no mutator available.");
}

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

/**
 * <p>//from  w w w.  ja v  a  2 s . c om
 * Use as TestNG dataProvider.
 * <p>
 * data-provider name: <code>isfw-database</code>
 * <p>
 * Required properties:
 * <ol>
 * <li>test.&lt;method name&gt;.query=&lt;sql query string&gt;
 * </ol>
 * 
 * @param m
 * @return
 */
@DataProvider(name = "isfw_database")
public static Object[][] getDataFromDB(Method m) {
    String query = getParameters(m).get(params.SQLQUERY.name());
    Class<?> types[] = m.getParameterTypes();

    if ((types.length == 1) && Map.class.isAssignableFrom(types[0])) {
        // will consider first row as header row
        return DatabaseUtil.getRecordDataAsMap(query);
    }
    return DatabaseUtil.getData(query);
}

From source file:edu.umn.msi.tropix.proteomics.parameters.ParameterUtils.java

/**
 * Uses reflection to dynamically set a parameter bean's parameters from a given map of Strings.
 * //from  w w w .  ja  v  a  2 s.  c  o  m
 * @param parameterMap
 *          Mapping of parameter names (attributes) to values.
 * @param parameterObject
 *          Java bean to set parameter values of of. Setter methods must conform to the Java bean naming scheme and must take in one argument of
 *          type String, Integer, Long, Float, or Double.
 * @exception IllegalArgumentException
 *              Thrown if a given setter method cannot be found or if the setter method does not conform to the rules described.
 */
@SuppressWarnings("unchecked")
public static void setParametersFromMap(final Map parameterMap, final Object parameterObject) {
    final Method[] methods = parameterObject.getClass().getMethods();
    final Map<String, Method> objectSetMethodMap = new HashMap<String, Method>(methods.length);

    for (final Method method : parameterObject.getClass().getMethods()) {
        final String methodName = method.getName();
        if (methodName.contains("set")) {
            objectSetMethodMap.put(method.getName(), method);
        }
    }

    for (final Object keyObject : parameterMap.keySet()) {
        final String key = (String) keyObject;
        final String value = (String) parameterMap.get(key);
        final String setterMethodName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1);
        final Method setterMethod = objectSetMethodMap.get(setterMethodName);
        if (setterMethod == null) {
            continue;
        }
        final Class<?>[] parameterTypes = setterMethod.getParameterTypes();
        if (parameterTypes.length != 1) {
            throw new IllegalArgumentException(
                    "Illegal setter method found, must take in exactly one argument.");
        }
        final Class<?> argumentType = parameterTypes[0];
        try {
            if (argumentType.equals(String.class)) {
                setterMethod.invoke(parameterObject, value);
                continue;
            }
            if (value == null || value.trim().equals("")) {
                setterMethod.invoke(parameterObject, (Object) null);
                continue;
            }
            if (argumentType.equals(Double.class) || argumentType.equals(double.class)) {
                setterMethod.invoke(parameterObject, Double.parseDouble(value));
            } else if (argumentType.equals(Integer.class) || argumentType.equals(int.class)) {
                setterMethod.invoke(parameterObject, Integer.parseInt(value));
            } else if (argumentType.equals(Long.class) || argumentType.equals(long.class)) {
                setterMethod.invoke(parameterObject, Long.parseLong(value));
            } else if (argumentType.equals(Float.class) || argumentType.equals(float.class)) {
                setterMethod.invoke(parameterObject, Float.parseFloat(value));
            } else if (argumentType.equals(Boolean.class) || argumentType.equals(boolean.class)) {
                setterMethod.invoke(parameterObject, Boolean.parseBoolean(value));
            } else if (argumentType.equals(Short.class) || argumentType.equals(short.class)) {
                setterMethod.invoke(parameterObject, Short.parseShort(value));
            } else {
                throw new IllegalArgumentException("Illegal type found for argument to setter bean - type is "
                        + argumentType + " - key is " + key);
            }
        } catch (final Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
}

From source file:com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBReflector.java

/**
 * Returns whether the method given is a getter method we should serialize /
 * deserialize to the service. The method must begin with "get" or "is",
 * have no arguments, belong to a class that declares its table, and not be
 * marked ignored./*from  www. j a  v  a 2 s  .  c  o  m*/
 */
private static boolean isRelevantGetter(Method m) {
    return (m.getName().startsWith("get") || m.getName().startsWith("is")) && m.getParameterTypes().length == 0
            && isDocumentType(m.getDeclaringClass())
            && !ReflectionUtils.getterOrFieldHasAnnotation(m, DynamoDBIgnore.class);
}

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

/**
 * Returns whether the given {@link Method} has a parameter of the given
 * type.//from w ww .j a  v a  2  s .co m
 * 
 * @param method
 * @param type
 * @return
 */
public static boolean hasParameterOfType(Method method, Class<?> type) {

    return Arrays.asList(method.getParameterTypes()).contains(type);
}