Search in sources :

Example 1 with AdapterType

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

the class XmlTypeVisitor method visitDeclared.

@Override
public XmlType visitDeclared(DeclaredType declaredType, Context context) {
    Element declaredElement = declaredType.asElement();
    String fqn = declaredElement instanceof TypeElement ? ((TypeElement) declaredElement).getQualifiedName().toString() : declaredType.toString();
    if (context.getStack().contains(fqn)) {
        // break the recursion.
        return KnownXmlType.ANY_TYPE;
    }
    context.getStack().push(fqn);
    try {
        AdapterType adapterType = JAXBUtil.findAdapterType(declaredElement, context.getContext());
        if (adapterType != null) {
            adapterType.getAdaptingType().accept(this, context);
        } else {
            MapType mapType = MapType.findMapType(declaredType, context.getContext());
            if (mapType != null) {
                XmlType keyType = mapType.getKeyType().accept(this, new Context(context.getContext(), false, false, context.stack));
                XmlType valueType = mapType.getValueType().accept(this, new Context(context.getContext(), false, false, context.stack));
                return new MapXmlType(keyType, valueType);
            } else {
                switch(declaredElement.getKind()) {
                    case ENUM:
                    case CLASS:
                        XmlType knownType = context.getContext().getKnownType(declaredElement);
                        if (knownType != null) {
                            return knownType;
                        } else {
                            // type not known, not specified.  Last chance: look for the type definition.
                            TypeDefinition typeDefinition = context.getContext().findTypeDefinition(declaredElement);
                            if (typeDefinition != null) {
                                return new XmlClassType(typeDefinition);
                            }
                        }
                        break;
                }
            }
        }
        return KnownXmlType.ANY_TYPE;
    } finally {
        context.getStack().pop();
    }
}
Also used : EnunciateJaxbContext(com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) AdapterType(com.webcohesion.enunciate.modules.jaxb.model.adapters.AdapterType) MapType(com.webcohesion.enunciate.modules.jaxb.model.util.MapType) TypeDefinition(com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)

Example 2 with AdapterType

use of com.webcohesion.enunciate.modules.jaxb.model.adapters.AdapterType 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 3 with AdapterType

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

the class ClientClassnameForMethod method convert.

@Override
public String convert(TypeElement declaration) throws TemplateModelException {
    AdapterType adapterType = JAXBUtil.findAdapterType(declaration, this.jaxbContext);
    if (adapterType != null) {
        return convert(adapterType.getAdaptingType());
    }
    if (declaration.getKind() == ElementKind.CLASS) {
        DecoratedTypeMirror superType = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(declaration.getSuperclass(), this.context.getProcessingEnvironment());
        if (superType != null && superType.isInstanceOf(JAXBElement.class.getName())) {
            // for client conversions, we're going to generalize subclasses of JAXBElement to JAXBElement
            return convert(superType);
        }
    }
    if (declaration.getKind() == ElementKind.INTERFACE && eraseInterfaces) {
        return "java.lang.Object";
    }
    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) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) AdapterType(com.webcohesion.enunciate.modules.jaxb.model.adapters.AdapterType)

Example 4 with AdapterType

use of com.webcohesion.enunciate.modules.jaxb.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) || isMap(declaration)) {
        return "array";
    }
    AdapterType adapterType = JAXBUtil.findAdapterType(declaration, this.jaxbContext);
    if (adapterType != null) {
        return convert(adapterType.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.jaxb.model.adapters.AdapterType)

Example 5 with AdapterType

use of com.webcohesion.enunciate.modules.jaxb.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 (isCollection(declaration)) {
        return "global::System.Collections.ArrayList";
    }
    AdapterType adapterType = JAXBUtil.findAdapterType(declaration, this.jaxbContext);
    if (adapterType != null) {
        return convert(adapterType.getAdaptingType());
    }
    if (declaration.getKind() == ElementKind.CLASS) {
        DecoratedTypeMirror superType = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(declaration.getSuperclass(), this.context.getProcessingEnvironment());
        if (superType != null && superType.isInstanceOf(JAXBElement.class.getName())) {
            // for client conversions, we're going to generalize subclasses of JAXBElement to JAXBElement
            return convert(superType);
        }
    }
    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) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) AdapterType(com.webcohesion.enunciate.modules.jaxb.model.adapters.AdapterType)

Aggregations

AdapterType (com.webcohesion.enunciate.modules.jaxb.model.adapters.AdapterType)9 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)6 ClientName (com.webcohesion.enunciate.metadata.ClientName)6 TypeElement (javax.lang.model.element.TypeElement)3 DecoratedDeclaredType (com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType)2 DeclaredType (javax.lang.model.type.DeclaredType)2 TypeMirror (javax.lang.model.type.TypeMirror)2 EnunciateException (com.webcohesion.enunciate.EnunciateException)1 DecoratedProcessingEnvironment (com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment)1 EnunciateJaxbContext (com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext)1 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)1 MapType (com.webcohesion.enunciate.modules.jaxb.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 XmlJavaTypeAdapter (javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)1