Example usage for com.google.common.reflect TypeResolver resolveType

List of usage examples for com.google.common.reflect TypeResolver resolveType

Introduction

In this page you can find the example usage for com.google.common.reflect TypeResolver resolveType.

Prototype

public Type resolveType(Type type) 

Source Link

Document

Resolves all type variables in type and all downstream types and returns a corresponding type with type variables resolved.

Usage

From source file:org.assertj.core.test.TypeCanonizer.java

/**
 * Returns a canonical form of {@code initialType} by replacing all {@link TypeVariable} by {@link Class}
 * instances.//from  w  ww . ja v  a 2 s  . c o m
 * <p>
 * <p>
 * Such a canonical form allows to compare {@link ParameterizedType}s, {@link WildcardType}(s),
 * {@link GenericArrayType}(s), {@link TypeVariable}(s).
 * </p>
 */
public static Type canonize(Type initialType) {
    if (doesNotNeedCanonization(initialType)) {
        return initialType;
    }

    ReplacementClassSupplier replacementClassSupplier = new ReplacementClassSupplier();
    TypeResolver typeResolver = new TypeResolver();

    for (TypeVariable<?> typeVariable : findAllTypeVariables(initialType)) {
        // Once we have all TypeVariable we need to resolve them with actual classes so the typeResolver can resolve
        // them properly
        typeResolver = typeResolver.where(typeVariable, replacementClassSupplier.get());
    }

    return typeResolver.resolveType(initialType);
}

From source file:org.apache.beam.sdk.values.TypeDescriptor.java

/**
 * Returns a new {@code TypeDescriptor} where type variables represented by
 * {@code typeParameter} are substituted by {@code typeDescriptor}. For example, it can be used to
 * construct {@code Map<K, V>} for any {@code K} and {@code V} type: <pre> {@code
 *   static <K, V> TypeDescriptor<Map<K, V>> mapOf(
 *       TypeDescriptor<K> keyType, TypeDescriptor<V> valueType) {
 *     return new TypeDescriptor<Map<K, V>>() {}
 *         .where(new TypeParameter<K>() {}, keyType)
 *         .where(new TypeParameter<V>() {}, valueType);
 *   }}</pre>/*from   www. j  ava2 s  . co  m*/
 *
 * @param <X> The parameter type
 * @param typeParameter the parameter type variable
 * @param typeDescriptor the actual type to substitute
 */
@SuppressWarnings("unchecked")
public <X> TypeDescriptor<T> where(TypeParameter<X> typeParameter, TypeDescriptor<X> typeDescriptor) {
    TypeResolver resolver = new TypeResolver().where(typeParameter.typeVariable, typeDescriptor.getType());
    return (TypeDescriptor<T>) TypeDescriptor.of(resolver.resolveType(token.getType()));
}

From source file:org.jclouds.util.TypeToken2.java

@SuppressWarnings("unchecked")
public <X, Y> TypeToken<T> where(TypeParameter2<X> typeParam1, TypeToken<X> typeArg1,
        TypeParameter2<Y> typeParam2, TypeToken<Y> typeArg2) {
    // resolving both parameters in one shot seems to work around 1635
    TypeResolver resolver = new TypeResolver();
    // where(Map) is package-private in TypeResolver
    Invokable<TypeResolver, TypeResolver> whereWithMap = Reflection2
            .<TypeResolver, TypeResolver>method(TypeResolver.class, "where", Map.class);
    try {//from w  ww.  j a  va 2 s. c  o m
        resolver = whereWithMap.invoke(resolver, ImmutableMap.of(typeParam1.getTypeVariable(),
                typeArg1.getType(), typeParam2.getTypeVariable(), typeArg2.getType()));
    } catch (IllegalAccessException exception) {
        // should never happen
        throw new IllegalStateException(exception);
    } catch (InvocationTargetException exception) {
        // should never happen
        throw new IllegalStateException(exception);
    }
    return (TypeToken<T>) TypeToken.of(resolver.resolveType(getType()));
}

From source file:edu.mit.streamjit.impl.compiler2.Compiler2.java

private boolean inferUpward() {
    boolean changed = false;
    //For each storage, if a reader's input type is a final type, all
    //writers' output types must be that final type.  (Wrappers are final,
    //so this works for wrappers, and maybe detects errors related to other
    //types.)//from www  . j a  v a  2s. co m
    for (Storage s : storage) {
        Set<TypeToken<?>> finalInputTypes = new HashSet<>();
        for (Actor a : s.downstream())
            if (Modifier.isFinal(a.inputType().getRawType().getModifiers()))
                finalInputTypes.add(a.inputType());
        if (finalInputTypes.isEmpty())
            continue;
        if (finalInputTypes.size() > 1)
            throw new IllegalStreamGraphException("Type mismatch among readers: " + s.downstream());

        TypeToken<?> inputType = finalInputTypes.iterator().next();
        for (Actor a : s.upstream())
            if (!a.outputType().equals(inputType)) {
                TypeToken<?> oldOutputType = a.outputType();
                TypeResolver resolver = new TypeResolver().where(oldOutputType.getType(), inputType.getType());
                TypeToken<?> newOutputType = TypeToken.of(resolver.resolveType(oldOutputType.getType()));
                if (!oldOutputType.equals(newOutputType)) {
                    a.setOutputType(newOutputType);
                    //                  System.out.println("inferUpward: inferred "+a+" output type: "+oldOutputType+" -> "+newOutputType);
                    changed = true;
                }

                TypeToken<?> oldInputType = a.inputType();
                TypeToken<?> newInputType = TypeToken.of(resolver.resolveType(oldInputType.getType()));
                if (!oldInputType.equals(newInputType)) {
                    a.setInputType(newInputType);
                    //                  System.out.println("inferUpward: inferred "+a+" input type: "+oldInputType+" -> "+newInputType);
                    changed = true;
                }
            }
    }
    return changed;
}

From source file:edu.mit.streamjit.impl.compiler2.Compiler2.java

private boolean inferDownward() {
    boolean changed = false;
    //For each storage, find the most specific common type among all the
    //writers' output types, then if it's final, unify with any variable or
    //wildcard reader input type.  (We only unify if final to avoid removing
    //a type variable too soon.  We could also refine concrete types like
    //Object to a more specific subclass.)
    for (Storage s : storage) {
        Set<? extends TypeToken<?>> commonTypes = null;
        for (Actor a : s.upstream())
            if (commonTypes == null)
                commonTypes = a.outputType().getTypes();
            else/*w  w w .j  ava2  s .  co m*/
                commonTypes = Sets.intersection(commonTypes, a.outputType().getTypes());
        if (commonTypes.isEmpty())
            throw new IllegalStreamGraphException("No common type among writers: " + s.upstream());

        TypeToken<?> mostSpecificType = commonTypes.iterator().next();
        if (!Modifier.isFinal(mostSpecificType.getRawType().getModifiers()))
            continue;
        for (Actor a : s.downstream()) {
            TypeToken<?> oldInputType = a.inputType();
            //TODO: this isn't quite right?
            if (!ReflectionUtils.containsVariableOrWildcard(oldInputType.getType()))
                continue;

            TypeResolver resolver = new TypeResolver().where(oldInputType.getType(),
                    mostSpecificType.getType());
            TypeToken<?> newInputType = TypeToken.of(resolver.resolveType(oldInputType.getType()));
            if (!oldInputType.equals(newInputType)) {
                a.setInputType(newInputType);
                //               System.out.println("inferDownward: inferred "+a+" input type: "+oldInputType+" -> "+newInputType);
                changed = true;
            }

            TypeToken<?> oldOutputType = a.outputType();
            TypeToken<?> newOutputType = TypeToken.of(resolver.resolveType(oldOutputType.getType()));
            if (!oldOutputType.equals(newOutputType)) {
                a.setOutputType(newOutputType);
                //               System.out.println("inferDownward: inferred "+a+" output type: "+oldOutputType+" -> "+newOutputType);
                changed = true;
            }
        }
    }
    return changed;
}