Search in sources :

Example 1 with AnnotatedField

use of com.fasterxml.jackson.databind.introspect.AnnotatedField in project jersey by jersey.

the class FilteringJacksonJaxbJsonProvider method _configForWriting.

@Override
protected JsonEndpointConfig _configForWriting(final ObjectMapper mapper, final Annotation[] annotations, final Class<?> defaultView) {
    final AnnotationIntrospector customIntrospector = mapper.getSerializationConfig().getAnnotationIntrospector();
    // Set the custom (user) introspector to be the primary one.
    final ObjectMapper filteringMapper = mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(customIntrospector, new JacksonAnnotationIntrospector() {

        @Override
        public Object findFilterId(final Annotated a) {
            final Object filterId = super.findFilterId(a);
            if (filterId != null) {
                return filterId;
            }
            if (a instanceof AnnotatedMethod) {
                final Method method = ((AnnotatedMethod) a).getAnnotated();
                // Interested only in getters - trying to obtain "field" name from them.
                if (ReflectionHelper.isGetter(method)) {
                    return ReflectionHelper.getPropertyName(method);
                }
            }
            if (a instanceof AnnotatedField || a instanceof AnnotatedClass) {
                return a.getName();
            }
            return null;
        }
    }));
    return super._configForWriting(filteringMapper, annotations, defaultView);
}
Also used : JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) Annotated(com.fasterxml.jackson.databind.introspect.Annotated) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) AnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) Method(java.lang.reflect.Method) AnnotatedField(com.fasterxml.jackson.databind.introspect.AnnotatedField) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with AnnotatedField

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

the class DefaultParameterExtension method handleAdditionalAnnotation.

/**
     * Adds additional annotation processing support 
     * 
     * @param parameters
     * @param annotation
     * @param type
     * @param typesToSkip
     */
private void handleAdditionalAnnotation(List<Parameter> parameters, Annotation annotation, final Type type, Set<Type> typesToSkip) {
    if (CLASS_BEAN_PARAM != null && CLASS_BEAN_PARAM.isAssignableFrom(annotation.getClass())) {
        // Use Jackson's logic for processing Beans
        final BeanDescription beanDesc = mapper.getSerializationConfig().introspect(constructType(type));
        final List<BeanPropertyDefinition> properties = beanDesc.findProperties();
        for (final BeanPropertyDefinition propDef : properties) {
            final AnnotatedField field = propDef.getField();
            final AnnotatedMethod setter = propDef.getSetter();
            final AnnotatedMethod getter = propDef.getGetter();
            final List<Annotation> paramAnnotations = new ArrayList<Annotation>();
            final Iterator<SwaggerExtension> extensions = SwaggerExtensions.chain();
            Type paramType = null;
            // Gather the field's details
            if (field != null) {
                paramType = field.getRawType();
                for (final Annotation fieldAnnotation : field.annotations()) {
                    if (!paramAnnotations.contains(fieldAnnotation)) {
                        paramAnnotations.add(fieldAnnotation);
                    }
                }
            }
            // Gather the setter's details but only the ones we need
            if (setter != null) {
                // Do not set the param class/type from the setter if the values are already identified
                if (paramType == null) {
                    paramType = setter.getRawParameterTypes() != null ? setter.getRawParameterTypes()[0] : null;
                }
                for (final Annotation fieldAnnotation : setter.annotations()) {
                    if (!paramAnnotations.contains(fieldAnnotation)) {
                        paramAnnotations.add(fieldAnnotation);
                    }
                }
            }
            // Gather the getter's details but only the ones we need
            if (getter != null) {
                // Do not set the param class/type from the getter if the values are already identified
                if (paramType == null) {
                    paramType = getter.getRawReturnType();
                }
                for (final Annotation fieldAnnotation : getter.annotations()) {
                    if (!paramAnnotations.contains(fieldAnnotation)) {
                        paramAnnotations.add(fieldAnnotation);
                    }
                }
            }
            if (paramType == null) {
                continue;
            }
            // Re-process all Bean fields and let the default swagger-jaxrs/swagger-jersey-jaxrs processors do their thing
            List<Parameter> extracted = extensions.next().extractParameters(paramAnnotations, paramType, typesToSkip, extensions);
            // since downstream processors won't know how to introspect @BeanParam, process here
            for (Parameter param : extracted) {
                if (ParameterProcessor.applyAnnotations(null, param, paramType, paramAnnotations) != null) {
                    parameters.add(param);
                }
            }
        }
    }
}
Also used : AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) Type(java.lang.reflect.Type) AbstractSwaggerExtension(io.swagger.jaxrs.ext.AbstractSwaggerExtension) SwaggerExtension(io.swagger.jaxrs.ext.SwaggerExtension) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) HeaderParameter(io.swagger.models.parameters.HeaderParameter) CookieParameter(io.swagger.models.parameters.CookieParameter) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) AnnotatedField(com.fasterxml.jackson.databind.introspect.AnnotatedField)

Example 3 with AnnotatedField

use of com.fasterxml.jackson.databind.introspect.AnnotatedField in project crnk-framework by crnk-project.

the class JacksonResourceFieldInformationProvider method getName.

protected Optional<String> getName(Field field) {
    ObjectMapper objectMapper = context.getObjectMapper();
    SerializationConfig serializationConfig = objectMapper.getSerializationConfig();
    if (serializationConfig != null && serializationConfig.getPropertyNamingStrategy() != null) {
        AnnotationMap annotationMap = buildAnnotationMap(field.getDeclaredAnnotations());
        AnnotatedClass annotatedClass = AnnotatedClassBuilder.build(field.getDeclaringClass(), serializationConfig);
        AnnotatedField annotatedField = AnnotatedFieldBuilder.build(annotatedClass, field, annotationMap);
        return Optional.of(serializationConfig.getPropertyNamingStrategy().nameForField(serializationConfig, annotatedField, field.getName()));
    }
    return Optional.empty();
}
Also used : AnnotationMap(com.fasterxml.jackson.databind.introspect.AnnotationMap) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) AnnotatedField(com.fasterxml.jackson.databind.introspect.AnnotatedField) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with AnnotatedField

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

the class PropertyAccessorCollector method _addSingleField.

private void _addSingleField(MethodVisitor mv, OptimizedBeanPropertyWriter<?> prop, int returnOpcode) {
    // load second arg (index)
    mv.visitVarInsn(ILOAD, 2);
    // load local for cast bean
    mv.visitVarInsn(ALOAD, 3);
    AnnotatedField field = (AnnotatedField) prop.getMember();
    mv.visitFieldInsn(GETFIELD, beanClassName, field.getName(), Type.getDescriptor(field.getRawType()));
    mv.visitInsn(returnOpcode);
}
Also used : AnnotatedField(com.fasterxml.jackson.databind.introspect.AnnotatedField)

Example 5 with AnnotatedField

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

the class PropertyMutatorCollector method _addFieldsUsingSwitch.

private <T extends OptimizedSettableBeanProperty<T>> void _addFieldsUsingSwitch(MethodVisitor mv, List<T> props, int loadValueCode, int beanIndex, boolean mustCast) {
    // load second arg (index)
    mv.visitVarInsn(ILOAD, 2);
    Label[] labels = new Label[props.size()];
    for (int i = 0, len = labels.length; i < len; ++i) {
        labels[i] = new Label();
    }
    Label defaultLabel = new Label();
    mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);
    for (int i = 0, len = labels.length; i < len; ++i) {
        mv.visitLabel(labels[i]);
        // load bean
        mv.visitVarInsn(ALOAD, beanIndex);
        // put 'value' to stack
        mv.visitVarInsn(loadValueCode, 3);
        AnnotatedField field = (AnnotatedField) props.get(i).getMember();
        Type type = Type.getType(field.getRawType());
        if (mustCast) {
            mv.visitTypeInsn(CHECKCAST, type.getInternalName());
        }
        mv.visitFieldInsn(PUTFIELD, beanClassName, field.getName(), type.getDescriptor());
        mv.visitInsn(RETURN);
    }
    mv.visitLabel(defaultLabel);
}
Also used : AnnotatedField(com.fasterxml.jackson.databind.introspect.AnnotatedField)

Aggregations

AnnotatedField (com.fasterxml.jackson.databind.introspect.AnnotatedField)14 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)6 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)3 AnnotatedClass (com.fasterxml.jackson.databind.introspect.AnnotatedClass)3 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)3 Annotation (java.lang.annotation.Annotation)3 Type (java.lang.reflect.Type)3 ArrayList (java.util.ArrayList)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 AnnotationMap (com.fasterxml.jackson.databind.introspect.AnnotationMap)2 Parameter (io.swagger.models.parameters.Parameter)2 AnnotationIntrospector (com.fasterxml.jackson.databind.AnnotationIntrospector)1 PropertyName (com.fasterxml.jackson.databind.PropertyName)1 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)1 Annotated (com.fasterxml.jackson.databind.introspect.Annotated)1 JacksonAnnotationIntrospector (com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector)1 AbstractSwaggerExtension (io.swagger.jaxrs.ext.AbstractSwaggerExtension)1 SwaggerExtension (io.swagger.jaxrs.ext.SwaggerExtension)1 AbstractSerializableParameter (io.swagger.models.parameters.AbstractSerializableParameter)1 CookieParameter (io.swagger.models.parameters.CookieParameter)1