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:org.openmrs.module.webservices.rest.web.ConversionUtilTest.java

/**
 * @verifies return the actual type if defined on the parent class
 * @see ConversionUtil#getTypeVariableClass(Class, java.lang.reflect.TypeVariable)
 *//*from   ww  w. j av  a  2s .  co m*/
@Test
public void getTypeVariableClass_shouldReturnTheActualTypeIfDefinedOnTheParentClass() throws Exception {
    ChildGenericType_Int i = new ChildGenericType_Int();
    ChildGenericType_String s = new ChildGenericType_String();
    ChildGenericType_Temp t = new ChildGenericType_Temp();

    Method setter = PropertyUtils.getPropertyDescriptor(i, "value").getWriteMethod();
    Type type = ConversionUtil.getTypeVariableClass(ChildGenericType_Int.class,
            (TypeVariable<?>) setter.getGenericParameterTypes()[0]);

    Assert.assertNotNull(type);
    Assert.assertEquals(Integer.class, type);

    setter = PropertyUtils.getPropertyDescriptor(s, "value").getWriteMethod();
    type = ConversionUtil.getTypeVariableClass(ChildGenericType_String.class,
            (TypeVariable<?>) setter.getGenericParameterTypes()[0]);

    Assert.assertNotNull(type);
    Assert.assertEquals(String.class, type);

    setter = PropertyUtils.getPropertyDescriptor(t, "value").getWriteMethod();
    type = ConversionUtil.getTypeVariableClass(ChildGenericType_Temp.class,
            (TypeVariable<?>) setter.getGenericParameterTypes()[0]);

    Assert.assertNotNull(type);
    Assert.assertEquals(Temp.class, type);
}

From source file:org.openmrs.module.webservices.rest.web.ConversionUtilTest.java

/**
 * @verifies return the correct actual type if there are multiple generic types
 * @see ConversionUtil#getTypeVariableClass(Class, java.lang.reflect.TypeVariable)
 *///  w  w  w.  j a v  a2  s . c  o  m
@Test
public void getTypeVariableClass_shouldReturnTheCorrectActualTypeIfThereAreMultipleGenericTypes()
        throws Exception {
    ChildMultiGenericType i = new ChildMultiGenericType();

    Method setter = PropertyUtils.getPropertyDescriptor(i, "first").getWriteMethod();
    Type type = ConversionUtil.getTypeVariableClass(ChildMultiGenericType.class,
            (TypeVariable<?>) setter.getGenericParameterTypes()[0]);

    Assert.assertNotNull(type);
    Assert.assertEquals(Integer.class, type);

    setter = PropertyUtils.getPropertyDescriptor(i, "second").getWriteMethod();
    type = ConversionUtil.getTypeVariableClass(ChildMultiGenericType.class,
            (TypeVariable<?>) setter.getGenericParameterTypes()[0]);

    Assert.assertNotNull(type);
    Assert.assertEquals(String.class, type);

    setter = PropertyUtils.getPropertyDescriptor(i, "third").getWriteMethod();
    type = ConversionUtil.getTypeVariableClass(ChildMultiGenericType.class,
            (TypeVariable<?>) setter.getGenericParameterTypes()[0]);

    Assert.assertNotNull(type);
    Assert.assertEquals(Temp.class, type);
}

From source file:springfox.documentation.spring.web.readers.operation.HandlerMethodResolver.java

private Predicate<ResolvedMethod> onlyCovariantMethods(final Method methodToResolve) {
    return new Predicate<ResolvedMethod>() {
        @Override//w  ww  .ja  v a 2s.co  m
        public boolean apply(ResolvedMethod input) {
            for (int index = 0; index < input.getArgumentCount(); index++) {
                if (!covariant(input.getArgumentType(index),
                        methodToResolve.getGenericParameterTypes()[index])) {
                    return false;
                }
            }
            ResolvedType candidateMethodReturnValue = returnTypeOrVoid(input);
            return bothAreVoids(candidateMethodReturnValue, methodToResolve.getGenericReturnType())
                    || contravariant(candidateMethodReturnValue, methodToResolve.getGenericReturnType());
        }
    };
}

From source file:org.structr.core.entity.SchemaMethod.java

private boolean getSignature(final Class type, final String methodName, final ActionEntry entry) {

    // superclass is AbstractNode
    for (final Method method : type.getMethods()) {

        if (methodName.equals(method.getName()) && (method.getModifiers() & Modifier.STATIC) == 0) {

            final Type[] parameterTypes = method.getGenericParameterTypes();
            final Type returnType = method.getGenericReturnType();
            final List<Type> types = new LinkedList<>();

            // compile list of types to check for generic type parameter
            types.addAll(Arrays.asList(parameterTypes));
            types.add(returnType);// w w w . j a  v a 2s  .com

            final String genericTypeParameter = getGenericMethodParameter(types, method);

            // check for generic return type, and if the method defines its own generic type
            if (returnType instanceof TypeVariable
                    && ((TypeVariable) returnType).getGenericDeclaration().equals(method)) {

                // method defines its own generic type
                entry.setReturnType(genericTypeParameter + returnType.getTypeName());

            } else {

                // non-generic return type
                final Class returnClass = method.getReturnType();
                if (returnClass.isArray()) {

                    entry.setReturnType(genericTypeParameter + returnClass.getComponentType().getName() + "[]");

                } else {

                    entry.setReturnType(genericTypeParameter + method.getReturnType().getName());
                }
            }

            for (final Parameter parameter : method.getParameters()) {

                String typeName = parameter.getParameterizedType().getTypeName();
                String name = parameter.getType().getSimpleName();

                if (typeName.contains("$")) {
                    typeName = typeName.replace("$", ".");
                }

                entry.addParameter(typeName, parameter.getName());
            }

            for (final Class exception : method.getExceptionTypes()) {
                entry.addException(exception.getName());
            }

            entry.setOverrides(getProperty(overridesExisting));
            entry.setCallSuper(getProperty(callSuper));

            // success
            return true;
        }
    }

    return false;
}

From source file:org.sybila.parasim.core.impl.ProvidingMethod.java

public ProvidingMethod(Method method, Object target) {
    Validate.notNull(method);/*from w ww . j  a va 2 s. c o  m*/
    Validate.notNull(target);
    this.provide = method.getAnnotation(Provide.class);
    Validate.notNull(provide,
            "Field without " + Provide.class.getName() + " annotation can't be providing point.");
    this.method = method;
    this.target = target;
    this.qualifier = ReflectionUtils.loadQualifier(method.getAnnotations());
    injectionPoints = new InjectionPoint[method.getGenericParameterTypes().length];
    for (int i = 0; i < method.getGenericParameterTypes().length; i++) {
        injectionPoints[i] = new GenericInjectionPoint(method.getParameterAnnotations()[i],
                method.getGenericParameterTypes()[i]);
    }
}

From source file:org.soybeanMilk.core.bean.PropertyInfo.java

protected PropertyInfo(Class<?> ownerClass, Class<?> propType, String propName, Method readMethod,
        Method writeMethod) {
    super();/*from w  w  w  .j a  va 2s. c om*/
    this.ownerClass = ownerClass;
    this.propType = propType;
    this.propName = propName;
    this.readMethod = readMethod;
    this.writeMethod = writeMethod;

    if (writeMethod != null)
        this.propGenericType = writeMethod.getGenericParameterTypes()[0];
    else
        this.propGenericType = propType;
}

From source file:cn.afterturn.easypoi.excel.imports.CellValueService.java

/**
 * ?cell//from  www . ja va2 s.c  om
 *
 * @param dataHandler
 * @param object
 * @param cellEntity
 * @param excelParams
 * @param titleString
 * @return
 */
public Object getValue(IExcelDataHandler<?> dataHandler, Object object, SaxReadCellEntity cellEntity,
        Map<String, ExcelImportEntity> excelParams, String titleString) {
    ExcelImportEntity entity = excelParams.get(titleString);
    Method setMethod = entity.getMethods() != null && entity.getMethods().size() > 0
            ? entity.getMethods().get(entity.getMethods().size() - 1)
            : entity.getMethod();
    Type[] ts = setMethod.getGenericParameterTypes();
    String classFullName = ts[0].toString();
    Object result = cellEntity.getValue();
    result = handlerSuffix(entity.getSuffix(), result);
    result = replaceValue(entity.getReplace(), result);
    result = handlerValue(dataHandler, object, result, titleString);
    return getValueByType(classFullName, result, entity, (Class) ts[0]);
}

From source file:org.cruxframework.crux.core.server.rest.core.dispatch.MethodInvoker.java

public MethodInvoker(Class<?> root, Method method, String httpMethod) {
    this.method = method;
    this.rootClass = root;
    this.restErrorHandler = RestErrorHandlerFactory.createErrorHandler(method);
    this.params = new ValueInjector[method.getParameterTypes().length];
    Type[] genericParameterTypes = method.getGenericParameterTypes();
    for (int i = 0; i < genericParameterTypes.length; i++) {
        Annotation[] annotations = method.getParameterAnnotations()[i];
        Type paramType = ClassUtils.resolveGenericTypeOnMethod(genericParameterTypes[i], rootClass, method);
        params[i] = createParameterExtractor(root, paramType, annotations);
    }/*from  www . ja  va  2s.  com*/
    validateParamExtractors(httpMethod);
    initializePreprocessors();
    initializePostprocessors();
}

From source file:cn.afterturn.easypoi.excel.imports.CellValueService.java

/**
 * ?cell//from   w w w. ja  v  a  2s.  c  o m
 *
 * @param object
 * @param cell
 * @param excelParams
 * @param titleString
 * @param dictHandler
 */
public Object getValue(IExcelDataHandler<?> dataHandler, Object object, Object cell,
        Map<String, ExcelImportEntity> excelParams, String titleString, IExcelDictHandler dictHandler)
        throws Exception {

    ExcelImportEntity entity = excelParams.get(titleString);
    String classFullName = "class java.lang.Object";
    Class clazz = null;
    if (!(object instanceof Map)) {
        Method setMethod = entity.getMethods() != null && entity.getMethods().size() > 0
                ? entity.getMethods().get(entity.getMethods().size() - 1)
                : entity.getMethod();
        Type[] ts = setMethod.getGenericParameterTypes();
        classFullName = ts[0].toString();
        clazz = (Class) ts[0];
    }
    Object result = null;
    if (cell instanceof Cell) {
        result = getCellValue(classFullName, (Cell) cell, entity);
    } else {
        result = cell;
    }
    if (entity != null) {
        result = handlerSuffix(entity.getSuffix(), result);
        result = replaceValue(entity.getReplace(), result);
        result = replaceValue(entity.getReplace(), result);
        if (dictHandler != null && StringUtils.isNoneBlank(entity.getDict())) {
            result = dictHandler.toValue(entity.getDict(), object, entity.getName(), result);
        }
    }
    result = handlerValue(dataHandler, object, result, titleString);
    return getValueByType(classFullName, result, entity, clazz);
}

From source file:com.grepcurl.random.ObjectGenerator.java

protected <T> boolean _processNormal(Method method, SetterOverrides setterOverrides, T t,
        Deque<Object> objectStack) throws InvocationTargetException, IllegalAccessException {
    if (method.getName().startsWith("set") && method.getName().length() > 3) {
        Type[] types = method.getGenericParameterTypes();

        Object[] params = new Object[types.length];
        for (int i = 0; i < types.length; i++) {
            params[i] = _process(types[i], setterOverrides, objectStack);
        }/*from ww  w  .  j a va2  s  .  co  m*/
        method.invoke(t, params);
        return true;
    } else {
        return false;
    }
}