Search in sources :

Example 1 with DecoratedDeclaredType

use of com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType in project enunciate by stoicflame.

the class JAXBUtil method findAdapterType.

protected static AdapterType findAdapterType(DecoratedTypeMirror maybeContainedAdaptedType, Element referer, PackageElement pckg, EnunciateJaxbContext context) {
    DecoratedProcessingEnvironment env = context.getContext().getProcessingEnvironment();
    TypeMirror adaptedType = TypeMirrorUtils.getComponentType(maybeContainedAdaptedType, env);
    adaptedType = adaptedType == null ? maybeContainedAdaptedType : adaptedType;
    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) && (pckg != null)) {
            TypeElement typeDeclaration = (TypeElement) ((DeclaredType) adaptedType).asElement();
            typeAdapterInfo = getAdaptersOfPackage(pckg, context).get(typeDeclaration.getQualifiedName().toString());
        }
    }
    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.getContext());
            if (!context.getContext().getProcessingEnvironment().getTypeUtils().isSameType(adapterType.getAdaptingType(), adaptedType)) {
                return adapterType;
            }
        }
    }
    return null;
}
Also used : DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType) TypeMirror(javax.lang.model.type.TypeMirror) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeElement(javax.lang.model.element.TypeElement) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) AdapterType(com.webcohesion.enunciate.modules.jaxb.model.adapters.AdapterType) DecoratedProcessingEnvironment(com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment) EnunciateException(com.webcohesion.enunciate.EnunciateException) DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType) DeclaredType(javax.lang.model.type.DeclaredType)

Example 2 with DecoratedDeclaredType

use of com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType in project enunciate by stoicflame.

the class JAXBUtil method getNormalizedCollection.

public static DecoratedDeclaredType getNormalizedCollection(DecoratedTypeMirror typeMirror, DecoratedProcessingEnvironment env) {
    DecoratedDeclaredType base = typeMirror.isList() ? TypeMirrorUtils.listType(env) : typeMirror.isCollection() ? TypeMirrorUtils.collectionType(env) : null;
    if (base != null) {
        // now narrow the component type to what can be valid xml.
        DecoratedTypeMirror componentType = findCollectionComponentType((DeclaredType) typeMirror, env);
        base = (DecoratedDeclaredType) env.getTypeUtils().getDeclaredType((TypeElement) base.asElement(), componentType);
    }
    return base;
}
Also used : DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

Example 3 with DecoratedDeclaredType

use of com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType in project enunciate by stoicflame.

the class ResourceMethod method loadReturnType.

protected DecoratedTypeMirror loadReturnType() {
    DecoratedTypeMirror returnType;
    TypeHint hintInfo = getAnnotation(TypeHint.class);
    if (hintInfo != null) {
        returnType = (DecoratedTypeMirror) TypeHintUtils.getTypeHint(hintInfo, this.env, getReturnType());
        returnType.setDeferredDocComment(new ReturnDocComment(this));
    } else {
        returnType = (DecoratedTypeMirror) getReturnType();
        // we can use the type argument to get the entity type
        if (returnType.isClass() && returnType.isInstanceOf("com.sun.jersey.api.JResponse")) {
            DecoratedDeclaredType jresponse = (DecoratedDeclaredType) returnType;
            if (!jresponse.getTypeArguments().isEmpty()) {
                DecoratedTypeMirror responseType = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(jresponse.getTypeArguments().get(0), this.env);
                if (responseType.isDeclared()) {
                    responseType.setDeferredDocComment(new ReturnDocComment(this));
                    returnType = responseType;
                }
            }
        } else if ((returnType.isInstanceOf(javax.ws.rs.core.Response.class) || returnType.isInstanceOf(jakarta.ws.rs.core.Response.class)) || returnType.isInstanceOf(java.io.InputStream.class)) {
            // generic response that doesn't have a type hint; we'll just have to assume return type of "object"
            DecoratedDeclaredType objectType = (DecoratedDeclaredType) TypeMirrorDecorator.decorate(this.env.getElementUtils().getTypeElement(Object.class.getName()).asType(), this.env);
            objectType.setDeferredDocComment(new ReturnDocComment(this));
            returnType = objectType;
        }
    }
    final ApiOperation apiOperation = getAnnotation(ApiOperation.class);
    if (apiOperation != null) {
        DecoratedTypeMirror swaggerReturnType = Annotations.mirrorOf(new Callable<Class<?>>() {

            @Override
            public Class<?> call() throws Exception {
                return apiOperation.response();
            }
        }, this.env, Void.class);
        if (swaggerReturnType != null) {
            if (!apiOperation.responseContainer().isEmpty()) {
                swaggerReturnType = (DecoratedTypeMirror) this.env.getTypeUtils().getArrayType(swaggerReturnType);
                swaggerReturnType.setDeferredDocComment(new ReturnDocComment(this));
            }
            returnType = swaggerReturnType;
        }
    }
    DecoratedTypeMirror returnWrapped = JAXDocletUtil.getReturnWrapped(getDocComment(), this.env, getContext().getContext().getLogger());
    if (returnWrapped != null) {
        returnWrapped.setDeferredDocComment(new ReturnWrappedDocComment(this));
        returnType = returnWrapped;
    }
    return returnType;
}
Also used : DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) ReturnWrappedDocComment(com.webcohesion.enunciate.modules.jaxrs.model.util.ReturnWrappedDocComment) EnunciateException(com.webcohesion.enunciate.EnunciateException) MirroredTypeException(javax.lang.model.type.MirroredTypeException) ReturnDocComment(com.webcohesion.enunciate.javac.javadoc.ReturnDocComment)

Example 4 with DecoratedDeclaredType

use of com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType in project enunciate by stoicflame.

the class ClientClassnameForMethod method convert.

@Override
public String convert(TypeMirror typeMirror) throws TemplateModelException {
    DecoratedTypeMirror decorated = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(typeMirror, this.context.getProcessingEnvironment());
    if (decorated.isArray()) {
        DecoratedTypeMirror componentType = (DecoratedTypeMirror) ((ArrayType) decorated).getComponentType();
        if (componentType.isPrimitive()) {
            switch(componentType.getKind()) {
                case BOOLEAN:
                    return "com.google.gwt.core.client.JsArrayBoolean";
                case BYTE:
                    // byte arrays serialized as base64-encoded strings.
                    return "java.lang.String";
                case CHAR:
                case INT:
                case SHORT:
                    return "com.google.gwt.core.client.JsArrayInteger";
                case DOUBLE:
                case FLOAT:
                case LONG:
                    return "com.google.gwt.core.client.JsArrayNumber";
                default:
                    return "com.google.gwt.core.client.JsArray";
            }
        } else if (componentType.isInstanceOf(String.class.getName())) {
            return "com.google.gwt.core.client.JsArrayString";
        } else {
            return "com.google.gwt.core.client.JsArray<" + convert(componentType) + ">";
        }
    } else if (decorated.isCollection()) {
        List<? extends TypeMirror> typeArgs = ((DecoratedDeclaredType) decorated).getTypeArguments();
        if (typeArgs != null && typeArgs.size() == 1) {
            DecoratedTypeMirror componentType = (DecoratedTypeMirror) typeArgs.iterator().next();
            if (componentType.isInstanceOf(String.class.getName())) {
                return "com.google.gwt.core.client.JsArrayString";
            } else if (componentType.isInstanceOf(Boolean.class.getName())) {
                return "com.google.gwt.core.client.JsArrayBoolean";
            } else if (componentType.isInstanceOf(Integer.class.getName()) || componentType.isInstanceOf(Character.class.getName()) || componentType.isInstanceOf(Short.class.getName())) {
                return "com.google.gwt.core.client.JsArrayInteger";
            } else if (componentType.isInstanceOf(Double.class.getName()) || componentType.isInstanceOf(Float.class.getName()) || componentType.isInstanceOf(Long.class.getName())) {
                return "com.google.gwt.core.client.JsArrayNumber";
            } else {
                return "com.google.gwt.core.client.JsArray<" + convert(componentType) + ">";
            }
        }
        return "com.google.gwt.core.client.JsArray";
    } else if (decorated.isDeclared()) {
        DeclaredType declaredType = ((DeclaredType) decorated);
        String fqn = ((TypeElement) declaredType.asElement()).getQualifiedName().toString();
        if (classConversions.containsKey(fqn)) {
            return classConversions.get(fqn);
        }
    } else if (decorated.isPrimitive()) {
        if (decorated.getKind() == TypeKind.LONG) {
            return Long.class.getName();
        }
    }
    return super.convert(typeMirror);
}
Also used : DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) DeclaredType(javax.lang.model.type.DeclaredType) DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType)

Example 5 with DecoratedDeclaredType

use of com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType in project enunciate by stoicflame.

the class JacksonUtil method getNormalizedCollection.

public static DecoratedDeclaredType getNormalizedCollection(DecoratedTypeMirror typeMirror, DecoratedProcessingEnvironment env) {
    DecoratedDeclaredType base = typeMirror.isList() ? TypeMirrorUtils.listType(env) : typeMirror.isCollection() || typeMirror.isStream() ? TypeMirrorUtils.collectionType(env) : null;
    if (base != null) {
        // now narrow the component type to what can be valid json.
        List<? extends DecoratedTypeMirror> typeArgs = (List<? extends DecoratedTypeMirror>) ((DeclaredType) typeMirror).getTypeArguments();
        if (typeArgs.size() == 1) {
            DecoratedTypeMirror componentType = typeArgs.get(0);
            base = (DecoratedDeclaredType) env.getTypeUtils().getDeclaredType((TypeElement) base.asElement(), componentType);
        }
    }
    return base;
}
Also used : DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

Aggregations

DecoratedDeclaredType (com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType)20 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)19 TypeElement (javax.lang.model.element.TypeElement)8 EnunciateException (com.webcohesion.enunciate.EnunciateException)5 DecoratedElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedElement)5 DeclaredType (javax.lang.model.type.DeclaredType)5 Element (javax.lang.model.element.Element)4 TypeMirror (javax.lang.model.type.TypeMirror)4 DecoratedProcessingEnvironment (com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment)3 JavaDoc (com.webcohesion.enunciate.javac.javadoc.JavaDoc)3 XmlJavaTypeAdapter (javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)3 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)1 Converter (com.fasterxml.jackson.databind.util.Converter)1 DecoratedTypeElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedTypeElement)1 ReturnDocComment (com.webcohesion.enunciate.javac.javadoc.ReturnDocComment)1 AdapterType (com.webcohesion.enunciate.modules.jackson.model.adapters.AdapterType)1 MapType (com.webcohesion.enunciate.modules.jackson.model.util.MapType)1 AdapterType (com.webcohesion.enunciate.modules.jackson1.model.adapters.AdapterType)1 MapType (com.webcohesion.enunciate.modules.jackson1.model.util.MapType)1 AdapterType (com.webcohesion.enunciate.modules.jaxb.model.adapters.AdapterType)1