Example usage for com.fasterxml.jackson.databind.introspect AnnotatedMember setValue

List of usage examples for com.fasterxml.jackson.databind.introspect AnnotatedMember setValue

Introduction

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

Prototype

public abstract void setValue(Object paramObject1, Object paramObject2);

Source Link

Usage

From source file:org.jongo.marshall.jackson.JacksonObjectIdUpdater.java

public void setObjectId(Object target, ObjectId id) {
    for (BeanPropertyDefinition def : beanDescription(target.getClass()).findProperties()) {
        if (isIdProperty(def)) {
            AnnotatedMember accessor = def.getAccessor();
            accessor.fixAccess();/*from   w ww .j  av a  2 s.  c o m*/
            if (accessor.getValue(target) != null) {
                throw new IllegalArgumentException("Unable to set objectid on class: " + target.getClass());
            }
            AnnotatedMember field = def.getField();
            field.fixAccess();
            Class<?> type = field.getRawType();
            if (ObjectId.class.isAssignableFrom(type)) {
                field.setValue(target, id);
            } else if (type.equals(String.class) && isObjectId(def)) {
                field.setValue(target, id.toString());
            }
            return;
        }
    }
}

From source file:com.github.jonpeterson.spring.mvc.versioning.VersionedModelResponseBodyAdvice.java

@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
        Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    VersionedResponseBody versionedResponseBody = returnType.getMethodAnnotation(VersionedResponseBody.class);
    String targetVersion = null;/*from ww w .  j  a  v  a2  s.co  m*/

    String queryParamName = versionedResponseBody.queryParamName();
    if (!queryParamName.isEmpty()) {
        List<String> queryParamValues = UriComponentsBuilder.fromUri(request.getURI()).build().getQueryParams()
                .get(queryParamName);
        if (queryParamValues != null && !queryParamValues.isEmpty())
            targetVersion = queryParamValues.get(0);
    }

    if (targetVersion == null) {
        String headerName = versionedResponseBody.headerName();
        if (!headerName.isEmpty()) {
            List<String> headerValues = request.getHeaders().get(headerName);
            if (headerValues != null && !headerValues.isEmpty())
                targetVersion = headerValues.get(0);
        }
    }

    if (targetVersion == null)
        targetVersion = versionedResponseBody.defaultVersion();

    try {
        boolean serializeToSet = false;

        for (BeanPropertyDefinition beanPropertyDefinition : mapper.getSerializationConfig()
                .introspect(mapper.getTypeFactory().constructType(body.getClass())).findProperties()) {
            AnnotatedMember field = beanPropertyDefinition.getField();
            AnnotatedMember setter = beanPropertyDefinition.getSetter();
            if ((field != null && field.hasAnnotation(JsonSerializeToVersion.class))
                    || (setter != null && setter.hasAnnotation(JsonSerializeToVersion.class))) {
                if (setter != null)
                    setter.setValue(body, targetVersion);
                else
                    field.setValue(body, targetVersion);
                serializeToSet = true;
            }
        }

        if (!serializeToSet)
            throw new RuntimeException("no @" + JsonSerializeToVersion.class.getSimpleName()
                    + " annotation found on String field or setter method in " + body.getClass()
                    + "; this is a requirement when using @" + VersionedResponseBody.class.getSimpleName());

    } catch (Exception e) {
        throw new RuntimeException("unable to set the version of the response body model", e);
    }

    return body;
}