Example usage for java.lang.reflect Parameter getParameterizedType

List of usage examples for java.lang.reflect Parameter getParameterizedType

Introduction

In this page you can find the example usage for java.lang.reflect Parameter getParameterizedType.

Prototype

public Type getParameterizedType() 

Source Link

Document

Returns a Type object that identifies the parameterized type for the parameter represented by this Parameter object.

Usage

From source file:ch.ifocusit.plantuml.utils.ClassUtils.java

public static Set<Class> getGenericTypes(Parameter parameter) {
    if (parameter.getParameterizedType() instanceof ParameterizedType) {
        // manage generics
        return getGenericTypes((ParameterizedType) parameter.getParameterizedType());
    }/*ww w.java2s . c  om*/
    return new HashSet<>();
}

From source file:com.fitbur.jestify.junit.spring.injector.IndexMockInjector.java

@Override
public void inject() {
    Map<DescriptorKey, ParameterDescriptor> parameterDescriptors = context.getParamaterDescriptors();
    Field field = fieldDescriptor.getField();

    Mock mock = fieldDescriptor.getMock().get();
    Integer index = mock.index();
    Optional<ParameterDescriptor> optional = parameterDescriptors.values().parallelStream()
            .filter(p -> index.equals(p.getIndex())).findFirst();

    ParameterDescriptor parameterDescriptor = optional.get();
    Parameter parameter = parameterDescriptor.getParameter();

    Type fieldType = field.getGenericType();
    Type parameterType = parameter.getParameterizedType();

    checkArgument(fieldType.equals(parameterType),
            "Can not mock field '%s'. Test class field type '%s' and class under test "
                    + "constructor parameter type '%s' at '%d' index do not match.",
            field.getName(), field.getGenericType(), parameterType, index);

    ResolvableType resolver = ResolvableType.forType(parameterType);

    Class rawType;//  w w w . ja  va  2s  .com

    if (resolver.hasGenerics()) {
        if (resolver.isAssignableFrom(Provider.class) || resolver.isAssignableFrom(Optional.class)) {
            rawType = resolver.getRawClass();
        } else {
            rawType = resolver.resolve();
        }
    } else {
        rawType = (Class) parameterType;
    }

    checkArgument(arguments[index] == null,
            "Can not mock field '%s'. Multipe test class fields have the same index of '%d'", field.getName(),
            index);

    Object instance = testReifier.reifyField(fieldDescriptor, parameterDescriptor);
    arguments[index] = instance;
}

From source file:com.fitbur.jestify.junit.spring.injector.NameMockInjector.java

@Override
public void inject() {
    Map<DescriptorKey, ParameterDescriptor> parameterDescriptors = context.getParamaterDescriptors();
    Field field = fieldDescriptor.getField();
    Type fieldType = field.getGenericType();

    Mock mock = fieldDescriptor.getMock().get();
    String mockName = mock.name();
    DescriptorKey descriptorKey = new DescriptorKey(fieldType, mockName);

    ParameterDescriptor parameterDescriptor = parameterDescriptors.get(descriptorKey);

    checkArgument(parameterDescriptor != null,
            "Can not mock field '%s'. Could not find class under test constructor "
                    + "argument with the name '%s'.",
            field.getName(), mockName);/*from w  w  w  . j a v a 2  s  .  co m*/

    Parameter parameter = parameterDescriptor.getParameter();
    Integer index = parameterDescriptor.getIndex();
    Type parameterType = parameter.getParameterizedType();

    checkArgument(fieldType.equals(parameterType),
            "Can not mock field '%s'. Test class field type '%s' and class under test "
                    + "constructor parameter type '%s' with name '%s' do not match.",
            field.getName(), field.getGenericType(), parameterType, mockName);

    ResolvableType resolver = ResolvableType.forType(parameterType);

    Class rawType;

    if (resolver.hasGenerics()) {
        if (resolver.isAssignableFrom(Provider.class) || resolver.isAssignableFrom(Optional.class)) {
            rawType = resolver.getRawClass();
        } else {
            rawType = resolver.resolve();
        }
    } else {
        rawType = (Class) parameterType;
    }

    checkArgument(arguments[index] == null,
            "Can not mock field '%s'. Multipe test class fields have the same index of '%d'", field.getName(),
            index);

    Object instance = testReifier.reifyField(fieldDescriptor, parameterDescriptor);

    arguments[index] = instance;
}

From source file:com.fitbur.jestify.junit.spring.injector.TypeMockInjector.java

@Override
public void inject() {
    Map<DescriptorKey, ParameterDescriptor> parameterDescriptors = context.getParamaterDescriptors();
    Field field = fieldDescriptor.getField();

    Mock mock = fieldDescriptor.getMock().get();
    Type fieldType = field.getGenericType();
    String fieldName = field.getName();
    DescriptorKey descriptorKey = new DescriptorKey(fieldType, fieldName);

    //if there is a parameter descriptor that matches the field then lets use that
    if (parameterDescriptors.containsKey(descriptorKey)) {
        ParameterDescriptor descriptor = parameterDescriptors.get(descriptorKey);
        Parameter parameter = descriptor.getParameter();
        Integer index = descriptor.getIndex();
        ResolvableType resolver = ResolvableType.forType(fieldType);

        Class rawType;//w ww.  ja  v a  2 s.c om

        if (resolver.hasGenerics()) {
            if (resolver.isAssignableFrom(Provider.class) || resolver.isAssignableFrom(Optional.class)) {
                rawType = resolver.getRawClass();
            } else {
                rawType = resolver.resolve();
            }
        } else {
            rawType = (Class) fieldType;
        }
        Object instance = testReifier.reifyField(fieldDescriptor, descriptor);
        arguments[index] = instance;
    } else {
        //otherwise find the right parameter based on the type of the field
        Collection<ParameterDescriptor> descriptors = parameterDescriptors.values();
        for (ParameterDescriptor descriptor : descriptors) {
            Parameter parameter = descriptor.getParameter();
            Type parameterType = parameter.getParameterizedType();
            Integer index = descriptor.getIndex();

            ResolvableType resolver = ResolvableType.forType(parameterType);

            Class rawType;

            if (resolver.hasGenerics()) {
                if (resolver.isAssignableFrom(Provider.class) || resolver.isAssignableFrom(Optional.class)) {
                    rawType = resolver.getRawClass();
                } else {
                    rawType = resolver.resolve();
                }
            } else {
                rawType = resolver.resolve();
            }

            if (arguments[index] != null) {
                continue;
            }

            if (parameterType.equals(fieldType)) {
                Object instance = testReifier.reifyField(fieldDescriptor, descriptor);
                arguments[index] = instance;
                break;
            }

        }
    }

}

From source file:com.phoenixnap.oss.ramlapisync.data.ApiParameterMetadata.java

/**
 * Default constructor that creates a metadata object from a Java parameter
 * //from   w w  w  .  j  a  v a2s . c  o m
 * @param param Java Parameter representation
 */
public ApiParameterMetadata(Parameter param) {
    super();
    this.resourceId = false;
    this.nullable = false;

    String annotatedName = null;
    if (param == null) {
        throw new NullArgumentException("param");
    }

    RequestParam requestParam = param.getAnnotation(RequestParam.class);
    if (requestParam != null) {
        annotatedName = requestParam.value();
        nullable = !requestParam.required();
    }

    PathVariable pathVariable = param.getAnnotation(PathVariable.class);
    if (pathVariable != null) {
        resourceId = true;
        annotatedName = pathVariable.value();
    }

    RequestBody requestBody = param.getAnnotation(RequestBody.class);
    if (requestBody != null) {
        nullable = !requestBody.required();

    }

    this.name = resolveParameterName(annotatedName, param);
    this.param = param;
    if (param != null) {
        this.type = param.getType();
        this.genericType = (Class<?>) TypeHelper.inferGenericType(param.getParameterizedType());
    }

    Example parameterExample = param.getAnnotation(Example.class);
    if (parameterExample != null && StringUtils.hasText(parameterExample.value())) {
        this.example = parameterExample.value();
    }
}

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);//from w  w  w .j av a 2 s. co m

            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;
}