Example usage for org.apache.commons.lang3.reflect TypeUtils unrollVariables

List of usage examples for org.apache.commons.lang3.reflect TypeUtils unrollVariables

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect TypeUtils unrollVariables.

Prototype

public static Type unrollVariables(Map<TypeVariable<?>, Type> typeArguments, final Type type) 

Source Link

Document

Get a type representing type with variable assignments "unrolled."

Usage

From source file:org.apache.bval.util.IndexedAccess.java

/**
 * Get the Java element type of a particular container type.
 * //from   w w  w  .ja  v a  2 s  . c o  m
 * @param containerType
 * @return Type or <code>null</code> if <code>containerType</code> is not
 *         some type of {@link Iterable} or array
 */
public static Type getJavaElementType(Type containerType) {
    if (TypeUtils.isArrayType(containerType)) {
        return TypeUtils.getArrayComponentType(containerType);
    }
    if (TypeUtils.isAssignable(containerType, Iterable.class)) {
        Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Iterable.class);
        return ObjectUtils.defaultIfNull(TypeUtils.unrollVariables(typeArguments, ITERABLE_TYPE), Object.class);
    }
    return null;
}

From source file:org.apache.bval.util.KeyedAccess.java

/**
 * Get the Java element type of a particular container type.
 * //from www  .ja va  2  s.c  om
 * @param containerType
 * @return Type or <code>null</code> if <code>containerType</code> is not
 *         some kind of {@link Map}
 */
public static Type getJavaElementType(Type containerType) {
    if (TypeUtils.isAssignable(containerType, Map.class)) {
        Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Map.class);
        return ObjectUtils.defaultIfNull(TypeUtils.unrollVariables(typeArguments, MAP_TYPEVARS[1]),
                Object.class);
    }
    return null;
}

From source file:org.apache.bval.util.KeyedAccess.java

/**
 * {@inheritDoc}//  ww  w  .ja v  a 2 s. c  o  m
 */
@Override
public Object get(Object instance) {
    if (instance instanceof Map<?, ?>) {
        Map<?, ?> map = (Map<?, ?>) instance;
        Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Map.class);
        Type keyType = TypeUtils.unrollVariables(typeArguments, MAP_TYPEVARS[0]);
        if (key == null || keyType == null || TypeUtils.isInstance(key, keyType)) {
            return map.get(key);
        }
        if (key instanceof String) {
            String name = (String) key;
            Class<?> rawKeyType = TypeUtils.getRawType(keyType, containerType);
            if (rawKeyType.isEnum()) {
                @SuppressWarnings({ "unchecked", "rawtypes" })
                final Object result = map.get(Enum.valueOf((Class<? extends Enum>) rawKeyType, name));
                return result;
            }
            for (Map.Entry<?, ?> e : map.entrySet()) {
                if (name.equals(e.getKey())) {
                    return e.getValue();
                }
            }
        }
    }
    return null;
}

From source file:org.nuxeo.ecm.core.io.registry.reflect.MarshallerInspector.java

/**
 * Get the Java class and generic type managed by this marshaller. If not found, search in the parent.
 *
 * @param clazz The marshaller class to analyse.
 * @since 7.2//from   w  ww.  j  a v a  2  s.  co  m
 */
private void loadMarshalledType(Class<?> clazz) {
    if (isWriter() || isReader()) {
        Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(clazz, Marshaller.class);
        for (Map.Entry<TypeVariable<?>, Type> entry : typeArguments.entrySet()) {
            if (Marshaller.class.equals(entry.getKey().getGenericDeclaration())) {
                genericType = TypeUtils.unrollVariables(typeArguments, entry.getValue());
                marshalledType = TypeUtils.getRawType(genericType, null);
                break;
            }
        }
    }
}

From source file:therian.Operation.java

private static boolean init(Class<?> type) {
    final boolean valid;
    synchronized (type) {
        if (VALID_INFO.containsKey(type)) {
            valid = VALID_INFO.get(type).booleanValue();
        } else if (Modifier.isAbstract(type.getModifiers())) {
            valid = true;/*w w w .java  2 s  . c  om*/
        } else {
            final Type resultType = TypeUtils.unrollVariables(TypeUtils.getTypeArguments(type, Operation.class),
                    TYPE_VARIABLE_RESULT);
            valid = !TypeUtils.containsTypeVariables(resultType);
            Validate.isTrue(valid, "%s does not fully bind type parameter %s from %s", type,
                    TYPE_VARIABLE_RESULT.getName(), Operation.class);
            VALID_INFO.put(type, Boolean.valueOf(valid));
        }
    }

    final Class<?> parent = type.getSuperclass();
    if (!Operation.class.equals(parent)) {
        init(parent.asSubclass(Operation.class));
    }
    return valid;
}

From source file:therian.Operation.java

/**
 * Learn whether {@code operator} seems to implement {@code this}.
 *
 * @param operator to check// w  ww  .  j  a  v a 2s.c o m
 * @return boolean
 */
public boolean matches(Operator<?> operator) {
    final Type expectedType = TypeUtils.unrollVariables(
            TypeUtils.getTypeArguments(operator.getClass(), Operator.class),
            Operator.class.getTypeParameters()[0]);

    if (!TypeUtils.isInstance(this, expectedType)) {
        return false;
    }

    final Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(expectedType, Operation.class);
    for (Class<?> c : ClassUtils.hierarchy(TypeUtils.getRawType(expectedType, operator.getClass()))) {
        if (c.equals(Operation.class)) {
            break;
        }
        for (TypeVariable<?> var : c.getTypeParameters()) {
            Type type = Types.resolveAt(this, var, typeArguments);
            if (type == null || typeArguments == null) {
                continue;
            }
            if (type instanceof Class<?> && ((Class<?>) type).isPrimitive()) {
                type = ClassUtils.primitiveToWrapper((Class<?>) type);
            }
            if (!TypeUtils.isAssignable(type, TypeUtils.unrollVariables(typeArguments, var))) {
                return false;
            }
        }
    }
    return true;
}

From source file:therian.operator.add.AddEntryToMap.java

@Override
public boolean supports(TherianContext context, Add<? extends Map.Entry, ? extends Map> add) {
    // cannot add to immutable types
    if (context.eval(ImmutableCheck.of(add.getTargetPosition())).booleanValue()) {
        return false;
    }/*  ww w  . j  av  a  2 s  . c  om*/
    if (add.getSourcePosition().getValue() == null) {
        return false;
    }
    final Map<TypeVariable<?>, Type> targetArgs = TypeUtils.getTypeArguments(add.getTargetType().getType(),
            Map.class);

    final Type targetKeyType = TypeUtils.unrollVariables(targetArgs, Map.class.getTypeParameters()[0]);

    if (targetKeyType != null
            && !TypeUtils.isInstance(add.getSourcePosition().getValue().getKey(), targetKeyType)) {
        return false;
    }
    final Type targetValueType = TypeUtils.unrollVariables(targetArgs, Map.class.getTypeParameters()[1]);

    return targetValueType == null
            || TypeUtils.isInstance(add.getSourcePosition().getValue().getValue(), targetValueType);
}

From source file:therian.operator.add.AddToCollection.java

@Override
public boolean supports(TherianContext context, Add<?, ? extends Collection<?>> add) {
    // cannot add to immutable types
    if (context.eval(ImmutableCheck.of(add.getTargetPosition())).booleanValue()) {
        return false;
    }// www. j  a v  a2s  .  co  m
    if (!TypeUtils.isAssignable(add.getTargetPosition().getType(), Collection.class)) {
        return false;
    }
    final Type targetElementType = TypeUtils.unrollVariables(
            TypeUtils.getTypeArguments(add.getTargetPosition().getType(), Collection.class),
            Collection.class.getTypeParameters()[0]);

    if (targetElementType == null) {
        // raw collection
        return true;
    }
    return TypeUtils.isAssignable(add.getSourcePosition().getType(), targetElementType);
}

From source file:therian.operator.add.AddToListIterator.java

@Override
public boolean supports(TherianContext context, Add<?, ? extends ListIterator<?>> add) {
    // cannot add to immutable types
    if (context.eval(ImmutableCheck.of(add.getTargetPosition())).booleanValue()) {
        return false;
    }/*from w  w w . j  av a  2 s .c o m*/
    if (!TypeUtils.isAssignable(add.getTargetPosition().getType(), ListIterator.class)) {
        return false;
    }
    final Type targetElementType = TypeUtils.unrollVariables(
            TypeUtils.getTypeArguments(add.getTargetPosition().getType(), ListIterator.class),
            ListIterator.class.getTypeParameters()[0]);

    if (targetElementType == null) {
        // raw
        return true;
    }
    return TypeUtils.isAssignable(add.getSourcePosition().getType(), targetElementType);
}

From source file:therian.operator.convert.AssignableElementConverter.java

protected Type sourceComponentType(Typed<?> item) {
    final Type t = item.getType();
    final Class<?> varOwner = sourceElementType.getGenericDeclaration();

    final Map<TypeVariable<?>, Type> args = TypeUtils.getTypeArguments(t, varOwner);
    return args == null ? null
            : ObjectUtils.defaultIfNull(TypeUtils.unrollVariables(args, sourceElementType), Object.class);
}