Search in sources :

Example 56 with JavaType

use of com.fasterxml.jackson.databind.JavaType in project jackson-databind by FasterXML.

the class AnnotatedClass method _resolveClassAnnotations.

/*
    /**********************************************************
    /* Public API, main-level resolution methods
    /**********************************************************
     */
/**
     * Initialization method that will recursively collect Jackson
     * annotations for this class and all super classes and
     * interfaces.
     */
private AnnotationMap _resolveClassAnnotations() {
    AnnotationMap ca = new AnnotationMap();
    // Should skip processing if annotation processing disabled
    if (_annotationIntrospector != null) {
        // add mix-in annotations first (overrides)
        if (_primaryMixIn != null) {
            _addClassMixIns(ca, _class, _primaryMixIn);
        }
        // first, annotations from the class itself:
        _addAnnotationsIfNotPresent(ca, ClassUtil.findClassAnnotations(_class));
        // and then from super types
        for (JavaType type : _superTypes) {
            // and mix mix-in annotations in-between
            _addClassMixIns(ca, type);
            _addAnnotationsIfNotPresent(ca, ClassUtil.findClassAnnotations(type.getRawClass()));
        }
        /* and finally... any annotations there might be for plain
             * old Object.class: separate because for all other purposes
             * it is just ignored (not included in super types)
             */
        /* 12-Jul-2009, tatu: Should this be done for interfaces too?
             *   For now, yes, seems useful for some cases, and not harmful for any?
             */
        _addClassMixIns(ca, Object.class);
    }
    return ca;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType)

Example 57 with JavaType

use of com.fasterxml.jackson.databind.JavaType in project jackson-databind by FasterXML.

the class AnnotatedClass method resolveMemberMethods.

/**
     * Method for resolving member method information: aggregating all non-static methods
     * and combining annotations (to implement method-annotation inheritance)
     * 
     * @param methodFilter Filter used to determine which methods to include
     */
private void resolveMemberMethods() {
    _memberMethods = new AnnotatedMethodMap();
    AnnotatedMethodMap mixins = new AnnotatedMethodMap();
    // first: methods from the class itself
    _addMemberMethods(_class, this, _memberMethods, _primaryMixIn, mixins);
    // and then augment these with annotations from super-types:
    for (JavaType type : _superTypes) {
        Class<?> mixin = (_mixInResolver == null) ? null : _mixInResolver.findMixInClassFor(type.getRawClass());
        _addMemberMethods(type.getRawClass(), new TypeResolutionContext.Basic(_typeFactory, type.getBindings()), _memberMethods, mixin, mixins);
    }
    // Special case: mix-ins for Object.class? (to apply to ALL classes)
    if (_mixInResolver != null) {
        Class<?> mixin = _mixInResolver.findMixInClassFor(Object.class);
        if (mixin != null) {
            _addMethodMixIns(_class, _memberMethods, mixin, mixins);
        }
    }
    // 14-Feb-2011, tatu: AnnotationIntrospector is null if annotations not enabled; if so, can skip:
    if (_annotationIntrospector != null) {
        if (!mixins.isEmpty()) {
            Iterator<AnnotatedMethod> it = mixins.iterator();
            while (it.hasNext()) {
                AnnotatedMethod mixIn = it.next();
                try {
                    Method m = Object.class.getDeclaredMethod(mixIn.getName(), mixIn.getRawParameterTypes());
                    if (m != null) {
                        // Since it's from java.lang.Object, no generics, no need for real type context:
                        AnnotatedMethod am = _constructMethod(m, this);
                        _addMixOvers(mixIn.getAnnotated(), am, false);
                        _memberMethods.add(am);
                    }
                } catch (Exception e) {
                }
            }
        }
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType)

Example 58 with JavaType

use of com.fasterxml.jackson.databind.JavaType in project jackson-databind by FasterXML.

the class TypeDeserializerBase method _findDeserializer.

/*
    /**********************************************************
    /* Helper methods for sub-classes
    /**********************************************************
     */
protected final JsonDeserializer<Object> _findDeserializer(DeserializationContext ctxt, String typeId) throws IOException {
    JsonDeserializer<Object> deser = _deserializers.get(typeId);
    if (deser == null) {
        /* As per [Databind#305], need to provide contextual info. But for
             * backwards compatibility, let's start by only supporting this
             * for base class, not via interface. Later on we can add this
             * to the interface, assuming deprecation at base class helps.
             */
        JavaType type = _idResolver.typeFromId(ctxt, typeId);
        if (type == null) {
            // As per [JACKSON-614], use the default impl if no type id available:
            deser = _findDefaultImplDeserializer(ctxt);
            if (deser == null) {
                // 10-May-2016, tatu: We may get some help...
                JavaType actual = _handleUnknownTypeId(ctxt, typeId);
                if (actual == null) {
                    // TODO: try to figure out something better
                    return null;
                }
                // ... would this actually work?
                deser = ctxt.findContextualValueDeserializer(actual, _property);
            }
        } else {
            /* 16-Dec-2010, tatu: Since nominal type we get here has no (generic) type parameters,
                 *   we actually now need to explicitly narrow from base type (which may have parameterization)
                 *   using raw type.
                 *
                 *   One complication, though; can not change 'type class' (simple type to container); otherwise
                 *   we may try to narrow a SimpleType (Object.class) into MapType (Map.class), losing actual
                 *   type in process (getting SimpleType of Map.class which will not work as expected)
                 */
            if ((_baseType != null) && _baseType.getClass() == type.getClass()) {
                //  now it should improve things.
                if (!type.hasGenericTypes()) {
                    type = ctxt.getTypeFactory().constructSpecializedType(_baseType, type.getRawClass());
                }
            }
            deser = ctxt.findContextualValueDeserializer(type, _property);
        }
        _deserializers.put(typeId, deser);
    }
    return deser;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType)

Example 59 with JavaType

use of com.fasterxml.jackson.databind.JavaType in project jackson-module-afterburner by FasterXML.

the class TestJvmDeserPerf method testDeser.

protected int testDeser(ObjectMapper mapper, byte[] input, int reps) throws Exception {
    JavaType type = TypeFactory.defaultInstance().constructType(MediaItem.class);
    MediaItem item = null;
    for (int i = 0; i < reps; ++i) {
        item = mapper.readValue(input, 0, input.length, type);
    }
    // just to get some non-optimizable number
    return item.hashCode();
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType)

Example 60 with JavaType

use of com.fasterxml.jackson.databind.JavaType in project swagger-core by swagger-api.

the class ModelResolver method resolveProperty.

public Property resolveProperty(JavaType propType, ModelConverterContext context, Annotation[] annotations, Iterator<ModelConverter> next) {
    LOGGER.debug("resolveProperty {}", propType);
    Property property = null;
    if (propType.isContainerType()) {
        JavaType keyType = propType.getKeyType();
        JavaType valueType = propType.getContentType();
        if (keyType != null && valueType != null) {
            property = new MapProperty().additionalProperties(context.resolveProperty(valueType, new Annotation[] {}));
        } else if (valueType != null) {
            Property items = context.resolveProperty(valueType, new Annotation[] {});
            // If property is XmlElement annotated, then use the name provided by annotation | https://github.com/swagger-api/swagger-core/issues/2047
            if (annotations != null && annotations.length > 0) {
                for (Annotation annotation : annotations) {
                    if (annotation instanceof XmlElement) {
                        XmlElement xmlElement = (XmlElement) annotation;
                        if (xmlElement != null && xmlElement.name() != null && !"".equals(xmlElement.name()) && !"##default".equals(xmlElement.name())) {
                            Xml xml = items.getXml() != null ? items.getXml() : new Xml();
                            xml.setName(xmlElement.name());
                            items.setXml(xml);
                        }
                    }
                }
            }
            ArrayProperty arrayProperty = new ArrayProperty().items(items);
            if (_isSetType(propType.getRawClass())) {
                arrayProperty.setUniqueItems(true);
            }
            property = arrayProperty;
        }
    } else {
        property = PrimitiveType.createProperty(propType);
    }
    if (property == null) {
        if (propType.isEnumType()) {
            property = new StringProperty();
            _addEnumProps(propType.getRawClass(), property);
        } else if (_isOptionalType(propType)) {
            property = context.resolveProperty(propType.containedType(0), null);
        } else {
            // complex type
            Model innerModel = context.resolve(propType);
            if (innerModel instanceof ComposedModel) {
                innerModel = ((ComposedModel) innerModel).getChild();
            }
            if (innerModel instanceof ModelImpl) {
                ModelImpl mi = (ModelImpl) innerModel;
                property = new RefProperty(StringUtils.isNotEmpty(mi.getReference()) ? mi.getReference() : mi.getName());
            }
        }
    }
    return property;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ArrayProperty(io.swagger.models.properties.ArrayProperty) Xml(io.swagger.models.Xml) ComposedModel(io.swagger.models.ComposedModel) MapProperty(io.swagger.models.properties.MapProperty) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ApiModel(io.swagger.annotations.ApiModel) XmlElement(javax.xml.bind.annotation.XmlElement) StringProperty(io.swagger.models.properties.StringProperty) ModelImpl(io.swagger.models.ModelImpl) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) UUIDProperty(io.swagger.models.properties.UUIDProperty) ApiModelProperty(io.swagger.annotations.ApiModelProperty) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) RefProperty(io.swagger.models.properties.RefProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) Annotation(java.lang.annotation.Annotation) RefProperty(io.swagger.models.properties.RefProperty)

Aggregations

JavaType (com.fasterxml.jackson.databind.JavaType)110 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)10 IOException (java.io.IOException)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)7 ArrayList (java.util.ArrayList)7 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)6 Property (io.swagger.models.properties.Property)6 Test (org.junit.Test)6 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)5 Method (java.lang.reflect.Method)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 StringProperty (io.swagger.models.properties.StringProperty)4 List (java.util.List)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)3 CollectionType (com.fasterxml.jackson.databind.type.CollectionType)3 MapType (com.fasterxml.jackson.databind.type.MapType)3 ClassConfig (io.servicecomb.common.javassist.ClassConfig)3 ModelImpl (io.swagger.models.ModelImpl)3