com.fasterxml.jackson.databind.introspect.AnnotatedMember Java Examples

The following examples show how to use com.fasterxml.jackson.databind.introspect.AnnotatedMember. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: lams   Author: lamsfoundation   File: BeanSerializerBase.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Helper method that can be used to see if specified property is annotated
 * to indicate use of a converter for property value (in case of container types,
 * it is container type itself, not key or content type).
 * 
 * @since 2.2
 */
protected JsonSerializer<Object> findConvertingSerializer(SerializerProvider provider,
        BeanPropertyWriter prop)
    throws JsonMappingException
{
    final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
    if (intr != null) {
        AnnotatedMember m = prop.getMember();
        if (m != null) {
            Object convDef = intr.findSerializationConverter(m);
            if (convDef != null) {
                Converter<Object,Object> conv = provider.converterInstance(prop.getMember(), convDef);
                JavaType delegateType = conv.getOutputType(provider.getTypeFactory());
                // [databind#731]: Should skip if nominally java.lang.Object
                JsonSerializer<?> ser = delegateType.isJavaLangObject() ? null
                        : provider.findValueSerializer(delegateType, prop);
                return new StdDelegatingSerializer(conv, delegateType, ser);
            }
        }
    }
    return null;
}
 
Example #2
Source Project: lams   Author: lamsfoundation   File: StdSerializer.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * @deprecated Since 2.9 use {link {@link #findContextualConvertingSerializer} instead
 */
@Deprecated
protected JsonSerializer<?> findConvertingContentSerializer(SerializerProvider provider,
        BeanProperty prop, JsonSerializer<?> existingSerializer)
    throws JsonMappingException
{
    final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
    if (_neitherNull(intr, prop)) {
        AnnotatedMember m = prop.getMember();
        if (m != null) {
            Object convDef = intr.findSerializationContentConverter(m);
            if (convDef != null) {
                Converter<Object,Object> conv = provider.converterInstance(prop.getMember(), convDef);
                JavaType delegateType = conv.getOutputType(provider.getTypeFactory());
                // [databind#731]: Should skip if nominally java.lang.Object
                if ((existingSerializer == null) && !delegateType.isJavaLangObject()) {
                    existingSerializer = provider.findValueSerializer(delegateType);
                }
                return new StdDelegatingSerializer(conv, delegateType, existingSerializer);
            }
        }
    }
    return existingSerializer;
}
 
Example #3
Source Project: lams   Author: lamsfoundation   File: StdSerializer.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convenience method for finding out possibly configured content value serializer.
 *
 * @since 2.7.4
 */
protected JsonSerializer<?> findAnnotatedContentSerializer(SerializerProvider serializers,
        BeanProperty property)
    throws JsonMappingException
{
    if (property != null) {
        // First: if we have a property, may have property-annotation overrides
        AnnotatedMember m = property.getMember();
        final AnnotationIntrospector intr = serializers.getAnnotationIntrospector();
        if (m != null) {
            Object serDef = intr.findContentSerializer(m);
            if (serDef != null) {
                return serializers.serializerInstance(m, serDef);
            }
        }
    }
    return null;
}
 
Example #4
Source Project: lams   Author: lamsfoundation   File: StdDeserializer.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Helper method that can be used to see if specified property has annotation
 * indicating that a converter is to be used for contained values (contents
 * of structured types; array/List/Map values)
 * 
 * @param existingDeserializer (optional) configured content
 *    serializer if one already exists.
 * 
 * @since 2.2
 */
protected JsonDeserializer<?> findConvertingContentDeserializer(DeserializationContext ctxt,
        BeanProperty prop, JsonDeserializer<?> existingDeserializer)
    throws JsonMappingException
{
    final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
    if (_neitherNull(intr, prop)) {
        AnnotatedMember member = prop.getMember();
        if (member != null) {
            Object convDef = intr.findDeserializationContentConverter(member);
            if (convDef != null) {
                Converter<Object,Object> conv = ctxt.converterInstance(prop.getMember(), convDef);
                JavaType delegateType = conv.getInputType(ctxt.getTypeFactory());
                if (existingDeserializer == null) {
                    existingDeserializer = ctxt.findContextualValueDeserializer(delegateType, prop);
                }
                return new StdDelegatingDeserializer<Object>(conv, delegateType, existingDeserializer);
            }
        }
    }
    return existingDeserializer;
}
 
Example #5
Source Project: lams   Author: lamsfoundation   File: EnumResolver.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * @since 2.9
 */
public static EnumResolver constructUsingMethod(Class<Enum<?>> enumCls,
        AnnotatedMember accessor,
        AnnotationIntrospector ai)
{
    Enum<?>[] enumValues = enumCls.getEnumConstants();
    HashMap<String, Enum<?>> map = new HashMap<String, Enum<?>>();
    // from last to first, so that in case of duplicate values, first wins
    for (int i = enumValues.length; --i >= 0; ) {
        Enum<?> en = enumValues[i];
        try {
            Object o = accessor.getValue(en);
            if (o != null) {
                map.put(o.toString(), en);
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("Failed to access @JsonValue of Enum value "+en+": "+e.getMessage());
        }
    }
    Enum<?> defaultEnum = (ai != null) ? ai.findDefaultEnumValue(enumCls) : null;
    return new EnumResolver(enumCls, enumValues, map, defaultEnum);
}
 
Example #6
Source Project: jackson-modules-base   Author: FasterXML   File: PropertyAccessorCollector.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation invocationOperation(
        AnnotatedMember annotatedMember, TypeDefinition beanClassDescription) {

    final String methodName = annotatedMember.getName();
    @SuppressWarnings("unchecked")
    final MethodList<MethodDescription> matchingMethods =
            (MethodList<MethodDescription>) beanClassDescription.getDeclaredMethods().filter(named(methodName));

    if (matchingMethods.size() == 1) { //method was declared on class
        return MethodInvocation.invoke(matchingMethods.getOnly());
    }
    if (matchingMethods.isEmpty()) { //method was not found on class, try super class
        return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
    }
    else { //should never happen
        throw new IllegalStateException("Could not find definition of method: " + methodName);
    }
}
 
Example #7
Source Project: jackson-modules-base   Author: FasterXML   File: PropertyAccessorCollector.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation invocationOperation(
        AnnotatedMember annotatedMember, TypeDefinition beanClassDescription) {

    final String fieldName = annotatedMember.getName();
    @SuppressWarnings("unchecked")
    final FieldList<FieldDescription> matchingFields =
            (FieldList<FieldDescription>) beanClassDescription.getDeclaredFields().filter(named(fieldName));

    if (matchingFields.size() == 1) { //method was declared on class
        return FieldAccess.forField(matchingFields.getOnly()).read();
    }
    if (matchingFields.isEmpty()) { //method was not found on class, try super class
        return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
    }
    else { //should never happen
        throw new IllegalStateException("Could not find definition of field: " + fieldName);
    }
}
 
Example #8
Source Project: jackson-modules-base   Author: FasterXML   File: PropertyMutatorCollector.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor,
                  Implementation.Context implementationContext) {

    final boolean mustCast = (beanValueAccess == MethodVariableAccess.REFERENCE);

    final List<StackManipulation> operations = new ArrayList<StackManipulation>();
    operations.add(loadLocalVar()); // load local for cast bean
    operations.add(loadBeanValueArg());

    final AnnotatedMember member = prop.getMember();
    if (mustCast) {
        operations.add(TypeCasting.to(new ForLoadedType(getClassToCastBeanValueTo(member))));
    }

    operations.add(invocationOperation(member, beanClassDescription));
    operations.add(MethodReturn.VOID);

    final StackManipulation.Compound compound = new StackManipulation.Compound(operations);
    return compound.apply(methodVisitor, implementationContext);
}
 
Example #9
Source Project: jackson-modules-base   Author: FasterXML   File: PropertyMutatorCollector.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected StackManipulation invocationOperation(AnnotatedMember annotatedMember,
        TypeDefinition beanClassDescription) {

    final String fieldName = annotatedMember.getName();
    final FieldList<FieldDescription> matchingFields =
            (FieldList<FieldDescription>) beanClassDescription.getDeclaredFields().filter(named(fieldName));

    if (matchingFields.size() == 1) { //method was declared on class
        return FieldAccess.forField(matchingFields.getOnly()).write();
    }
    if (matchingFields.isEmpty()) { //method was not found on class, try super class
        return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
    }
    else { //should never happen
        throw new IllegalStateException("Could not find definition of field: " + fieldName);
    }
}
 
Example #10
Source Project: jackson-modules-base   Author: FasterXML   File: PropertyMutatorCollector.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation invocationOperation(AnnotatedMember annotatedMember,
        TypeDefinition beanClassDescription) {

    final String methodName = annotatedMember.getName();
    @SuppressWarnings("unchecked")
    final MethodList<MethodDescription> matchingMethods =
            (MethodList<MethodDescription>) beanClassDescription.getDeclaredMethods().filter(named(methodName));

    if (matchingMethods.size() == 1) { //method was declared on class
        return MethodInvocation.invoke(matchingMethods.getOnly());
    }
    if (matchingMethods.isEmpty()) { //method was not found on class, try super class
        return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
    }
    else { //should never happen
        throw new IllegalStateException("Could not find definition of method: " + methodName);
    }

}
 
Example #11
Source Project: spring-data-dev-tools   Author: spring-projects   File: IssueTrackerConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void setupModule(SetupContext context) {

	context.insertAnnotationIntrospector(new NopAnnotationIntrospector() {

		private static final long serialVersionUID = 479313244908256455L;

		@Override
		public boolean hasIgnoreMarker(AnnotatedMember m) {

			if (!(m instanceof AnnotatedMethod)) {
				return super.hasIgnoreMarker(m);
			}

			AnnotatedMethod method = (AnnotatedMethod) m;

			return method.getName().startsWith("lambda$") ? true : super.hasIgnoreMarker(m);
		}
	});
}
 
Example #12
Source Project: netty-rest   Author: buremba   File: SwaggerJacksonAnnotationIntrospector.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Boolean hasRequiredMarker(AnnotatedMember m)
{
    ApiParam apiParam = m.getAnnotation(ApiParam.class);
    if (apiParam != null) {
        return apiParam.required();
    }

    ApiModelProperty ann = m.getAnnotation(ApiModelProperty.class);
    if (ann != null) {
        return ann.required();
    }
    XmlElement elem = m.getAnnotation(XmlElement.class);
    if (elem != null) {
        if (elem.required()) {
            return true;
        }
    }
    return null;
}
 
Example #13
Source Project: dropwizard-guicey   Author: xvik   File: ConfigTreeBuilder.java    License: MIT License 6 votes vote down vote up
private static Object readValue(final AnnotatedMember member, final Object object) {
    if (object == null) {
        return null;
    }
    final Object res;
    final AccessibleObject accessor = (AccessibleObject) member.getMember();
    // case: private field
    if (!accessor.isAccessible()) {
        accessor.setAccessible(true);
        try {
            res = member.getValue(object);
        } finally {
            accessor.setAccessible(false);
        }
    } else {
        // public access (most likely getter)
        res = member.getValue(object);
    }
    return res;
}
 
Example #14
Source Project: spring-boot-starter-data-firebase   Author: fabiomaffioletti   File: FirebaseConfiguration.java    License: MIT License 5 votes vote down vote up
@Bean
public ObjectMapper firebaseObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
        @Override
        public boolean hasIgnoreMarker(AnnotatedMember m) {
            return _findAnnotation(m, FirebaseId.class) != null;
        }
    });
    return objectMapper;
}
 
Example #15
Source Project: quarkus   Author: quarkusio   File: HalEntityWrapperJacksonSerializer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(HalEntityWrapper wrapper, JsonGenerator generator, SerializerProvider serializers)
        throws IOException {

    generator.writeStartObject();
    for (BeanPropertyDefinition property : getPropertyDefinitions(serializers, wrapper.getEntity().getClass())) {
        AnnotatedMember accessor = property.getAccessor();
        if (accessor != null) {
            writeValue(property.getName(), accessor.getValue(wrapper.getEntity()), generator);
        }
    }
    writeLinks(wrapper.getEntity(), generator);
    generator.writeEndObject();
}
 
Example #16
Source Project: aws-xray-sdk-java   Author: aws   File: UnsignedXrayClient.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean hasIgnoreMarker(AnnotatedMember m) {
    // This is a somewhat hacky way of having ObjectMapper only serialize the fields in our
    // model classes instead of the base class that comes from the SDK. In the future, we will
    // remove the SDK dependency itself and the base classes and this hack will go away.
    if (m.getDeclaringClass() == AmazonWebServiceRequest.class ||
        m.getDeclaringClass() == AmazonWebServiceResult.class) {
        return true;
    }
    return super.hasIgnoreMarker(m);
}
 
Example #17
Source Project: lams   Author: lamsfoundation   File: BeanProperty.java    License: GNU General Public License v2.0 5 votes vote down vote up
public Std(PropertyName name, JavaType type, PropertyName wrapperName,
        AnnotatedMember member, PropertyMetadata metadata)
{
    _name = name;
    _type = type;
    _wrapperName = wrapperName;
    _metadata = metadata;
    _member = member;
}
 
Example #18
Source Project: lams   Author: lamsfoundation   File: BeanProperty.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @deprecated Since 2.9
 */
@Deprecated
public Std(PropertyName name, JavaType type, PropertyName wrapperName,
        Annotations contextAnnotations,
        AnnotatedMember member, PropertyMetadata metadata)
{
    this(name, type, wrapperName, member, metadata);
}
 
Example #19
Source Project: lams   Author: lamsfoundation   File: BeanSerializerBuilder.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void setTypeId(AnnotatedMember idProp) {
    // Not legal to use multiple ones...
    if (_typeId != null) {
        throw new IllegalArgumentException("Multiple type ids specified with "+_typeId+" and "+idProp);
    }
    _typeId = idProp;
}
 
Example #20
Source Project: lams   Author: lamsfoundation   File: JsonValueSerializer.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param ser Explicit serializer to use, if caller knows it (which
 *    occurs if and only if the "value method" was annotated with
 *    {@link com.fasterxml.jackson.databind.annotation.JsonSerialize#using}), otherwise
 *    null
 *    
 * @since 2.8 Earlier method took "raw" Method, but that does not work with access
 *    to information we need
 */
@SuppressWarnings("unchecked")
public JsonValueSerializer(AnnotatedMember accessor, JsonSerializer<?> ser)
{
    super(accessor.getType());
    _accessor = accessor;
    _valueSerializer = (JsonSerializer<Object>) ser;
    _property = null;
    _forceTypeInformation = true; // gets reconsidered when we are contextualized
}
 
Example #21
Source Project: lams   Author: lamsfoundation   File: StaticListSerializerBase.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public JsonSerializer<?> createContextual(SerializerProvider serializers,
        BeanProperty property)
    throws JsonMappingException
{
    JsonSerializer<?> ser = null;
    Boolean unwrapSingle = null;
    
    if (property != null) {
        final AnnotationIntrospector intr = serializers.getAnnotationIntrospector();
        AnnotatedMember m = property.getMember();
        if (m != null) {
            Object serDef = intr.findContentSerializer(m);
            if (serDef != null) {
                ser = serializers.serializerInstance(m, serDef);
            }
        }
    }
    JsonFormat.Value format = findFormatOverrides(serializers, property, handledType());
    if (format != null) {
        unwrapSingle = format.getFeature(JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
    }
    // [databind#124]: May have a content converter
    ser = findContextualConvertingSerializer(serializers, property, ser);
    if (ser == null) {
        ser = serializers.findValueSerializer(String.class, property);
    }
    // Optimization: default serializer just writes String, so we can avoid a call:
    if (isDefaultSerializer(ser)) {
        if (unwrapSingle == _unwrapSingle) {
            return this;
        }
        return _withResolved(property, unwrapSingle);
    }
    // otherwise...
    // note: will never have TypeSerializer, because Strings are "natural" type
    return new CollectionSerializer(serializers.constructType(String.class),
            true, /*TypeSerializer*/ null, (JsonSerializer<Object>) ser);
}
 
Example #22
Source Project: lams   Author: lamsfoundation   File: AnyGetterWriter.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public AnyGetterWriter(BeanProperty property,
        AnnotatedMember accessor, JsonSerializer<?> serializer)
{
    _accessor = accessor;
    _property = property;
    _serializer = (JsonSerializer<Object>) serializer;
    if (serializer instanceof MapSerializer) {
        _mapSerializer = (MapSerializer) serializer;
    }
}
 
Example #23
Source Project: lams   Author: lamsfoundation   File: SettableAnyProperty.java    License: GNU General Public License v2.0 5 votes vote down vote up
public SettableAnyProperty(BeanProperty property, AnnotatedMember setter, JavaType type,
        KeyDeserializer keyDeser,
        JsonDeserializer<Object> valueDeser, TypeDeserializer typeDeser)
{
    _property = property;
    _setter = setter;
    _type = type;
    _valueDeserializer = valueDeser;
    _valueTypeDeserializer = typeDeser;
    _keyDeserializer = keyDeser;
    _setterIsField = setter instanceof AnnotatedField;
}
 
Example #24
Source Project: lams   Author: lamsfoundation   File: ValueInjector.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @deprecated in 2.9 (remove from 3.0)
 */
@Deprecated // see [databind#1835]
public ValueInjector(PropertyName propName, JavaType type,
        com.fasterxml.jackson.databind.util.Annotations contextAnnotations, // removed from later versions
        AnnotatedMember mutator, Object valueId)
{
    this(propName, type, mutator, valueId);
}
 
Example #25
Source Project: lams   Author: lamsfoundation   File: EnumResolver.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method used when actual String serialization is indicated using @JsonValue
 * on a method.
 *
 * @since 2.9
 */
@SuppressWarnings({ "unchecked" })
public static EnumResolver constructUnsafeUsingMethod(Class<?> rawEnumCls,
        AnnotatedMember accessor,
        AnnotationIntrospector ai)
{            
    // wrong as ever but:
    Class<Enum<?>> enumCls = (Class<Enum<?>>) rawEnumCls;
    return constructUsingMethod(enumCls, accessor, ai);
}
 
Example #26
Source Project: mattermost4j   Author: maruTA-bis5   File: MattermostPropertyNamingStrategy.java    License: Apache License 2.0 5 votes vote down vote up
protected PropertyNamingStrategy judgeStrategy(AnnotatedMember member) {
  Class<?> clazz = member.getDeclaringClass();
  if (Config.class.isAssignableFrom(clazz)) {
    return PropertyNamingStrategy.UPPER_CAMEL_CASE;
  } else if (clazz.getCanonicalName().startsWith("net.bis5.mattermost.model.config.")) {
    return PropertyNamingStrategy.UPPER_CAMEL_CASE;
  } else {
    return PropertyNamingStrategy.SNAKE_CASE;
  }
}
 
Example #27
Source Project: haven-platform   Author: codeabovelab   File: KeeperBeanDeserializerModifier.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanDescription beanDesc, BeanDeserializerBuilder builder) {
    for (BeanPropertyDefinition propDef : beanDesc.findProperties()) {
        if (!propDef.hasGetter() || propDef.hasSetter()) {
            continue;
        }
        AnnotatedMember getter = propDef.getAccessor();
        if (!Keeper.class.equals(getter.getRawType())) {
            continue;
        }

        builder.addOrReplaceProperty(new CustomGetterBeanProperty(propDef, getter), true);
    }
    return builder;
}
 
Example #28
Source Project: jackson-modules-base   Author: FasterXML   File: ParanamerAnnotationIntrospector.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String findImplicitPropertyName(AnnotatedMember param) {
    if (param instanceof AnnotatedParameter) {
        return _paranamer.findParameterName((AnnotatedParameter) param);
    }
    return null;
}
 
Example #29
Source Project: jackson-modules-base   Author: FasterXML   File: PropertyAccessorCollector.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor,
                  Implementation.Context implementationContext) {

    final List<StackManipulation> operations = new ArrayList<StackManipulation>();
    operations.add(loadLocalVar()); // load local for cast bean

    final AnnotatedMember member = prop.getMember();

    operations.add(invocationOperation(member, beanClassDescription));
    operations.add(methodReturn);

    final StackManipulation.Compound compound = new StackManipulation.Compound(operations);
    return compound.apply(methodVisitor, implementationContext);
}
 
Example #30
Source Project: netty-rest   Author: buremba   File: SwaggerJacksonAnnotationIntrospector.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean hasIgnoreMarker(AnnotatedMember m)
{
    ApiModelProperty ann = m.getAnnotation(ApiModelProperty.class);
    if (ann != null && ann.hidden()) {
        return true;
    }
    return false;
}