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

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

Introduction

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

Prototype

public final <A extends Annotation> boolean hasAnnotation(Class<A> paramClass) 

Source Link

Usage

From source file:com.github.jonpeterson.jackson.module.versioning.VersionedModelUtils.java

public static BeanPropertyDefinition getSerializeToVersionProperty(BeanDescription beanDescription)
        throws RuntimeException {
    BeanPropertyDefinition serializeToVersionProperty = null;
    for (BeanPropertyDefinition definition : beanDescription.findProperties()) {

        // merge field and accessor annotations
        if (definition instanceof POJOPropertyBuilder)
            ((POJOPropertyBuilder) definition).mergeAnnotations(true);

        AnnotatedMember accessor = definition.getAccessor();
        if (accessor != null && accessor.hasAnnotation(JsonSerializeToVersion.class)) {
            if (serializeToVersionProperty != null)
                throw new RuntimeException("@" + JsonSerializeToVersion.class.getSimpleName()
                        + " must be present on at most one field or method");
            if (accessor.getRawType() != String.class
                    || (definition.getField() == null && !definition.hasGetter()))
                throw new RuntimeException("@" + JsonSerializeToVersion.class.getSimpleName()
                        + " must be on a field or a getter method that returns a String");
            serializeToVersionProperty = definition;
        }/*from ww w.  j a v  a  2  s .  co  m*/
    }

    return serializeToVersionProperty;
}

From source file:org.usrz.libs.riak.introspection.RiakAnnotationIntrospector.java

@Override
public boolean hasIgnoreMarker(AnnotatedMember m) {
    if (m.hasAnnotation(RiakKey.class))
        return false;
    if (m.hasAnnotation(RiakLink.class))
        return false;
    if (m.hasAnnotation(RiakIndex.class))
        return false;
    if (m.hasAnnotation(RiakMetadata.class))
        return false;
    else//from  w w  w  .  j a  v  a2  s .c om
        return true;
}

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  . ja va  2s.  c  o 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;
}

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 ww  .  ja va 2s .com
    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);
        }
    }
}

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

/**
 * Applies annotations to property's {@link Xml} definition.
 *
 * @param member   annotations provider//from  ww w .j  av  a  2 s . c  o m
 * @param property property instance to be updated
 */
public static void apply(AnnotatedMember member, Property property) {
    if (member.hasAnnotation(XmlElementWrapper.class) || member.hasAnnotation(XmlElement.class)) {
        applyElement(member, property);
    } else if (member.hasAnnotation(XmlAttribute.class) && isAttributeAllowed(property)) {
        applyAttribute(member, property);
    }
}