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

The following examples show how to use com.googlecode.gentyref.GenericTypeReflector#erase() . 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: ReflectionUtils.java    From elepy with Apache License 2.0 5 votes vote down vote up
public static Class<?> returnTypeOf(AnnotatedElement field) {
    if (field instanceof AnnotatedType) {
        return (Class) ((AnnotatedType) field).getType();
    }
    if (field instanceof Parameter) {
        return GenericTypeReflector.erase(((Parameter) field).getType());
    }
    return (field instanceof Field) ? ((Field) field).getType() : ((Method) field).getReturnType();
}
 
Example 3
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 4
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 5
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 6
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 7
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 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: AbstractSinglePropertyField.java    From flow with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> Class<T> findElementPropertyTypeFromTypeParameter(
        Class<?> hasValueClass) {
    return (Class<T>) GenericTypeReflector
            .erase(GenericTypeReflector.getTypeParameter(hasValueClass,
                    HasValue.class.getTypeParameters()[1]));
}
 
Example 10
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);
}