Search in sources :

Example 1 with AdapterType

use of com.webcohesion.enunciate.modules.jackson.model.adapters.AdapterType in project enunciate by stoicflame.

the class JsonTypeVisitor method visitDeclared.

@Override
public JsonType visitDeclared(DeclaredType declaredType, Context context) {
    JsonType jsonType = null;
    Element declaredElement = declaredType.asElement();
    DecoratedProcessingEnvironment env = context.getContext().getContext().getProcessingEnvironment();
    DecoratedTypeMirror decoratedTypeMirror = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(declaredType, env);
    String fqn = declaredElement instanceof TypeElement ? ((TypeElement) declaredElement).getQualifiedName().toString() : declaredType.toString();
    if (context.getStack().contains(fqn) && !decoratedTypeMirror.isCollection() && !decoratedTypeMirror.isStream()) {
        // break out of recursive loop.
        return wrapAsNeeded(KnownJsonType.OBJECT, context);
    }
    context.getStack().push(fqn);
    try {
        TypeHint typeHint = declaredElement.getAnnotation(TypeHint.class);
        if (typeHint != null) {
            TypeMirror hint = TypeHintUtils.getTypeHint(typeHint, context.getContext().getContext().getProcessingEnvironment(), null);
            if (hint != null) {
                jsonType = hint.accept(this, new Context(context.context, false, false, context.stack));
            }
        }
        final JsonSerialize serializeInfo = declaredElement.getAnnotation(JsonSerialize.class);
        if (serializeInfo != null) {
            DecoratedTypeMirror using = Annotations.mirrorOf(new Callable<Class<?>>() {

                @Override
                public Class<?> call() throws Exception {
                    return serializeInfo.using();
                }
            }, env, JsonSerializer.None.class);
            if (using != null) {
                // custom serializer; just say it's an object.
                jsonType = KnownJsonType.OBJECT;
            }
            DecoratedTypeMirror as = Annotations.mirrorOf(new Callable<Class<?>>() {

                @Override
                public Class<?> call() throws Exception {
                    return serializeInfo.as();
                }
            }, env, Void.class);
            if (as != null) {
                jsonType = (JsonType) as.accept(this, new Context(context.context, false, false, context.stack));
            }
        }
        AdapterType adapterType = JacksonUtil.findAdapterType(declaredElement, context.getContext());
        if (adapterType != null) {
            adapterType.getAdaptingType().accept(this, new Context(context.context, false, false, context.stack));
        } else {
            MapType mapType = MapType.findMapType(declaredType, context.getContext());
            if (mapType != null) {
                JsonType keyType = mapType.getKeyType().accept(this, new Context(context.getContext(), false, false, context.stack));
                JsonType valueType = mapType.getValueType().accept(this, new Context(context.getContext(), false, false, context.stack));
                jsonType = new JsonMapType(keyType, valueType);
            } else {
                TypeMirror componentType = getComponentType(decoratedTypeMirror, env);
                if (componentType != null) {
                    return wrapAsNeeded(componentType.accept(this, new Context(context.context, false, true, context.stack)), context);
                } else {
                    switch(declaredElement.getKind()) {
                        case ENUM:
                        case CLASS:
                        case INTERFACE:
                            JsonType knownType = context.getContext().getKnownType(declaredElement);
                            if (knownType != null) {
                                jsonType = knownType;
                            } else {
                                // type not known, not specified.  Last chance: look for the type definition.
                                TypeDefinition typeDefinition = context.getContext().findTypeDefinition(declaredElement);
                                if (typeDefinition != null) {
                                    jsonType = new JsonClassType(typeDefinition);
                                }
                            }
                            break;
                    }
                }
            }
        }
        if (jsonType == null) {
            jsonType = super.visitDeclared(declaredType, context);
        }
        return wrapAsNeeded(jsonType, context);
    } finally {
        context.getStack().pop();
    }
}
Also used : EnunciateJacksonContext(com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext) TypeHint(com.webcohesion.enunciate.metadata.rs.TypeHint) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) AdapterType(com.webcohesion.enunciate.modules.jackson.model.adapters.AdapterType) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) DecoratedProcessingEnvironment(com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment) MapType(com.webcohesion.enunciate.modules.jackson.model.util.MapType) TypeDefinition(com.webcohesion.enunciate.modules.jackson.model.TypeDefinition) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

Example 2 with AdapterType

use of com.webcohesion.enunciate.modules.jackson.model.adapters.AdapterType in project enunciate by stoicflame.

the class JacksonUtil method findAdapterType.

private static AdapterType findAdapterType(DecoratedTypeMirror maybeContainedAdaptedType, Element referer, EnunciateJacksonContext context) {
    DecoratedProcessingEnvironment env = context.getContext().getProcessingEnvironment();
    TypeMirror adaptedType = TypeMirrorUtils.getComponentType(maybeContainedAdaptedType, env);
    final boolean isContained = adaptedType != null;
    adaptedType = isContained ? adaptedType : maybeContainedAdaptedType;
    JsonSerialize serializationInfo = referer != null ? referer.getAnnotation(JsonSerialize.class) : null;
    if (serializationInfo == null && adaptedType instanceof DeclaredType) {
        serializationInfo = ((DeclaredType) adaptedType).asElement().getAnnotation(JsonSerialize.class);
    }
    if (serializationInfo != null) {
        final JsonSerialize finalInfo = serializationInfo;
        DecoratedTypeMirror adapterTypeMirror = Annotations.mirrorOf(new Callable<Class<?>>() {

            @Override
            public Class<?> call() throws Exception {
                return isContained ? finalInfo.contentConverter() : finalInfo.converter();
            }
        }, env, Converter.None.class);
        if (adapterTypeMirror instanceof DeclaredType) {
            return new AdapterType((DeclaredType) adapterTypeMirror, context);
        }
    }
    if (context.isHonorJaxb()) {
        XmlJavaTypeAdapter typeAdapterInfo = referer != null ? referer.getAnnotation(XmlJavaTypeAdapter.class) : null;
        if (adaptedType instanceof DeclaredType) {
            if (typeAdapterInfo == null) {
                typeAdapterInfo = ((DeclaredType) adaptedType).asElement().getAnnotation(XmlJavaTypeAdapter.class);
            }
        }
        if (typeAdapterInfo != null) {
            final XmlJavaTypeAdapter finalInfo = typeAdapterInfo;
            DecoratedTypeMirror adapterTypeMirror = Annotations.mirrorOf(new Callable<Class<?>>() {

                @Override
                public Class<?> call() throws Exception {
                    return finalInfo.value();
                }
            }, env);
            if (adapterTypeMirror instanceof DecoratedDeclaredType) {
                AdapterType adapterType = new AdapterType((DecoratedDeclaredType) adapterTypeMirror, context);
                if (!context.getContext().getProcessingEnvironment().getTypeUtils().isSameType(adapterType.getAdaptingType(), adaptedType)) {
                    return adapterType;
                }
            }
        }
    }
    return null;
}
Also used : DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) AdapterType(com.webcohesion.enunciate.modules.jackson.model.adapters.AdapterType) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) DecoratedProcessingEnvironment(com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment) EnunciateException(com.webcohesion.enunciate.EnunciateException) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) TypeMirror(javax.lang.model.type.TypeMirror) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) Converter(com.fasterxml.jackson.databind.util.Converter) DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType) DeclaredType(javax.lang.model.type.DeclaredType)

Example 3 with AdapterType

use of com.webcohesion.enunciate.modules.jackson.model.adapters.AdapterType in project enunciate by stoicflame.

the class MapType method findMapTypeDeclaration.

public static DeclaredType findMapTypeDeclaration(TypeMirror typeMirror, EnunciateJacksonContext context) {
    if (!(typeMirror instanceof DeclaredType)) {
        return null;
    }
    DeclaredType declaredType = (DeclaredType) typeMirror;
    TypeElement element = (TypeElement) declaredType.asElement();
    String fqn = element.getQualifiedName().toString();
    if (Map.class.getName().equals(fqn)) {
        return declaredType;
    }
    AdapterType adapterType = JacksonUtil.findAdapterType(element, context);
    if (adapterType != null) {
        return findMapTypeDeclaration(adapterType.getAdaptingType(), context);
    }
    DeclaredType mapType = null;
    Types typeUtils = context.getContext().getProcessingEnvironment().getTypeUtils();
    List<? extends TypeMirror> supers = typeUtils.directSupertypes(declaredType);
    for (TypeMirror superInterface : supers) {
        mapType = findMapTypeDeclaration(superInterface, context);
        if (mapType != null) {
            break;
        }
    }
    return mapType;
}
Also used : Types(javax.lang.model.util.Types) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AdapterType(com.webcohesion.enunciate.modules.jackson.model.adapters.AdapterType) HashMap(java.util.HashMap) Map(java.util.Map) DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType) DeclaredType(javax.lang.model.type.DeclaredType)

Example 4 with AdapterType

use of com.webcohesion.enunciate.modules.jackson.model.adapters.AdapterType in project enunciate by stoicflame.

the class TypeNameForMethod method convert.

@Override
public String convert(TypeElement declaration) throws TemplateModelException {
    String fqn = declaration.getQualifiedName().toString();
    if (classConversions.containsKey(fqn)) {
        return classConversions.get(fqn);
    } else if (declaration.getKind() == ElementKind.ENUM) {
        return "string";
    } else if (isCollection(declaration) || isStream(declaration) || isMap(declaration)) {
        return "array";
    }
    if (this.jacksonContext != null) {
        AdapterType adapterType = JacksonUtil.findAdapterType(declaration, this.jacksonContext);
        if (adapterType != null) {
            return convert(adapterType.getAdaptingType());
        }
    }
    if (this.jackson1Context != null) {
        com.webcohesion.enunciate.modules.jackson1.model.adapters.AdapterType adapter1Type = com.webcohesion.enunciate.modules.jackson1.model.util.JacksonUtil.findAdapterType(declaration, this.jackson1Context);
        if (adapter1Type != null) {
            return convert(adapter1Type.getAdaptingType());
        }
    }
    String convertedPackage = convertPackage(this.context.getProcessingEnvironment().getElementUtils().getPackageOf(declaration));
    ClientName specifiedName = declaration.getAnnotation(ClientName.class);
    String simpleName = specifiedName == null ? declaration.getSimpleName().toString() : specifiedName.value();
    return convertedPackage + getPackageSeparator() + simpleName;
}
Also used : ClientName(com.webcohesion.enunciate.metadata.ClientName) AdapterType(com.webcohesion.enunciate.modules.jackson.model.adapters.AdapterType)

Example 5 with AdapterType

use of com.webcohesion.enunciate.modules.jackson.model.adapters.AdapterType in project enunciate by stoicflame.

the class ClientClassnameForMethod method convert.

@Override
public String convert(TypeElement declaration) throws TemplateModelException {
    String fqn = declaration.getQualifiedName().toString();
    if (classConversions.containsKey(fqn)) {
        return classConversions.get(fqn);
    } else if (declaration.getKind() == ElementKind.ENUM) {
        return "String";
    } else if (isCollection(declaration) || isStream(declaration) || isMap(declaration)) {
        return "Array";
    }
    if (this.jacksonContext != null) {
        AdapterType adapterType = JacksonUtil.findAdapterType(declaration, this.jacksonContext);
        if (adapterType != null) {
            return convert(adapterType.getAdaptingType());
        }
    }
    if (this.jackson1Context != null) {
        com.webcohesion.enunciate.modules.jackson1.model.adapters.AdapterType adapter1Type = com.webcohesion.enunciate.modules.jackson1.model.util.JacksonUtil.findAdapterType(declaration, this.jackson1Context);
        if (adapter1Type != null) {
            return convert(adapter1Type.getAdaptingType());
        }
    }
    String convertedPackage = convertPackage(this.context.getProcessingEnvironment().getElementUtils().getPackageOf(declaration));
    ClientName specifiedName = declaration.getAnnotation(ClientName.class);
    String simpleName = specifiedName == null ? declaration.getSimpleName().toString() : specifiedName.value();
    return convertedPackage + getPackageSeparator() + simpleName;
}
Also used : ClientName(com.webcohesion.enunciate.metadata.ClientName) AdapterType(com.webcohesion.enunciate.modules.jackson.model.adapters.AdapterType)

Aggregations

AdapterType (com.webcohesion.enunciate.modules.jackson.model.adapters.AdapterType)8 ClientName (com.webcohesion.enunciate.metadata.ClientName)5 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)2 DecoratedProcessingEnvironment (com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment)2 DecoratedDeclaredType (com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType)2 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)2 TypeElement (javax.lang.model.element.TypeElement)2 DeclaredType (javax.lang.model.type.DeclaredType)2 TypeMirror (javax.lang.model.type.TypeMirror)2 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)1 Converter (com.fasterxml.jackson.databind.util.Converter)1 EnunciateException (com.webcohesion.enunciate.EnunciateException)1 TypeHint (com.webcohesion.enunciate.metadata.rs.TypeHint)1 EnunciateJacksonContext (com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext)1 TypeDefinition (com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)1 MapType (com.webcohesion.enunciate.modules.jackson.model.util.MapType)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Element (javax.lang.model.element.Element)1 Types (javax.lang.model.util.Types)1