Search in sources :

Example 1 with Transformer

use of com.google.api.server.spi.config.Transformer in project endpoints-java by cloudendpoints.

the class Serializers method getSerializerClasses.

public static List<Class<? extends Transformer<?, ?>>> getSerializerClasses(TypeToken<?> type, @Nullable final ApiSerializationConfig config) {
    if (type == null) {
        return Collections.emptyList();
    }
    List<Class<? extends Transformer<?, ?>>> allParentSerializers = Lists.newArrayList();
    List<TypeToken<?>> serializedTypes = Lists.newArrayList();
    for (TypeToken<?> typeToken : type.getTypes()) {
        ApiTransformer apiSerialization = typeToken.getRawType().getAnnotation(ApiTransformer.class);
        if (isSupertypeOf(typeToken, serializedTypes)) {
            continue;
        }
        if (apiSerialization != null) {
            allParentSerializers.add(apiSerialization.value());
            serializedTypes.add(typeToken);
        } else if (config != null) {
            ApiSerializationConfig.SerializerConfig serializerConfig = config.getSerializerConfig(typeToken);
            if (serializerConfig != null) {
                allParentSerializers.add(serializerConfig.getSerializer());
                serializedTypes.add(typeToken);
            }
        }
    }
    return allParentSerializers;
}
Also used : ApiTransformer(com.google.api.server.spi.config.ApiTransformer) Transformer(com.google.api.server.spi.config.Transformer) ApiTransformer(com.google.api.server.spi.config.ApiTransformer) TypeToken(com.google.common.reflect.TypeToken)

Example 2 with Transformer

use of com.google.api.server.spi.config.Transformer in project endpoints-java by cloudendpoints.

the class ApiAnnotationIntrospector method getSchemaType.

/**
 * Gets the schema type for a type. The schema type is identical to the original type if
 * there is no matching {@link com.google.api.server.spi.config.ApiTransformer} annotation for
 * the type. If there is a {@link com.google.api.server.spi.config.ResourceTransformer} installed,
 * the source type determines schema, not the output map.
 */
public static TypeToken<?> getSchemaType(TypeToken<?> type, ApiConfig config) {
    Type rawType = type.getType();
    if (rawType instanceof Class || rawType instanceof ParameterizedType) {
        List<Class<? extends Transformer<?, ?>>> serializers = Serializers.getSerializerClasses(type, config.getSerializationConfig());
        if (!serializers.isEmpty() && !(ResourceTransformer.class.isAssignableFrom(serializers.get(0)))) {
            TypeToken<?> sourceType = Serializers.getSourceType(serializers.get(0));
            TypeToken<?> serializedType = Serializers.getTargetType(serializers.get(0));
            Preconditions.checkArgument(sourceType.isSupertypeOf(type), "Serializer specified for %s, but only serializes for %s: %s", type, sourceType, serializers.get(0));
            Preconditions.checkArgument(serializedType != null, "Couldn't find Serializer interface in serializer for %s: %s", type, serializers.get(0));
            return serializedType;
        }
    }
    return type;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ResourceTransformer(com.google.api.server.spi.config.ResourceTransformer) Transformer(com.google.api.server.spi.config.Transformer) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) ResourceTransformer(com.google.api.server.spi.config.ResourceTransformer)

Example 3 with Transformer

use of com.google.api.server.spi.config.Transformer in project endpoints-java by cloudendpoints.

the class ApiAnnotationIntrospector method findSerializerInstance.

@Nullable
private Transformer<?, ?> findSerializerInstance(Annotated a) {
    if (a instanceof AnnotatedClass) {
        AnnotatedClass clazz = (AnnotatedClass) a;
        List<Class<? extends Transformer<?, ?>>> serializerClasses = Serializers.getSerializerClasses(clazz.getRawType(), config);
        if (!serializerClasses.isEmpty()) {
            return Serializers.instantiate(serializerClasses.get(0), TypeToken.of(a.getRawType()));
        }
    }
    return null;
}
Also used : ResourceTransformer(com.google.api.server.spi.config.ResourceTransformer) Transformer(com.google.api.server.spi.config.Transformer) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) Nullable(javax.annotation.Nullable)

Example 4 with Transformer

use of com.google.api.server.spi.config.Transformer in project endpoints-java by cloudendpoints.

the class ApiConfigAnnotationReader method readEndpointClass.

@SuppressWarnings("unchecked")
private boolean readEndpointClass(ApiConfig config, Class<?> endpointClass, Annotation api, Annotation apiClass, @Nullable Class<?> cycleCheck) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, CyclicApiInheritanceException {
    cycleCheck = checkForInheritanceCycle(endpointClass, cycleCheck);
    boolean hasAnnotation = api != null;
    Class<?> inheritanceSource = determineInheritanceSource(endpointClass);
    if (inheritanceSource != null) {
        Annotation superApi = getDeclaredAnnotation(inheritanceSource, annotationTypes.get("Api"));
        Annotation superApiClass = getDeclaredAnnotation(inheritanceSource, annotationTypes.get("ApiClass"));
        hasAnnotation |= readEndpointClass(config, inheritanceSource, superApi, superApiClass, cycleCheck);
    }
    if (api != null) {
        readApi(new ApiAnnotationConfig(config), api);
        readApiAuth(new ApiAuthAnnotationConfig(config.getAuthConfig()), (Annotation) getAnnotationProperty(api, "auth"));
        readApiFrontendLimits(new ApiFrontendLimitsAnnotationConfig(config.getFrontendLimitsConfig()), (Annotation) getAnnotationProperty(api, "frontendLimits"));
        readApiCacheControl(new ApiCacheControlAnnotationConfig(config.getCacheControlConfig()), (Annotation) getAnnotationProperty(api, "cacheControl"));
        readApiNamespace(new ApiNamespaceAnnotationConfig(config.getNamespaceConfig()), (Annotation) getAnnotationProperty(api, "namespace"));
        readSerializers(config.getSerializationConfig(), (Class<? extends Transformer<?, ?>>[]) getAnnotationProperty(api, "transformers"));
    }
    if (apiClass != null) {
        readApiClass(new ApiClassAnnotationConfig(config.getApiClassConfig()), apiClass);
    }
    return hasAnnotation;
}
Also used : Transformer(com.google.api.server.spi.config.Transformer) Annotation(java.lang.annotation.Annotation)

Aggregations

Transformer (com.google.api.server.spi.config.Transformer)4 AnnotatedClass (com.fasterxml.jackson.databind.introspect.AnnotatedClass)2 ResourceTransformer (com.google.api.server.spi.config.ResourceTransformer)2 ApiTransformer (com.google.api.server.spi.config.ApiTransformer)1 TypeToken (com.google.common.reflect.TypeToken)1 Annotation (java.lang.annotation.Annotation)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Nullable (javax.annotation.Nullable)1