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

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

Introduction

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

Prototype

public final TypeToken<?> resolveType(Type type) 

Source Link

Document

Resolves the given type against the type context represented by this type.

Usage

From source file:org.zalando.switchboard.TypeResolver.java

static <T> Class<T> resolve(final Object instance, final Class<?> type, final int index) {
    final TypeToken<?> token = TypeToken.of(instance.getClass());
    final TypeToken<?> resolved = token.resolveType(type.getTypeParameters()[index]);
    return cast(resolved.getRawType());
}

From source file:com.github.steveash.spring.WiringFactoryBeanFactoryPostProcessor.java

@Nullable
static java.lang.Class<?> getPrototypeClassFromFactory(Class<?> factoryClass) {
    if (!WiringFactory.class.isAssignableFrom(factoryClass))
        return null;

    TypeToken<?> tok = TypeToken.of(factoryClass);
    TypeToken<?> prototypeTok = tok.resolveType(WiringFactory.class.getTypeParameters()[0]);
    return prototypeTok.getRawType();
}

From source file:com.google.api.server.spi.config.model.Types.java

/**
 * Gets the element type of a type we want to treat as an array. Actual arrays or subtypes of
 * {@link java.util.Collection} can be treated as arrays. Returns null if the type cannot be
 * treated as an array.//ww w.ja  v  a  2s  .c o m
 */
public static TypeToken<?> getArrayItemType(TypeToken<?> type) {
    if (type.isSubtypeOf(Collection.class)) {
        return type.resolveType(Collection.class.getTypeParameters()[0]);
    } else if (type.isArray()) {
        return type.getComponentType();
    }
    return null;
}

From source file:ratpack.handling.internal.Extractions.java

public static void extract(List<TypeToken<?>> types, Registry registry, Object[] services, int startIndex) {
    for (int i = 0; i < types.size(); ++i) {
        TypeToken<?> type = types.get(i);
        if (type.getRawType().equals(Optional.class)) {
            TypeToken<?> paramType;
            try {
                paramType = type.resolveType(Optional.class.getMethod("get").getGenericReturnType());
            } catch (NoSuchMethodException e) {
                throw new InternalError("Optional class does not have get method");
            }/*w ww . j  av  a2  s  . c o m*/
            Object optional = registry.maybeGet(paramType);
            services[i + startIndex] = optional;

        } else {
            Object service = registry.get(type);
            services[i + startIndex] = service;
        }

    }
}

From source file:net.navatwo.jfxproperties.MoreReflection.java

/**
 * Resolves the {@code index}th type parameter from the token.
 *
 * @param token/*w w w .  j  ava2  s  .  c  o m*/
 * @param index
 * @return
 * @throws ArrayIndexOutOfBoundsException if the type parameter doesn't exist
 */
public static TypeToken<?> resolveTypeParameter(TypeToken<?> token, int index) {
    return token.resolveType(token.getRawType().getTypeParameters()[index]);
}

From source file:co.cask.cdap.internal.app.deploy.InMemoryConfigurator.java

private static String getSpecJson(Application app, final String bundleVersion, final String configString)
        throws IllegalAccessException, InstantiationException {
    // Now, we call configure, which returns application specification.
    DefaultAppConfigurer configurer = new DefaultAppConfigurer(app, configString);

    Config appConfig;//from   ww w .j a va2  s.c  o  m
    TypeToken typeToken = TypeToken.of(app.getClass());
    TypeToken<?> configToken = typeToken.resolveType(Application.class.getTypeParameters()[0]);
    if (Strings.isNullOrEmpty(configString)) {
        appConfig = (Config) configToken.getRawType().newInstance();
    } else {
        //TODO: CDAP-2869 Handle JsonSyntax Exception better and throw a more meaningful error. This will be done when we
        //use PluginInstantiator methods instead of GSON deserialization
        appConfig = GSON.fromJson(configString, configToken.getType());
    }

    app.configure(configurer, new DefaultApplicationContext(appConfig));
    ApplicationSpecification specification = configurer.createSpecification(bundleVersion);

    // Convert the specification to JSON.
    // We write the Application specification to output file in JSON format.
    // TODO: The SchemaGenerator should be injected
    return ApplicationSpecificationAdapter.create(new ReflectionSchemaGenerator()).toJson(specification);
}

From source file:com.google.api.server.spi.config.model.Serializers.java

/**
 * Gets the {@code Serializer} source type for a class. This resolves placeholders in generics.
 *
 * @param clazz a class, possibly implementing {@code Transformer}
 * @return the resolved source type, null if clazz is not a serializer
 *//* w  w  w . j  av a 2s  .  c  o m*/
@Nullable
public static TypeToken<?> getSourceType(Class<? extends Transformer<?, ?>> clazz) {
    try {
        TypeToken<?> token = TypeToken.of(clazz);
        return token
                .resolveType(Transformer.class.getMethod("transformFrom", Object.class).getGenericReturnType());
    } catch (NoSuchMethodException e) {
        return null;
    }
}

From source file:com.google.api.server.spi.config.model.Serializers.java

/**
 * Gets the {@code Serializer} target type for a class. This resolves placeholders in generics.
 *
 * @param clazz a class, possibly implementing {@code Transformer}
 * @return the resolved target type, null if clazz is not a serializer
 *///from w ww  .  j  av  a  2s.com
@Nullable
public static TypeToken<?> getTargetType(Class<? extends Transformer<?, ?>> clazz) {
    try {
        TypeToken<?> token = TypeToken.of(clazz);
        return token
                .resolveType(Transformer.class.getMethod("transformTo", Object.class).getGenericReturnType());
    } catch (NoSuchMethodException e) {
        return null;
    }
}

From source file:com.google.api.server.spi.config.jsonwriter.JacksonResourceSchemaProvider.java

private static boolean hasUnresolvedType(TypeToken<?> type) {
    Type javaType = type.getType();
    if (javaType instanceof ParameterizedType) {
        ParameterizedType p = (ParameterizedType) javaType;
        for (Type t : p.getActualTypeArguments()) {
            if (Types.isWildcardType(type.resolveType(t))) {
                logger.warning("skipping field of type " + type + " because it is unresolved");
                return true;
            }/*from  ww w .  ja va  2 s  .c  o  m*/
        }
    }
    return false;
}

From source file:org.jboss.errai.config.rebind.ProxyUtil.java

public static String createCallSignature(final Class<?> referenceClass, final Method m) {
    final TypeToken<?> resolver = TypeToken.of(referenceClass);
    final StringBuilder append = new StringBuilder(m.getName()).append(':');
    for (final Type c : m.getGenericParameterTypes()) {
        final TypeToken<?> resolvedParamType = resolver.resolveType(c);
        append.append(resolvedParamType.getRawType().getCanonicalName()).append(':');
    }/*  w w w.jav a2 s . c  o  m*/
    return append.toString();
}