Search in sources :

Example 1 with Accessor

use of com.webcohesion.enunciate.modules.jackson1.model.Accessor in project enunciate by stoicflame.

the class Jackson1Module method getDefaultVisibility.

public AccessorVisibilityChecker getDefaultVisibility() {
    List<HierarchicalConfiguration> visibilityElements = this.config.configurationsAt("accessor-visibility");
    AccessorVisibilityChecker checker = AccessorVisibilityChecker.DEFAULT_CHECKER;
    for (HierarchicalConfiguration visibilityElement : visibilityElements) {
        JsonMethod method = JsonMethod.valueOf(visibilityElement.getString("[@type]", "").toUpperCase());
        JsonAutoDetect.Visibility visibility = JsonAutoDetect.Visibility.valueOf(visibilityElement.getString("[@visibility]", "").toUpperCase());
        checker = checker.withVisibility(method, visibility);
    }
    return checker;
}
Also used : JsonAutoDetect(org.codehaus.jackson.annotate.JsonAutoDetect) AccessorVisibilityChecker(com.webcohesion.enunciate.modules.jackson1.model.AccessorVisibilityChecker) JsonMethod(org.codehaus.jackson.annotate.JsonMethod) HierarchicalConfiguration(org.apache.commons.configuration.HierarchicalConfiguration)

Example 2 with Accessor

use of com.webcohesion.enunciate.modules.jackson1.model.Accessor in project enunciate by stoicflame.

the class Accessor method getAccessorType.

/**
 * The type of the accessor.
 *
 * @return The type of the accessor.
 */
public DecoratedTypeMirror getAccessorType() {
    DecoratedTypeMirror accessorType = (DecoratedTypeMirror) asType();
    accessorType = OptionalUtils.stripOptional(accessorType, this.context.getContext().getProcessingEnvironment());
    accessorType = this.context.resolveSyntheticType(accessorType);
    DecoratedDeclaredType normalizedCollection = JacksonUtil.getNormalizedCollection(accessorType, this.context.getContext().getProcessingEnvironment());
    if (normalizedCollection != null) {
        accessorType = normalizedCollection;
    } else {
        MapType mapType = MapType.findMapType(accessorType, this.context);
        if (mapType != null) {
            accessorType = mapType;
        }
    }
    return accessorType;
}
Also used : DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) MapType(com.webcohesion.enunciate.modules.jackson1.model.util.MapType)

Example 3 with Accessor

use of com.webcohesion.enunciate.modules.jackson1.model.Accessor 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, EnunciateJackson1Context 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);
            }
        }
        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(org.codehaus.jackson.map.JsonSerializer) Accessor(com.webcohesion.enunciate.modules.jackson1.model.Accessor) DecoratedProcessingEnvironment(com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment) MapType(com.webcohesion.enunciate.modules.jackson1.model.util.MapType) JsonSerialize(org.codehaus.jackson.map.annotate.JsonSerialize) TypeMirror(javax.lang.model.type.TypeMirror) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

Example 4 with Accessor

use of com.webcohesion.enunciate.modules.jackson1.model.Accessor in project enunciate by stoicflame.

the class Jackson1CodeErrors method findConflictingAccessorNamingErrors.

public static List<String> findConflictingAccessorNamingErrors(EnunciateJackson1Context 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.jackson1.model.Accessor) TypeDefinition(com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition)

Example 5 with Accessor

use of com.webcohesion.enunciate.modules.jackson1.model.Accessor in project enunciate by stoicflame.

the class Member method getAccessorType.

/**
 * The type of an element accessor can be specified by an annotation.
 *
 * @return The accessor type.
 */
@Override
public DecoratedTypeMirror getAccessorType() {
    if (this.explicitType != null) {
        return this.explicitType;
    }
    JsonType specifiedJsonType = JsonTypeFactory.findSpecifiedType(this, this.context);
    DecoratedTypeMirror specifiedType = specifiedJsonType instanceof JsonClassType ? (DecoratedTypeMirror) ((JsonClassType) specifiedJsonType).getTypeDefinition().asType() : null;
    if (specifiedType != null) {
        return specifiedType;
    }
    return super.getAccessorType();
}
Also used : JsonType(com.webcohesion.enunciate.modules.jackson1.model.types.JsonType) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) JsonClassType(com.webcohesion.enunciate.modules.jackson1.model.types.JsonClassType)

Aggregations

DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)3 Accessor (com.webcohesion.enunciate.modules.jackson1.model.Accessor)2 MapType (com.webcohesion.enunciate.modules.jackson1.model.util.MapType)2 DecoratedProcessingEnvironment (com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment)1 DecoratedDeclaredType (com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType)1 TypeHint (com.webcohesion.enunciate.metadata.rs.TypeHint)1 AccessorVisibilityChecker (com.webcohesion.enunciate.modules.jackson1.model.AccessorVisibilityChecker)1 TypeDefinition (com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition)1 JsonClassType (com.webcohesion.enunciate.modules.jackson1.model.types.JsonClassType)1 JsonType (com.webcohesion.enunciate.modules.jackson1.model.types.JsonType)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 TypeMirror (javax.lang.model.type.TypeMirror)1 HierarchicalConfiguration (org.apache.commons.configuration.HierarchicalConfiguration)1 JsonAutoDetect (org.codehaus.jackson.annotate.JsonAutoDetect)1 JsonMethod (org.codehaus.jackson.annotate.JsonMethod)1 JsonSerializer (org.codehaus.jackson.map.JsonSerializer)1 JsonSerialize (org.codehaus.jackson.map.annotate.JsonSerialize)1