Search in sources :

Example 1 with AnnotatedMethod

use of com.fasterxml.jackson.databind.introspect.AnnotatedMethod 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 AnnotatedMethod

use of com.fasterxml.jackson.databind.introspect.AnnotatedMethod 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 AnnotatedMethod

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

the class DeprecatedConstructType1456Test method testGenericParameterViaClass.

// and this is how new code should resolve types if at all possible
public void testGenericParameterViaClass() throws Exception {
    BeanDescription desc = MAPPER.getDeserializationConfig().introspect(MAPPER.constructType(ImplController.class));
    AnnotatedClass ac = desc.getClassInfo();
    AnnotatedMethod m = ac.findMethod("process", new Class<?>[] { BaseEntity.class });
    assertNotNull(m);
    assertEquals(1, m.getParameterCount());
    AnnotatedParameter param = m.getParameter(0);
    assertEquals(ImplEntity.class, param.getType().getRawClass());
}
Also used : AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) AnnotatedParameter(com.fasterxml.jackson.databind.introspect.AnnotatedParameter)

Example 4 with AnnotatedMethod

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

the class TestAccessorGeneration method testLotsaIntAccessorGeneration.

// And then test to ensure Switch-table construction also works...
public void testLotsaIntAccessorGeneration() throws Exception {
    PropertyAccessorCollector coll = new PropertyAccessorCollector(BeanN.class);
    String[] methodNames = new String[] { "getX", "getY", "get3", "get4", "get5", "get6", "get7" };
    for (String methodName : methodNames) {
        Method method = BeanN.class.getDeclaredMethod(methodName);
        AnnotatedMethod annMethod = new AnnotatedMethod(null, method, null, null);
        coll.addIntGetter(new BeanPropertyWriter(SimpleBeanPropertyDefinition.construct(null, annMethod, new PropertyName(methodName)), annMethod, null, null, null, null, null, false, null));
    }
    BeanPropertyAccessor acc = coll.findAccessor(null);
    BeanN bean = new BeanN();
    assertEquals(bean.getX(), acc.intGetter(bean, 0));
    assertEquals(bean.getY(), acc.intGetter(bean, 1));
    assertEquals(bean.get3(), acc.intGetter(bean, 2));
    assertEquals(bean.get4(), acc.intGetter(bean, 3));
    assertEquals(bean.get5(), acc.intGetter(bean, 4));
    assertEquals(bean.get6(), acc.intGetter(bean, 5));
    assertEquals(bean.get7(), acc.intGetter(bean, 6));
}
Also used : PropertyName(com.fasterxml.jackson.databind.PropertyName) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) Method(java.lang.reflect.Method) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter)

Example 5 with AnnotatedMethod

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

the class SettableAnyProperty method set.

@SuppressWarnings("unchecked")
public void set(Object instance, Object propName, Object value) throws IOException {
    try {
        // if annotation in the field (only map is supported now)
        if (_setterIsField) {
            AnnotatedField field = (AnnotatedField) _setter;
            Map<Object, Object> val = (Map<Object, Object>) field.getValue(instance);
            /* 01-Jun-2016, tatu: At this point it is not quite clear what to do if
                 *    field is `null` -- we can not necessarily count on zero-args
                 *    constructor except for a small set of types, so for now just
                 *    ignore if null. May need to figure out something better in future.
                 */
            if (val != null) {
                // add the property key and value
                val.put(propName, value);
            }
        } else {
            // note: can not use 'setValue()' due to taking 2 args
            ((AnnotatedMethod) _setter).callOnWith(instance, propName, value);
        }
    } catch (Exception e) {
        _throwAsIOE(e, propName, value);
    }
}
Also used : AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) AnnotatedField(com.fasterxml.jackson.databind.introspect.AnnotatedField) Map(java.util.Map) IOException(java.io.IOException)

Aggregations

AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)9 Method (java.lang.reflect.Method)5 PropertyName (com.fasterxml.jackson.databind.PropertyName)3 AnnotatedField (com.fasterxml.jackson.databind.introspect.AnnotatedField)3 BeanPropertyWriter (com.fasterxml.jackson.databind.ser.BeanPropertyWriter)3 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 AnnotatedClass (com.fasterxml.jackson.databind.introspect.AnnotatedClass)2 AnnotatedParameter (com.fasterxml.jackson.databind.introspect.AnnotatedParameter)2 FormParameter (io.swagger.models.parameters.FormParameter)2 HeaderParameter (io.swagger.models.parameters.HeaderParameter)2 Parameter (io.swagger.models.parameters.Parameter)2 PathParameter (io.swagger.models.parameters.PathParameter)2 QueryParameter (io.swagger.models.parameters.QueryParameter)2 ArrayList (java.util.ArrayList)2 AnnotationIntrospector (com.fasterxml.jackson.databind.AnnotationIntrospector)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 Annotated (com.fasterxml.jackson.databind.introspect.Annotated)1 AnnotatedMember (com.fasterxml.jackson.databind.introspect.AnnotatedMember)1 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)1