Example usage for java.lang.reflect Method getGenericParameterTypes

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

Introduction

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

Prototype

@Override
public Type[] getGenericParameterTypes() 

Source Link

Usage

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

public static boolean isSetter(Method method) {
    Type returnType = method.getGenericReturnType();
    if (!returnType.equals(void.class)) {
        return false; //should not return anything
    }/*from  w  ww.  j  a  v a  2  s  .  c  o m*/
    Type[] argumentTypes = method.getGenericParameterTypes();
    if (argumentTypes == null || argumentTypes.length != 1) {
        return false; //should accept exactly one argument
    }
    String name = method.getName();
    if (name.startsWith("set")) {
        if (name.length() < 4) {
            return false; //setSomething
        }
        String fourthChar = name.substring(3, 4);
        return fourthChar.toUpperCase(Locale.ROOT).equals(fourthChar); //setSomething (upper case)
    }
    return false;
}

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

public static boolean isGetter(Method method) {
    Type returnType = method.getGenericReturnType();
    if (returnType.equals(void.class)) {
        return false; //should return something
    }//from  w w  w.  j  a v  a 2 s.c om
    Type[] argumentTypes = method.getGenericParameterTypes();
    if (argumentTypes != null && argumentTypes.length != 0) {
        return false; //should not accept any arguments
    }
    String name = method.getName();
    if (name.startsWith("get")) {
        if (name.length() < 4) {
            return false; //has to be getSomething
        }
        String fourthChar = name.substring(3, 4);
        return fourthChar.toUpperCase(Locale.ROOT).equals(fourthChar); //getSomething (upper case)
    } else if (name.startsWith("is")) {
        if (name.length() < 3) {
            return false; //isSomething
        }
        String thirdChar = name.substring(2, 3);
        //noinspection SimplifiableIfStatement
        if (!thirdChar.toUpperCase(Locale.ROOT).equals(thirdChar)) {
            return false; //has to start with uppercase (or something that uppercases to itself, like a number?)
        }
        return returnType.equals(boolean.class) || returnType.equals(Boolean.class);
    } else {
        return false;
    }
}

From source file:org.solmix.datax.support.InvokerObject.java

private static GenericParameterNode[] getGenericParameterTypes(Method method) {
    if (method == null)
        return new GenericParameterNode[0];
    Type[] gParams = method.getGenericParameterTypes();
    GenericParameterNode[] nodes = new GenericParameterNode[gParams.length];
    for (int i = 0; i < gParams.length; i++) {
        GenericParameterNode gpn;//from   w ww . j a  v a 2s.com
        if (gParams[i] instanceof ParameterizedType)
            gpn = buildGenericParameterTree((ParameterizedType) gParams[i]);
        else
            gpn = new GenericParameterNode((Class<?>) gParams[i]);
        nodes[i] = (gpn);
    }
    return nodes;
}

From source file:reflex.node.KernelExecutor.java

public static ReflexValue executeFunction(int lineNumber, Object outerApi, String areaName, String fnName,
        List<ReflexValue> params) {
    String apiName;/* w w  w .  j av  a2  s . com*/
    if (!StringUtils.isEmpty(areaName) && areaName.length() > 1) {
        apiName = areaName.substring(0, 1).toUpperCase() + areaName.substring(1);
    } else {
        apiName = "<api name missing>";
    }
    int numPassedParams = params.size();
    try {
        // Find the method get[AreaName], which will return the area
        Method[] methods = outerApi.getClass().getMethods();
        String getApiMethodName = "get" + apiName;

        for (Method m : methods) {
            if (m.getName().equals(getApiMethodName)) {
                // Call that method to get the api requested
                Object api = m.invoke(outerApi);
                // Now find the method with the fnName
                Method[] innerMethods = api.getClass().getMethods();
                for (Method im : innerMethods) {
                    if (im.getName().equals(fnName)) {
                        // the api should just have one entry
                        Type[] types = im.getGenericParameterTypes();
                        int numExpectedParams = types.length;
                        if (numExpectedParams == numPassedParams) {
                            List<Object> callParams = new ArrayList<Object>(types.length);
                            // Now coerce the types...
                            for (int i = 0; i < types.length; i++) {
                                ReflexValue v = params.get(i);
                                Object x = convertValueToType(v, types[i]);
                                callParams.add(x);
                            }

                            // Now invoke
                            Object ret;
                            try {
                                ret = im.invoke(api, callParams.toArray());
                            } catch (InvocationTargetException e) {
                                // TODO Auto-generated catch block
                                throw new ReflexException(lineNumber, String.format(
                                        "Error in Reflex script at line %d. Call to %s.%s failed: %s",
                                        lineNumber, apiName, fnName, e.getTargetException().getMessage()), e);
                            }
                            ReflexValue retVal = new ReflexNullValue(lineNumber);
                            if (ret != null) {
                                retVal = new ReflexValue(convertObject(ret));
                            }
                            return retVal;
                        }
                    }
                }
                throw new ReflexException(lineNumber, String.format(
                        "API call not found: %s.%s (taking %s parameters)", apiName, fnName, numPassedParams));
            }
        }
        throw new ReflexException(lineNumber, "API '" + apiName + "' not found!");
    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        if (cause != null) {
            if (cause instanceof OutOfMemoryError) {
                log.error(ExceptionToString.format(e));
                throw (OutOfMemoryError) cause;
            } else if (cause instanceof RaptureException) {
                log.warn(ExceptionToString.format(e));
                String message = ((RaptureException) cause).getFormattedMessage();
                throw new ReflexException(lineNumber, message, e);
            } else {
                String details = null;
                if (cause.getMessage() != null) {
                    details = ": " + cause.getMessage();
                }
                RaptureException re = RaptureExceptionFactory
                        .create(String.format("Error executing api call: %s.%s (takes %s parameters)%s",
                                apiName, fnName, numPassedParams, details), e);

                String message = re.getFormattedMessage();
                throw new ReflexException(lineNumber, message, re);
            }
        }
        throw new ReflexException(lineNumber, e.getTargetException().getMessage(), e);
    } catch (ReflexException e) {
        throw e;
    } catch (Exception e) {
        log.error(ExceptionToString.format(e));
        throw new ReflexException(lineNumber, e.getMessage(), e);
    }
}

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

public static Method findSetter(Class<?> clazz, String propName) {
    String expectedName = "set" + propName.substring(0, 1).toUpperCase(Locale.ROOT) + propName.substring(1);
    for (Method method : clazz.getMethods()) {
        if (!method.getName().equals(expectedName)) {
            continue;
        }/*from  ww  w  .j  a  va 2s. c  o  m*/
        if (!method.getReturnType().equals(void.class)) {
            continue;
        }
        Type[] argTypes = method.getGenericParameterTypes();
        if (argTypes == null || argTypes.length != 1) {
            continue;
        }
        return method;
    }
    return null;
}

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  ww w . j a v a2  s  .c o m*/
        sb.delete(sb.length() - 2, sb.length());
    }
    sb.append(")");
    return sb.toString();
}

From source file:com.github.dozermapper.core.util.ReflectionUtils.java

public static Class<?> determineGenericsType(Class<?> clazz, Method method, boolean isReadMethod) {
    Class<?> result = null;
    if (isReadMethod) {
        Type parameterType = method.getGenericReturnType();
        result = determineGenericsType(parameterType);
    } else {/*from ww w. j  ava2  s  . c  o  m*/
        Type[] parameterTypes = method.getGenericParameterTypes();
        if (parameterTypes != null) {
            result = determineGenericsType(parameterTypes[0]);
        }
    }

    if (result == null) {
        // Have a look at generic superclass
        Type genericSuperclass = clazz.getGenericSuperclass();
        if (genericSuperclass != null) {
            result = determineGenericsType(genericSuperclass);
        }
    }

    return result;
}

From source file:org.jtester.utility.ReflectionUtils.java

/**
 * Returns the setter methods in the given class that have an argument with
 * the exact given type. The class's superclasses are also investigated.
 * /*  w ww .  j a va2  s  . c  om*/
 * @param clazz
 *            The class to get the setter from, not null
 * @param type
 *            The type, not null
 * @param isStatic
 *            True if static setters are to be returned, false for
 *            non-static
 * @return All setters for an object of the given type
 */
public static Set<Method> getSettersOfType(Class<?> clazz, Type type, boolean isStatic) {
    Set<Method> settersOfType = new HashSet<Method>();
    Set<Method> allMethods = getAllMethods(clazz);
    for (Method method : allMethods) {
        if (isSetter(method) && method.getGenericParameterTypes()[0].equals(type)
                && isStatic == isStatic(method.getModifiers())) {
            settersOfType.add(method);
        }
    }
    return settersOfType;
}

From source file:org.openflexo.antar.binding.TypeUtils.java

private static boolean checkSuperType(Method m) {
    Type t1 = m.getGenericParameterTypes()[0];
    Type t2 = m.getGenericParameterTypes()[1];
    System.out.println("checkSuperType " + (getSuperType(t1).equals(t2) ? "OK  " : "NOK ") + "Method "
            + m.getName() + " type: " + simpleRepresentation(t1) + " super type: " + simpleRepresentation(t2));
    return true;/*from  w  w  w  .ja v  a  2s .  c  o  m*/
}

From source file:org.openflexo.antar.binding.TypeUtils.java

private static boolean checkFail(Method m) {
    Type t1 = m.getGenericParameterTypes()[0];
    Type t2 = m.getGenericParameterTypes()[1];
    System.out.println("checkFail " + (isTypeAssignableFrom(t1, t2, true) ? "NOK " : "OK  ") + "Method "
            + m.getName() + " t1: " + t1 + " of " + t1.getClass().getSimpleName() + " t2: " + t2 + " of "
            + t2.getClass().getSimpleName());
    return isTypeAssignableFrom(t1, t2, true);
}