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 File: BeanSerializerBase.java    From lams with 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 File: PropertyMutatorCollector.java    From jackson-modules-base with 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 #3
Source File: EnumResolver.java    From lams with 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 #4
Source File: StdSerializer.java    From lams with 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 #5
Source File: PropertyMutatorCollector.java    From jackson-modules-base with 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 #6
Source File: StdSerializer.java    From lams with 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 #7
Source File: PropertyMutatorCollector.java    From jackson-modules-base with 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 #8
Source File: ConfigTreeBuilder.java    From dropwizard-guicey with 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 #9
Source File: PropertyAccessorCollector.java    From jackson-modules-base with 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 #10
Source File: IssueTrackerConfiguration.java    From spring-data-dev-tools with 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 #11
Source File: PropertyAccessorCollector.java    From jackson-modules-base with 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 #12
Source File: SwaggerJacksonAnnotationIntrospector.java    From netty-rest with 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 File: StdDeserializer.java    From lams with 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 #14
Source File: BeanProperty.java    From lams with 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 #15
Source File: JsonValueSerializer.java    From lams with 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 #16
Source File: SwaggerJacksonAnnotationIntrospector.java    From netty-rest with Apache License 2.0 5 votes vote down vote up
@Override
public String findImplicitPropertyName(AnnotatedMember member)
{
    ApiParam annotation = member.getAnnotation(ApiParam.class);
    if (annotation != null && annotation.value() != "") {
        return annotation.value();
    }
    else {
        return super.findImplicitPropertyName(member);
    }
}
 
Example #17
Source File: AutoMatterAnnotationIntrospector.java    From auto-matter with Apache License 2.0 5 votes vote down vote up
@Override
public String findImplicitPropertyName(final AnnotatedMember member) {
  final AutoMatter.Field field = member.getAnnotation(AutoMatter.Field.class);
  if (field == null) {
    return null;
  }
  if (member instanceof AnnotatedParameter) {
    return field.value();
  }
  if (member instanceof AnnotatedMethod) {
    return member.getName();
  }
  return null;
}
 
Example #18
Source File: JacksonLombokAnnotationIntrospector.java    From jackson-lombok with MIT License 5 votes vote down vote up
@Override
public String findImplicitPropertyName(AnnotatedMember member) {
    JsonProperty property = member.getAnnotation(JsonProperty.class);
    if (property == null) {
        if (member instanceof AnnotatedMethod) {
            AnnotatedMethod method = (AnnotatedMethod) member;
            String fieldName = BeanUtil.okNameForGetter(method);
            return getJacksonPropertyName(member.getDeclaringClass(), fieldName);
        }
    } else if (!property.value().equals("")) {
        return property.value();
    }

    return null;
}
 
Example #19
Source File: UnsignedXrayClient.java    From aws-xray-sdk-java with 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 #20
Source File: HalEntityWrapperJacksonSerializer.java    From quarkus with 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 #21
Source File: FirebaseConfiguration.java    From spring-boot-starter-data-firebase with 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 #22
Source File: StaticListSerializerBase.java    From lams with 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 #23
Source File: AnyGetterWriter.java    From lams with 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 #24
Source File: SettableAnyProperty.java    From lams with 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 #25
Source File: PropertyAccessorCollector.java    From jackson-modules-base with 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 #26
Source File: ParanamerAnnotationIntrospector.java    From jackson-modules-base with 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 #27
Source File: KeeperBeanDeserializerModifier.java    From haven-platform with 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 File: ValueInjector.java    From lams with 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 #29
Source File: EnumResolver.java    From lams with 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 #30
Source File: MattermostPropertyNamingStrategy.java    From mattermost4j with 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;
  }
}