Example usage for com.fasterxml.jackson.databind.introspect BeanPropertyDefinition getSetter

List of usage examples for com.fasterxml.jackson.databind.introspect BeanPropertyDefinition getSetter

Introduction

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

Prototype

public abstract AnnotatedMethod getSetter();

Source Link

Usage

From source file:springfox.documentation.schema.Annotations.java

@SuppressWarnings("PMD")
private static <A extends Annotation> Optional<A> tryGetSetterAnnotation(
        BeanPropertyDefinition beanPropertyDefinition, Class<A> annotationClass) {
    if (beanPropertyDefinition.hasSetter()) {
        return Optional.fromNullable(beanPropertyDefinition.getSetter().getAnnotation(annotationClass));
    }//from   w  w  w. j av  a 2 s.c  om
    return Optional.absent();
}

From source file:com.google.api.server.spi.config.jsonwriter.JacksonResourceSchemaProvider.java

@Override
public ResourceSchema getResourceSchema(TypeToken<?> type, ApiConfig config) {
    ResourceSchema schema = super.getResourceSchema(type, config);
    if (schema != null) {
        return schema;
    }// w  w w .  j av  a2 s. c  o m
    ObjectMapper objectMapper = ObjectMapperUtil.createStandardObjectMapper(config.getSerializationConfig());
    JavaType javaType = objectMapper.getTypeFactory().constructType(type.getRawType());
    BeanDescription beanDescription = objectMapper.getSerializationConfig().introspect(javaType);
    ResourceSchema.Builder schemaBuilder = ResourceSchema.builderForType(type.getRawType());
    Set<String> genericDataFieldNames = getGenericDataFieldNames(type);
    for (BeanPropertyDefinition definition : beanDescription.findProperties()) {
        TypeToken<?> propertyType = getPropertyType(type, toMethod(definition.getGetter()),
                toMethod(definition.getSetter()), definition.getField(), config);
        String name = definition.getName();
        if (genericDataFieldNames == null || genericDataFieldNames.contains(name)) {
            if (hasUnresolvedType(propertyType)) {
                logger.warning("skipping field '" + name + "' of type '" + propertyType
                        + "' because it is unresolved.");
                continue;
            }
            if (propertyType != null) {
                schemaBuilder.addProperty(name, ResourcePropertySchema.of(propertyType));
            } else {
                logger.warning("No type found for property '" + name + "' on class '" + type + "'.");
            }
        } else {
            logger.fine("skipping field '" + name + "' because it's not a Java client model field.");
        }
    }
    return schemaBuilder.build();
}

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 w  w  w.  java 2 s. c  om

    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;
}

From source file:fr.norad.jaxrs.doc.parser.ModelJacksonParser.java

@Override
public List<PropertyAccessor> findProperties(Class<?> modelClass) {
    List<PropertyAccessor> properties = new ArrayList<>();

    BasicBeanDescription beanDesc = fakeSerializer.getDescription(modelClass);
    List<BeanPropertyDefinition> findProperties = beanDesc.findProperties();
    for (BeanPropertyDefinition beanPropertyDefinition : findProperties) {
        if (modelClass.isEnum() && "declaringClass".equals(beanPropertyDefinition.getName())) {
            continue;
        }//w w w  .  ja v a 2  s  .co  m
        AnnotatedMethod getterJackson = beanPropertyDefinition.getGetter();
        AnnotatedMethod setterJackson = beanPropertyDefinition.getSetter();
        AnnotatedField fieldJackson = null;
        try {
            fieldJackson = beanPropertyDefinition.getField();
        } catch (Exception e) {
            log.warning("Name conflict on fields in bean : " + beanPropertyDefinition + " during doc generation"
                    + e.getMessage());
        }

        Method getter = getterJackson == null ? null : getterJackson.getAnnotated();
        Method setter = setterJackson == null ? null : setterJackson.getAnnotated();
        Field field = fieldJackson == null ? null : fieldJackson.getAnnotated();
        if (getter == null && setter == null && field == null) {
            log.warning(
                    "Cannot find valid property element for : " + beanPropertyDefinition + " on " + modelClass);
            continue;
        }

        PropertyAccessor property = new PropertyAccessor();
        property.setField(field);
        property.setGetter(getter);
        property.setSetter(setter);
        property.setName(beanPropertyDefinition.getName());

        properties.add(property);
    }
    return properties;
}

From source file:org.candlepin.swagger.CandlepinSwaggerModelConverter.java

private void parseProperty(ModelConverterContext context, boolean isNested, final BeanDescription beanDesc,
        Set<String> propertiesToIgnore, List<Property> props, BeanPropertyDefinition propDef) {
    Property property = null;//from  w w w . j  a va2 s.  co  m
    String propName = propDef.getName();
    Annotation[] annotations = null;

    propName = getPropName(propDef, propName);

    PropertyMetadata md = propDef.getMetadata();

    boolean hasSetter = false, hasGetter = false;
    if (propDef.getSetter() == null) {
        hasSetter = false;
    } else {
        hasSetter = true;
    }
    if (propDef.getGetter() != null) {
        JsonProperty pd = propDef.getGetter().getAnnotation(JsonProperty.class);
        if (pd != null) {
            hasGetter = true;
        }
    }
    Boolean isReadOnly = null;
    if (!hasSetter & hasGetter) {
        isReadOnly = Boolean.TRUE;
    } else {
        isReadOnly = Boolean.FALSE;
    }

    final AnnotatedMember member = propDef.getPrimaryMember();

    if (member != null && !propertiesToIgnore.contains(propName) &&
    /**
     * If the owning type is nested than we should include only those
     * fields that have the Hateoas annotation.
     */
            !(isNested && !member.hasAnnotation(HateoasInclude.class))) {

        List<Annotation> annotationList = new ArrayList<Annotation>();
        for (Annotation a : member.annotations()) {
            annotationList.add(a);
        }

        annotations = annotationList.toArray(new Annotation[annotationList.size()]);

        ApiModelProperty mp = member.getAnnotation(ApiModelProperty.class);

        if (mp != null && mp.readOnly()) {
            isReadOnly = mp.readOnly();
        }

        Type nested = null;
        JavaType propType = member.getType(beanDesc.bindingsForBeanType());
        JsonFilter jsonFilter = propType.getRawClass().getAnnotation(JsonFilter.class);

        /**
         * At this point the propType is a type of some nested field of the
         * type that is being processed. The condition checks if this
         * particular type should have Hateoas serialization enabled. In
         * other words, if we should create a new Nested* model.
         */
        if (jsonFilter != null && (jsonFilter.value().equals("ConsumerFilter")
                || jsonFilter.value().equals("EntitlementFilter") || jsonFilter.value().equals("OwnerFilter")
                || jsonFilter.value().equals("GuestFilter"))) {
            if (!nestedJavaTypes.containsKey(propType)) {
                nestedJavaTypes.put(propType, new NestedComplexType(propType));
            }
            nested = nestedJavaTypes.get(propType);
        } else {
            nested = propType;
        }

        // allow override of name from annotation
        if (mp != null && !mp.name().isEmpty()) {
            propName = mp.name();
        }

        if (mp != null && !mp.dataType().isEmpty()) {
            property = resolveApiAnnotated(context, property, annotations, mp, propType);
        }

        // no property from override, construct from propType
        if (property == null) {
            if (mp != null && StringUtils.isNotEmpty(mp.reference())) {
                property = new RefProperty(mp.reference());
            } else if (member.getAnnotation(JsonIdentityInfo.class) != null) {
                property = GeneratorWrapper.processJsonIdentity(propType, context, pMapper,
                        member.getAnnotation(JsonIdentityInfo.class),
                        member.getAnnotation(JsonIdentityReference.class));
            }
            if (property == null) {
                property = context.resolveProperty(nested, annotations);
            }
        }

        if (property != null) {
            addMetadataToProperty(property, propName, md, isReadOnly, member, mp);
            applyBeanValidatorAnnotations(property, annotations);
            props.add(property);
        }
    }
}