Example usage for com.fasterxml.jackson.databind.introspect AnnotatedField getAnnotated

List of usage examples for com.fasterxml.jackson.databind.introspect AnnotatedField getAnnotated

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.introspect AnnotatedField getAnnotated.

Prototype

public Field getAnnotated() 

Source Link

Usage

From source file:org.gvnix.web.json.ConversionServiceBeanSerializerModifier.java

@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc,
        List<BeanPropertyWriter> beanProperties) {

    // We need the BeanPropertyDefinition to get the related Field
    List<BeanPropertyDefinition> properties = beanDesc.findProperties();
    Map<String, BeanPropertyDefinition> propertyDefMap = new HashMap<String, BeanPropertyDefinition>();
    for (BeanPropertyDefinition property : properties) {
        propertyDefMap.put(property.getName(), property);
    }//w  w  w .java2  s. c o  m

    // iterate over bean's properties to configure serializers
    for (int i = 0; i < beanProperties.size(); i++) {
        BeanPropertyWriter beanPropertyWriter = beanProperties.get(i);
        Class<?> propertyType = beanPropertyWriter.getPropertyType();

        if (beanPropertyWriter.hasSerializer()) {
            continue;
        }

        // For conversion between collection, array, and map types,
        // ConversionService.canConvert() method will return 'true'
        // but better to delegate in default Jackson property writer for
        // right start and ends markers serialization and iteration
        if (propertyType.isArray() || Collection.class.isAssignableFrom(propertyType)
                || Map.class.isAssignableFrom(propertyType)) {

            // Don't set ConversionService serializer, let Jackson
            // use default Collection serializer
            continue;
        } else if (BindingResult.class.isAssignableFrom(propertyType)) {
            // Use BindingResultSerializer
            beanPropertyWriter.assignSerializer(bindingResultSerializer);
        } else {

            // ConversionService uses value Class plus related Field
            // annotations to be able to select the right converter,
            // so we must get/ the Field annotations for success
            // formatting
            BeanPropertyDefinition propertyDef = propertyDefMap.get(beanPropertyWriter.getName());
            AnnotatedField annotatedField = propertyDef.getField();
            if (annotatedField == null) {
                continue;
            }
            AnnotatedElement annotatedEl = annotatedField.getAnnotated();

            // Field contains info about Annotations, info that
            // ConversionService uses for success formatting, use it if
            // available. Otherwise use the class of given value.
            TypeDescriptor sourceType = annotatedEl != null ? new TypeDescriptor((Field) annotatedEl)
                    : TypeDescriptor.valueOf(propertyType);

            TypeDescriptor targetType = TypeDescriptor.valueOf(String.class);
            if (beanPropertyWriter.getSerializationType() != null) {
                targetType = TypeDescriptor.valueOf(beanPropertyWriter.getSerializationType().getRawClass());
            }
            if (ObjectUtils.equals(sourceType, targetType)) {
                // No conversion needed
                continue;
            } else if (sourceType.getObjectType() == Object.class && targetType.getObjectType() == String.class
                    && beanPropertyWriter.getSerializationType() == null) {
                // Can't determine source type and no target type has been
                // configure. Delegate on jackson.
                continue;
            }

            // All other converters must be set in ConversionService
            if (this.conversionService.canConvert(sourceType, targetType)) {

                // We must create BeanPropertyWriter own Serializer that
                // has knowledge about the Field related to that
                // BeanPropertyWriter in order to have access to
                // Field Annotations for success serialization
                JsonSerializer<Object> jsonSerializer = new ConversionServicePropertySerializer(
                        this.conversionService, sourceType, targetType);

                beanPropertyWriter.assignSerializer(jsonSerializer);
            }
            // If no converter set, use default Jackson property writer
            else {
                continue;
            }
        }
    }
    return beanProperties;
}

From source file:fr.norad.jaxrs.doc.parser.ModelJacksonParser.java

@Override
public List<PropertyAccessor> findProperties(Class<?> modelClass) {
    List<PropertyAccessor> properties = new ArrayList<>();

    BasicBeanDescription beanDesc = fakeSerializer.getDescription(modelClass);
    List<BeanPropertyDefinition> findProperties = beanDesc.findProperties();
    for (BeanPropertyDefinition beanPropertyDefinition : findProperties) {
        if (modelClass.isEnum() && "declaringClass".equals(beanPropertyDefinition.getName())) {
            continue;
        }// ww w . j  av  a2  s.  c o  m
        AnnotatedMethod getterJackson = beanPropertyDefinition.getGetter();
        AnnotatedMethod setterJackson = beanPropertyDefinition.getSetter();
        AnnotatedField fieldJackson = null;
        try {
            fieldJackson = beanPropertyDefinition.getField();
        } catch (Exception e) {
            log.warning("Name conflict on fields in bean : " + beanPropertyDefinition + " during doc generation"
                    + e.getMessage());
        }

        Method getter = getterJackson == null ? null : getterJackson.getAnnotated();
        Method setter = setterJackson == null ? null : setterJackson.getAnnotated();
        Field field = fieldJackson == null ? null : fieldJackson.getAnnotated();
        if (getter == null && setter == null && field == null) {
            log.warning(
                    "Cannot find valid property element for : " + beanPropertyDefinition + " on " + modelClass);
            continue;
        }

        PropertyAccessor property = new PropertyAccessor();
        property.setField(field);
        property.setGetter(getter);
        property.setSetter(setter);
        property.setName(beanPropertyDefinition.getName());

        properties.add(property);
    }
    return properties;
}