Search in sources :

Example 1 with DecoratedTypeMirror

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

the class TypeHintUtils method getTypeHint.

public static TypeMirror getTypeHint(TypeHint hintInfo, DecoratedProcessingEnvironment env, TypeMirror defaultValue) {
    TypeMirror typeMirror;
    try {
        Class hint = hintInfo.value();
        if (TypeHint.NO_CONTENT.class.equals(hint)) {
            typeMirror = env.getTypeUtils().getNoType(TypeKind.VOID);
        } else {
            String hintName = hint.getName();
            if (TypeHint.NONE.class.equals(hint)) {
                hintName = hintInfo.qualifiedName();
            }
            if (!"##NONE".equals(hintName)) {
                TypeElement type = env.getElementUtils().getTypeElement(hintName);
                typeMirror = TypeMirrorDecorator.decorate(env.getTypeUtils().getDeclaredType(type), env);
            } else {
                typeMirror = defaultValue;
            }
        }
    } catch (MirroredTypeException e) {
        DecoratedTypeMirror decorated = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(e.getTypeMirror(), env);
        if (decorated.isInstanceOf(TypeHint.NO_CONTENT.class)) {
            typeMirror = env.getTypeUtils().getNoType(TypeKind.VOID);
        } else if (decorated instanceof DeclaredType) {
            String hintName = ((TypeElement) ((DeclaredType) decorated).asElement()).getQualifiedName().toString();
            if (decorated.isInstanceOf(TypeHint.NONE.class)) {
                hintName = hintInfo.qualifiedName();
            }
            if (!"##NONE".equals(hintName)) {
                TypeElement type = env.getElementUtils().getTypeElement(hintName);
                if (type != null) {
                    typeMirror = TypeMirrorDecorator.decorate(env.getTypeUtils().getDeclaredType(type), env);
                } else {
                    env.getMessager().printMessage(Diagnostic.Kind.WARNING, "Unable to find element " + hintName);
                    typeMirror = null;
                }
            } else {
                typeMirror = defaultValue;
            }
        } else {
            typeMirror = decorated;
        }
    }
    return typeMirror;
}
Also used : MirroredTypeException(javax.lang.model.type.MirroredTypeException) TypeHint(com.webcohesion.enunciate.metadata.rs.TypeHint) 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) DeclaredType(javax.lang.model.type.DeclaredType)

Example 2 with DecoratedTypeMirror

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

the class EnunciateJaxrsContext method add.

/**
 * Add a root resource to the model.
 *
 * @param rootResource The root resource to add to the model.
 */
public void add(RootResource rootResource) {
    if (rootResource.isInterface()) {
        // if the root resource is an interface, don't add it if its implementation has already been added (avoid duplication).
        for (RootResource resource : this.rootResources) {
            if (((DecoratedTypeMirror) (resource.asType())).isInstanceOf(rootResource)) {
                debug("%s was identified as a JAX-RS root resource, but will be ignored because root resource %s implements it.", rootResource.getQualifiedName(), resource.getQualifiedName());
                return;
            }
        }
    } else {
        // remove any interfaces of this root resource that have been identified as root resources (avoid duplication)
        DecoratedTypeMirror rootResourceType = (DecoratedTypeMirror) rootResource.asType();
        Iterator<RootResource> it = this.rootResources.iterator();
        while (it.hasNext()) {
            RootResource resource = it.next();
            if (resource.isInterface() && rootResourceType.isInstanceOf(resource)) {
                debug("%s was identified as a JAX-RS root resource, but will be ignored because root resource %s implements it.", resource.getQualifiedName(), rootResource.getQualifiedName());
                it.remove();
            }
        }
    }
    this.rootResources.add(rootResource);
    debug("Added %s as a JAX-RS root resource.", rootResource.getQualifiedName());
    if (getContext().getProcessingEnvironment().findSourcePosition(rootResource) == null) {
        OneTimeLogMessage.SOURCE_FILES_NOT_FOUND.log(getContext());
        if (OneTimeLogMessage.SOURCE_FILES_NOT_FOUND.getLogged() <= 3) {
            info("Unable to find source file for %s.", rootResource.getQualifiedName());
        } else {
            debug("Unable to find source file for %s.", rootResource.getQualifiedName());
        }
    }
}
Also used : RootResource(com.webcohesion.enunciate.modules.jaxrs.model.RootResource) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

Example 3 with DecoratedTypeMirror

use of com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror 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 4 with DecoratedTypeMirror

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

the class JAXBUtil method getAdaptersOfPackage.

/**
 * Gets the adapters of the specified package.
 *
 * @param pckg the package for which to get the adapters.
 * @param context The context.
 * @return The adapters for the package.
 */
private static Map<String, XmlJavaTypeAdapter> getAdaptersOfPackage(PackageElement pckg, EnunciateJaxbContext context) {
    if (pckg == null) {
        return null;
    }
    Map<String, Map<String, XmlJavaTypeAdapter>> adaptersOfAllPackages = (Map<String, Map<String, XmlJavaTypeAdapter>>) context.getContext().getProperty(ADAPTERS_BY_PACKAGE_PROPERTY);
    if (adaptersOfAllPackages == null) {
        adaptersOfAllPackages = new HashMap<String, Map<String, XmlJavaTypeAdapter>>();
        context.getContext().setProperty(ADAPTERS_BY_PACKAGE_PROPERTY, adaptersOfAllPackages);
    }
    Map<String, XmlJavaTypeAdapter> adaptersOfPackage = adaptersOfAllPackages.get(pckg.getQualifiedName().toString());
    if (adaptersOfPackage == null) {
        adaptersOfPackage = new HashMap<String, XmlJavaTypeAdapter>();
        adaptersOfAllPackages.put(pckg.getQualifiedName().toString(), adaptersOfPackage);
        XmlJavaTypeAdapter javaType = pckg.getAnnotation(XmlJavaTypeAdapter.class);
        XmlJavaTypeAdapters javaTypes = pckg.getAnnotation(XmlJavaTypeAdapters.class);
        if ((javaType != null) || (javaTypes != null)) {
            ArrayList<XmlJavaTypeAdapter> allAdaptedTypes = new ArrayList<XmlJavaTypeAdapter>();
            if (javaType != null) {
                allAdaptedTypes.add(javaType);
            }
            if (javaTypes != null) {
                allAdaptedTypes.addAll(Arrays.asList(javaTypes.value()));
            }
            for (final XmlJavaTypeAdapter adaptedTypeInfo : allAdaptedTypes) {
                DecoratedTypeMirror typeMirror = Annotations.mirrorOf(new Callable<Class<?>>() {

                    @Override
                    public Class<?> call() throws Exception {
                        return adaptedTypeInfo.type();
                    }
                }, context.getContext().getProcessingEnvironment(), XmlJavaTypeAdapter.DEFAULT.class);
                if (typeMirror == null) {
                    throw new EnunciateException("Package " + pckg.getQualifiedName() + ": a type must be specified in " + XmlJavaTypeAdapter.class.getName() + " at the package-level.");
                }
                if (!(typeMirror instanceof DeclaredType)) {
                    throw new EnunciateException("Package " + pckg.getQualifiedName() + ": unadaptable type: " + typeMirror);
                }
                TypeElement typeDeclaration = (TypeElement) ((DeclaredType) typeMirror).asElement();
                if (typeDeclaration == null) {
                    throw new EnunciateException("Element not found: " + typeMirror);
                }
                adaptersOfPackage.put(typeDeclaration.getQualifiedName().toString(), adaptedTypeInfo);
            }
        }
    }
    return adaptersOfPackage;
}
Also used : DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeElement(javax.lang.model.element.TypeElement) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) EnunciateException(com.webcohesion.enunciate.EnunciateException) EnunciateException(com.webcohesion.enunciate.EnunciateException) XmlJavaTypeAdapters(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters) DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType) DeclaredType(javax.lang.model.type.DeclaredType)

Example 5 with DecoratedTypeMirror

use of com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror 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)

Aggregations

DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)79 DecoratedDeclaredType (com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType)24 TypeElement (javax.lang.model.element.TypeElement)24 TypeMirror (javax.lang.model.type.TypeMirror)17 DeclaredType (javax.lang.model.type.DeclaredType)16 Element (javax.lang.model.element.Element)10 EnunciateException (com.webcohesion.enunciate.EnunciateException)9 DecoratedProcessingEnvironment (com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment)7 ClientName (com.webcohesion.enunciate.metadata.ClientName)7 MediaTypeDescriptor (com.webcohesion.enunciate.api.resources.MediaTypeDescriptor)6 DecoratedTypeElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedTypeElement)6 TypeHint (com.webcohesion.enunciate.metadata.rs.TypeHint)6 DecoratedElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedElement)5 AdapterType (com.webcohesion.enunciate.modules.jaxb.model.adapters.AdapterType)5 ArrayList (java.util.ArrayList)5 MirroredTypeException (javax.lang.model.type.MirroredTypeException)5 XmlJavaTypeAdapter (javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)5 JavaDoc (com.webcohesion.enunciate.javac.javadoc.JavaDoc)4 MapType (com.webcohesion.enunciate.modules.jackson.model.util.MapType)4 Types (javax.lang.model.util.Types)4