Search in sources :

Example 1 with TypeDefinition

use of com.webcohesion.enunciate.modules.jackson.model.TypeDefinition 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 TypeDefinition

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

the class JacksonCodeErrors method findConflictingAccessorNamingErrors.

public static List<String> findConflictingAccessorNamingErrors(EnunciateJacksonContext context) {
    List<String> errors = (List<String>) context.getContext().getProperty(CONFLICTING_JAXB_ACCESSOR_NAMING_ERRORS_PROPERTY);
    if (errors == null) {
        errors = new ArrayList<String>();
        context.getContext().setProperty(CONFLICTING_JAXB_ACCESSOR_NAMING_ERRORS_PROPERTY, errors);
        for (TypeDefinition typeDefinition : context.getTypeDefinitions()) {
            Map<String, Accessor> accessorsBySimpleName = new HashMap<String, Accessor>();
            for (Accessor accessor : typeDefinition.getAllAccessors()) {
                String name = accessor.getClientSimpleName();
                Accessor conflict = accessorsBySimpleName.get(name);
                if (conflict != null) {
                    errors.add(String.format("%s: accessor \"%s\" conflicts with accessor \"%s\" of %s: both are named \"%s\".", typeDefinition.getQualifiedName(), accessor, conflict, conflict.getTypeDefinition().getQualifiedName(), name));
                } else {
                    accessorsBySimpleName.put(name, accessor);
                }
            }
        }
    }
    return errors;
}
Also used : HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) Accessor(com.webcohesion.enunciate.modules.jackson.model.Accessor) TypeDefinition(com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)

Example 3 with TypeDefinition

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

the class ObjectDataTypeImpl method gatherInterfaces.

private void gatherInterfaces(TypeElement clazz, Set<DataTypeReference> interfaces) {
    if (clazz == null) {
        return;
    }
    if (clazz.getQualifiedName().contentEquals(Object.class.getName())) {
        return;
    }
    List<? extends TypeMirror> ifaces = clazz.getInterfaces();
    for (TypeMirror iface : ifaces) {
        DecoratedTypeMirror decorated = (DecoratedTypeMirror) iface;
        decorated = this.typeDefinition.getContext().resolveSyntheticType(decorated);
        TypeDefinition typeDefinition = this.typeDefinition.getContext().findTypeDefinition(((DeclaredType) decorated).asElement());
        if (typeDefinition != null) {
            interfaces.add(new DataTypeReferenceImpl(new JsonClassType(typeDefinition), registrationContext));
        }
    }
    TypeMirror superclass = clazz.getSuperclass();
    if (superclass instanceof DeclaredType) {
        gatherInterfaces((TypeElement) ((DeclaredType) superclass).asElement(), interfaces);
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) JsonClassType(com.webcohesion.enunciate.modules.jackson.model.types.JsonClassType) TypeDefinition(com.webcohesion.enunciate.modules.jackson.model.TypeDefinition) ObjectTypeDefinition(com.webcohesion.enunciate.modules.jackson.model.ObjectTypeDefinition) DeclaredType(javax.lang.model.type.DeclaredType)

Example 4 with TypeDefinition

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

the class SyntaxImpl method getTypes.

@Override
public List<? extends DataType> getTypes() {
    Collection<TypeDefinition> typeDefinitions = this.context.getTypeDefinitions();
    ArrayList<DataType> dataTypes = new ArrayList<DataType>();
    FacetFilter facetFilter = this.registrationContext.getFacetFilter();
    for (TypeDefinition typeDefinition : typeDefinitions) {
        if (!facetFilter.accept(typeDefinition)) {
            continue;
        }
        if (typeDefinition instanceof ObjectTypeDefinition) {
            dataTypes.add(new ObjectDataTypeImpl((ObjectTypeDefinition) typeDefinition, registrationContext));
        } else if (typeDefinition instanceof EnumTypeDefinition) {
            dataTypes.add(new EnumDataTypeImpl((EnumTypeDefinition) typeDefinition, registrationContext));
        }
    }
    Collections.sort(dataTypes, new Comparator<DataType>() {

        @Override
        public int compare(DataType o1, DataType o2) {
            return o1.getLabel().compareTo(o2.getLabel());
        }
    });
    return dataTypes;
}
Also used : ObjectTypeDefinition(com.webcohesion.enunciate.modules.jackson.model.ObjectTypeDefinition) FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) EnumTypeDefinition(com.webcohesion.enunciate.modules.jackson.model.EnumTypeDefinition) TypeDefinition(com.webcohesion.enunciate.modules.jackson.model.TypeDefinition) ObjectTypeDefinition(com.webcohesion.enunciate.modules.jackson.model.ObjectTypeDefinition) EnumTypeDefinition(com.webcohesion.enunciate.modules.jackson.model.EnumTypeDefinition)

Example 5 with TypeDefinition

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

the class PHPJSONClientModule method usesUnmappableElements.

protected boolean usesUnmappableElements() {
    boolean usesUnmappableElements = false;
    if (this.jacksonModule != null && this.jacksonModule.getJacksonContext() != null) {
        for (TypeDefinition complexType : this.jacksonModule.getJacksonContext().getTypeDefinitions()) {
            if (!Character.isUpperCase(complexType.getClientSimpleName().charAt(0))) {
                warn("%s: PHP requires your class name to be upper-case. Please rename the class or apply the @org.codehaus.enunciate.ClientName annotation to the class.", positionOf(complexType));
                usesUnmappableElements = true;
            }
        }
    }
    if (this.jackson1Module != null && this.jackson1Module.getJacksonContext() != null) {
        for (com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition complexType : this.jackson1Module.getJacksonContext().getTypeDefinitions()) {
            if (!Character.isUpperCase(complexType.getClientSimpleName().charAt(0))) {
                warn("%s: PHP requires your class name to be upper-case. Please rename the class or apply the @org.codehaus.enunciate.ClientName annotation to the class.", positionOf(complexType));
                usesUnmappableElements = true;
            }
        }
    }
    return usesUnmappableElements;
}
Also used : TypeDefinition(com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)

Aggregations

TypeDefinition (com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)11 FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)6 EnunciateJacksonContext (com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext)6 EnunciateException (com.webcohesion.enunciate.EnunciateException)5 EnunciateJackson1Context (com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context)5 TemplateException (freemarker.template.TemplateException)5 URL (java.net.URL)5 File (java.io.File)4 IOException (java.io.IOException)4 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)3 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)3 DecoratedTypeElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedTypeElement)3 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)2 ObjectTypeDefinition (com.webcohesion.enunciate.modules.jackson.model.ObjectTypeDefinition)2 AntPatternMatcher (com.webcohesion.enunciate.util.AntPatternMatcher)2 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)1 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)1 DecoratedProcessingEnvironment (com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment)1 TypeHint (com.webcohesion.enunciate.metadata.rs.TypeHint)1 Accessor (com.webcohesion.enunciate.modules.jackson.model.Accessor)1