Search in sources :

Example 1 with TypeHint

use of com.webcohesion.enunciate.metadata.rs.TypeHint 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 TypeHint

use of com.webcohesion.enunciate.metadata.rs.TypeHint 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.getStack()));
                JsonType valueType = mapType.getValueType().accept(this, new Context(context.getContext(), false, false, context.getStack()));
                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 : EnunciateJackson1Context(com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context) 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.jackson1.model.adapters.AdapterType) JsonSerializer(org.codehaus.jackson.map.JsonSerializer) DecoratedProcessingEnvironment(com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment) MapType(com.webcohesion.enunciate.modules.jackson1.model.util.MapType) TypeDefinition(com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition) JsonSerialize(org.codehaus.jackson.map.annotate.JsonSerialize) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

Example 3 with TypeHint

use of com.webcohesion.enunciate.metadata.rs.TypeHint in project enunciate by stoicflame.

the class JsonTypeFactory method findSpecifiedType.

/**
 * Find the specified type of the given adaptable element, if it exists.
 *
 * @param adaptable The adaptable element for which to find the specified type.
 * @param context The context
 * @return The specified JSON type, or null if it doesn't exist.
 */
public static JsonType findSpecifiedType(Adaptable adaptable, EnunciateJacksonContext context) {
    JsonType jsonType = null;
    if (adaptable instanceof Accessor) {
        Accessor accessor = (Accessor) adaptable;
        TypeHint typeHint = accessor.getAnnotation(TypeHint.class);
        if (typeHint != null) {
            TypeMirror hint = TypeHintUtils.getTypeHint(typeHint, context.getContext().getProcessingEnvironment(), null);
            if (hint != null) {
                return getJsonType(hint, context);
            }
        }
        JsonFormat format = accessor.getAnnotation(JsonFormat.class);
        if (format != null) {
            switch(format.shape()) {
                case ARRAY:
                    return KnownJsonType.ARRAY;
                case BOOLEAN:
                    return KnownJsonType.BOOLEAN;
                case NUMBER:
                case NUMBER_FLOAT:
                    return KnownJsonType.NUMBER;
                case NUMBER_INT:
                    return KnownJsonType.WHOLE_NUMBER;
                case OBJECT:
                    return KnownJsonType.OBJECT;
                case STRING:
                case SCALAR:
                    return KnownJsonType.STRING;
                case ANY:
                default:
            }
        }
        final JsonSerialize serializeInfo = accessor.getAnnotation(JsonSerialize.class);
        if (serializeInfo != null) {
            DecoratedProcessingEnvironment env = context.getContext().getProcessingEnvironment();
            DecoratedTypeMirror using = Annotations.mirrorOf(new Callable<Class<?>>() {

                @Override
                public Class<?> call() throws Exception {
                    return serializeInfo.using();
                }
            }, env, JsonSerializer.None.class);
            if (using != null) {
                // we're using some custom serialization, so we just have to return a generic object.
                return KnownJsonType.OBJECT;
            } else {
                DecoratedTypeMirror as = Annotations.mirrorOf(new Callable<Class<?>>() {

                    @Override
                    public Class<?> call() throws Exception {
                        return serializeInfo.as();
                    }
                }, env, Void.class);
                if (as != null) {
                    return getJsonType(as, context);
                } else {
                    DecoratedTypeMirror contentAs = Annotations.mirrorOf(new Callable<Class<?>>() {

                        @Override
                        public Class<?> call() throws Exception {
                            return serializeInfo.contentAs();
                        }
                    }, env, Void.class);
                    DecoratedTypeMirror contentUsing = Annotations.mirrorOf(new Callable<Class<?>>() {

                        @Override
                        public Class<?> call() throws Exception {
                            return serializeInfo.contentUsing();
                        }
                    }, env, JsonSerializer.None.class);
                    DecoratedTypeMirror accessorType = (DecoratedTypeMirror) accessor.asType();
                    if (accessorType.isCollection() || accessorType.isArray() || accessorType.isStream()) {
                        if (contentUsing != null) {
                            // the json type has to be just a list of object.
                            return new JsonArrayType(KnownJsonType.OBJECT);
                        } else if (contentAs != null) {
                            return new JsonArrayType(getJsonType(contentAs, context));
                        }
                    } else {
                        MapType mapType = MapType.findMapType(accessorType, context);
                        if (mapType != null) {
                            DecoratedTypeMirror keyAs = Annotations.mirrorOf(new Callable<Class<?>>() {

                                @Override
                                public Class<?> call() throws Exception {
                                    return serializeInfo.keyAs();
                                }
                            }, env, Void.class);
                            DecoratedTypeMirror keyUsing = Annotations.mirrorOf(new Callable<Class<?>>() {

                                @Override
                                public Class<?> call() throws Exception {
                                    return serializeInfo.keyUsing();
                                }
                            }, env, JsonSerializer.None.class);
                            if (keyAs != null || contentAs != null) {
                                JsonType keyType = keyUsing == null ? getJsonType(keyAs == null ? (DecoratedTypeMirror) mapType.getKeyType() : keyAs, context) : KnownJsonType.OBJECT;
                                JsonType valueType = contentUsing == null ? getJsonType(contentAs == null ? (DecoratedTypeMirror) mapType.getValueType() : contentAs, context) : KnownJsonType.OBJECT;
                                return new JsonMapType(keyType, valueType);
                            }
                        }
                    }
                }
            }
        }
    }
    if (adaptable.isAdapted()) {
        jsonType = getJsonType(adaptable.getAdapterType().getAdaptingType(), context);
    }
    return jsonType;
}
Also used : TypeHint(com.webcohesion.enunciate.metadata.rs.TypeHint) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) Accessor(com.webcohesion.enunciate.modules.jackson.model.Accessor) DecoratedProcessingEnvironment(com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment) MapType(com.webcohesion.enunciate.modules.jackson.model.util.MapType) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) JsonFormat(com.fasterxml.jackson.annotation.JsonFormat) TypeMirror(javax.lang.model.type.TypeMirror) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

Example 4 with TypeHint

use of com.webcohesion.enunciate.metadata.rs.TypeHint 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 5 with TypeHint

use of com.webcohesion.enunciate.metadata.rs.TypeHint in project enunciate by stoicflame.

the class EnunciateJackson1Context method add.

public void add(TypeDefinition typeDef, LinkedList<Element> stack) {
    if (findTypeDefinition(typeDef) == null && !isKnownType(typeDef)) {
        this.typeDefinitions.put(typeDef.getQualifiedName().toString(), typeDef);
        if (this.context.isExcluded(typeDef)) {
            warn("Added %s as a Jackson type definition even though is was supposed to be excluded according to configuration. It was referenced from %s%s, so it had to be included to prevent broken references.", typeDef.getQualifiedName(), stack.size() > 0 ? stack.get(0) : "an unknown location", stack.size() > 1 ? " of " + stack.get(1) : "");
        } else {
            debug("Added %s as a Jackson type definition.", typeDef.getQualifiedName());
        }
        if (getContext().getProcessingEnvironment().findSourcePosition(typeDef) == null) {
            OneTimeLogMessage.SOURCE_FILES_NOT_FOUND.log(getContext());
            if (OneTimeLogMessage.SOURCE_FILES_NOT_FOUND.getLogged() <= 3) {
                info("Unable to find source file for %s.", typeDef.getQualifiedName());
            } else {
                debug("Unable to find source file for %s.", typeDef.getQualifiedName());
            }
        }
        typeDef.getReferencedFrom().addAll(stack);
        try {
            stack.push(typeDef);
            addSeeAlsoTypeDefinitions(typeDef, stack);
            for (Member member : typeDef.getMembers()) {
                TypeHint hintInfo = member.getAnnotation(TypeHint.class);
                if (hintInfo != null) {
                    TypeMirror hint = TypeHintUtils.getTypeHint(hintInfo, context.getProcessingEnvironment(), null);
                    if (hint != null) {
                        addReferencedTypeDefinitions(hint, stack);
                    }
                } else {
                    addReferencedTypeDefinitions(member, stack);
                }
            }
            Value value = typeDef.getValue();
            if (value != null) {
                addReferencedTypeDefinitions(value, stack);
            }
            TypeMirror superclass = typeDef.getSuperclass();
            if (!typeDef.isBaseObject() && superclass != null && superclass.getKind() != TypeKind.NONE && !isCollapseTypeHierarchy()) {
                addReferencedTypeDefinitions(superclass, stack);
            }
        } finally {
            stack.pop();
        }
    }
}
Also used : TypeHint(com.webcohesion.enunciate.metadata.rs.TypeHint) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

Aggregations

TypeHint (com.webcohesion.enunciate.metadata.rs.TypeHint)8 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)7 DecoratedProcessingEnvironment (com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment)4 TypeElement (javax.lang.model.element.TypeElement)4 Element (javax.lang.model.element.Element)3 TypeMirror (javax.lang.model.type.TypeMirror)3 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)2 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)2 MapType (com.webcohesion.enunciate.modules.jackson.model.util.MapType)2 MapType (com.webcohesion.enunciate.modules.jackson1.model.util.MapType)2 JsonSerializer (org.codehaus.jackson.map.JsonSerializer)2 JsonSerialize (org.codehaus.jackson.map.annotate.JsonSerialize)2 JsonFormat (com.fasterxml.jackson.annotation.JsonFormat)1 Container (com.sun.jersey.samples.storageservice.Container)1 PropertyElement (com.webcohesion.enunciate.javac.decorations.element.PropertyElement)1 EnunciateJacksonContext (com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext)1 InterfaceJacksonTypeElement (com.webcohesion.enunciate.modules.jackson.javac.InterfaceJacksonTypeElement)1 Accessor (com.webcohesion.enunciate.modules.jackson.model.Accessor)1 TypeDefinition (com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)1 AdapterType (com.webcohesion.enunciate.modules.jackson.model.adapters.AdapterType)1