Search in sources :

Example 1 with Accessor

use of com.webcohesion.enunciate.modules.jackson.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, 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 2 with Accessor

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

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

the class IsAccessorOfTypeLongMethod method exec.

/**
 * Returns the qname of the element that has the first parameter as the namespace, the second as the element.
 *
 * @param list The arguments.
 * @return The qname.
 */
public Object exec(List list) throws TemplateModelException {
    if (list.size() < 1) {
        throw new TemplateModelException("The isDefinedGlobally method must have a local element declaration as a parameter.");
    }
    TemplateModel from = (TemplateModel) list.get(0);
    Object unwrapped = DeepUnwrap.unwrap(from);
    if (unwrapped instanceof Accessor) {
        Accessor accessor = (Accessor) unwrapped;
        DecoratedTypeMirror decorated = accessor.getAccessorType();
        if (decorated.isPrimitive()) {
            return decorated.getKind() == TypeKind.LONG;
        } else if (decorated.isInstanceOf(Long.class.getName())) {
            return true;
        } else if (decorated.isInstanceOf(Date.class.getName())) {
            return true;
        } else if (decorated.isInstanceOf(Calendar.class.getName())) {
            return true;
        }
    } else if (unwrapped instanceof com.webcohesion.enunciate.modules.jackson1.model.Accessor) {
        com.webcohesion.enunciate.modules.jackson1.model.Accessor accessor = (com.webcohesion.enunciate.modules.jackson1.model.Accessor) unwrapped;
        DecoratedTypeMirror decorated = accessor.getAccessorType();
        if (decorated.isPrimitive()) {
            return decorated.getKind() == TypeKind.LONG;
        } else if (decorated.isInstanceOf(Long.class.getName())) {
            return true;
        } else if (decorated.isInstanceOf(Date.class.getName())) {
            return true;
        } else if (decorated.isInstanceOf(Calendar.class.getName())) {
            return true;
        }
    } else {
        throw new TemplateModelException("The IsAccessorOfTypeLong method must have an accessor as a parameter.");
    }
    return false;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) Calendar(java.util.Calendar) TemplateModel(freemarker.template.TemplateModel) Accessor(com.webcohesion.enunciate.modules.jackson.model.Accessor) Date(java.util.Date)

Aggregations

Accessor (com.webcohesion.enunciate.modules.jackson.model.Accessor)3 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)2 JsonFormat (com.fasterxml.jackson.annotation.JsonFormat)1 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 TypeDefinition (com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)1 MapType (com.webcohesion.enunciate.modules.jackson.model.util.MapType)1 TemplateModel (freemarker.template.TemplateModel)1 TemplateModelException (freemarker.template.TemplateModelException)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 TypeMirror (javax.lang.model.type.TypeMirror)1