Java Code Examples for com.googlecode.gentyref.GenericTypeReflector#getTypeParameter()

The following examples show how to use com.googlecode.gentyref.GenericTypeReflector#getTypeParameter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FieldUtils.java    From zstack with Apache License 2.0 6 votes vote down vote up
private static GenericType inferCollection(Type type) {
    CollectionGenericType ret = new CollectionGenericType();
    Type valueType = GenericTypeReflector.getTypeParameter(type, Collection.class.getTypeParameters()[0]);
    ret.isInferred = valueType != null;
    if (valueType == null) {
        return ret;
    }

    ret.valueType = GenericTypeReflector.erase(valueType);
    if (Map.class.isAssignableFrom(ret.valueType)) {
        ret.nestedGenericValue = inferMap(valueType);
    } else if (Collection.class.isAssignableFrom(ret.valueType)) {
        ret.nestedGenericValue = inferCollection(valueType);
    }
    return ret;
}
 
Example 2
Source File: FieldUtils.java    From zstack with Apache License 2.0 5 votes vote down vote up
private static GenericType inferMap(Type type) {
    MapGenericType ret = new MapGenericType();
    Type keyType = GenericTypeReflector.getTypeParameter(type, Map.class.getTypeParameters()[0]);
    Type valueType = GenericTypeReflector.getTypeParameter(type, Map.class.getTypeParameters()[1]);
    ret.isInferred = keyType != null && valueType != null;
    if (keyType == null || valueType == null) {
        return ret;
    }

    ret.keyType = GenericTypeReflector.erase(keyType);
    ret.valueType = GenericTypeReflector.erase(valueType);
    if (!ret.isInferred) {
        return ret;
    }

    if (Map.class.isAssignableFrom(ret.keyType)) {
        ret.nestedGenericKey = inferMap(keyType);
    } else if (Collection.class.isAssignableFrom(ret.keyType)) {
        ret.nestedGenericKey = inferCollection(keyType);
    }

    if (Map.class.isAssignableFrom(ret.valueType)) {
        ret.nestedGenericValue = inferMap(valueType);
    } else if (Collection.class.isAssignableFrom(ret.valueType)) {
        ret.nestedGenericValue = inferCollection(valueType);
    }

    return ret;
}
 
Example 3
Source File: ModelEncoder.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Get the decoded type of this encoder.
 *
 * @return the application type
 */
@SuppressWarnings("unchecked")
default Class<D> getDecodedType() {
    Type type = GenericTypeReflector.getTypeParameter(this.getClass(),
            ModelEncoder.class.getTypeParameters()[0]);
    if (type instanceof Class<?>) {
        return (Class<D>) GenericTypeReflector.erase(type);
    }
    throw new InvalidTemplateModelException(String.format(
            "Could not detect the model type of %s '%s'. "
                    + "The method 'getDecodedType' needs to be overridden manually.",
            ModelEncoder.class.getSimpleName(), this.getClass().getName()));
}
 
Example 4
Source File: ModelEncoder.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Get the encoded type of this encoder.
 *
 * @return the model type
 */
@SuppressWarnings("unchecked")
default Class<E> getEncodedType() {
    Type type = GenericTypeReflector.getTypeParameter(this.getClass(),
            ModelEncoder.class.getTypeParameters()[1]);
    if (type instanceof Class<?>) {
        return (Class<E>) GenericTypeReflector.erase(type);
    }
    throw new InvalidTemplateModelException(String.format(
            "Could not detect the presentation type of %s '%s'. "
                    + "The method 'getEncodedType' needs to be overridden manually.",
            ModelEncoder.class.getSimpleName(), this.getClass().getName()));
}
 
Example 5
Source File: AbstractServerHandlers.java    From flow with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private final Class<T> getType() {
    Type type = GenericTypeReflector.getTypeParameter(
            getClass().getGenericSuperclass(),
            getClass().getSuperclass().getTypeParameters()[0]);
    if (type instanceof Class || type instanceof ParameterizedType) {
        return (Class<T>) GenericTypeReflector.erase(type);
    }
    throw new IllegalStateException(getExceptionMessage(type));
}
 
Example 6
Source File: ReflectTools.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the Class type for the generic interface class extended by given
 * class if exists.
 *
 * @param clazz
 *            class that should extend interface
 * @param interfaceType
 *            class type of interface to get generic for
 * @return Class if found else {@code null}
 */
public static Class<?> getGenericInterfaceType(Class<?> clazz,
        Class<?> interfaceType) {
    Type type = GenericTypeReflector.getTypeParameter(clazz,
            interfaceType.getTypeParameters()[0]);

    if (type instanceof Class || type instanceof ParameterizedType) {
        return GenericTypeReflector.erase(type);
    }
    return null;
}
 
Example 7
Source File: Composite.java    From flow with Apache License 2.0 5 votes vote down vote up
private static Class<? extends Component> findContentType(
        Class<? extends Composite<?>> compositeClass) {
    Type type = GenericTypeReflector.getTypeParameter(
            compositeClass.getGenericSuperclass(),
            Composite.class.getTypeParameters()[0]);
    if (type instanceof Class || type instanceof ParameterizedType) {
        return GenericTypeReflector.erase(type).asSubclass(Component.class);
    }
    throw new IllegalStateException(getExceptionMessage(type));
}
 
Example 8
Source File: AbstractTemplate.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the type of the template model to use with with this template.
 *
 * @return the model type, not <code>null</code>
 */
@SuppressWarnings("unchecked")
protected Class<? extends M> getModelType() {
    Type type = GenericTypeReflector.getTypeParameter(
            getClass().getGenericSuperclass(),
            AbstractTemplate.class.getTypeParameters()[0]);
    if (type instanceof Class || type instanceof ParameterizedType) {
        return (Class<M>) GenericTypeReflector.erase(type);
    }
    throw new IllegalStateException(getExceptionMessage(type));
}
 
Example 9
Source File: ParameterDeserializer.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Get the parameter type class.
 *
 * @param navigationTarget
 *            navigation target to get parameter type class for
 * @return parameter type class
 */
public static Class<?> getClassType(Class<?> navigationTarget) {
    Type type = GenericTypeReflector.getTypeParameter(navigationTarget,
            HasUrlParameter.class.getTypeParameters()[0]);
    if (!(type instanceof Class<?>)) {
        throw new IllegalArgumentException(String.format(
                "Parameter type of the given navigationTarget '%s' could not be resolved.",
                navigationTarget.getName()));
    }
    return GenericTypeReflector.erase(type);
}
 
Example 10
Source File: Binder.java    From flow with Apache License 2.0 5 votes vote down vote up
/**
 * Binds {@code property} with {@code propertyType} to the field in the
 * {@code objectWithMemberFields} instance using {@code memberField} as a
 * reference to a member.
 *
 * @param objectWithMemberFields
 *            the object that contains (Java) member fields to build and
 *            bind
 * @param memberField
 *            reference to a member field to bind
 * @param property
 *            property name to bind
 * @param propertyType
 *            type of the property
 * @return {@code true} if property is successfully bound
 */
private boolean bindProperty(Object objectWithMemberFields,
        Field memberField, String property, Class<?> propertyType) {
    Type valueType = GenericTypeReflector.getTypeParameter(
            memberField.getGenericType(),
            HasValue.class.getTypeParameters()[1]);
    if (valueType == null) {
        throw new IllegalStateException(String.format(
                "Unable to detect value type for the member '%s' in the "
                        + "class '%s'.",
                memberField.getName(),
                objectWithMemberFields.getClass().getName()));
    }
    if (propertyType.equals(GenericTypeReflector.erase(valueType))) {
        HasValue<?, ?> field;
        // Get the field from the object
        try {
            field = (HasValue<?, ?>) ReflectTools.getJavaFieldValue(
                    objectWithMemberFields, memberField, HasValue.class);
        } catch (IllegalArgumentException | IllegalAccessException
                | InvocationTargetException e) {
            // If we cannot determine the value, just skip the field
            return false;
        }
        if (field == null) {
            field = makeFieldInstance(
                    (Class<? extends HasValue<?, ?>>) memberField
                            .getType());
            initializeField(objectWithMemberFields, memberField, field);
        }
        forField(field).bind(property);
        return true;
    } else {
        throw new IllegalStateException(String.format(
                "Property type '%s' doesn't "
                        + "match the field type '%s'. "
                        + "Binding should be configured manually using converter.",
                propertyType.getName(), valueType.getTypeName()));
    }
}