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

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

Introduction

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

Prototype

public abstract String getName();

Source Link

Usage

From source file:org.springframework.data.rest.webmvc.alps.RootResourceInformationToAlpsDescriptorConverter.java

private List<Descriptor> buildPropertyDescriptors(final Class<?> type, String baseRel) {

    final PersistentEntity<?, ?> entity = persistentEntities.getPersistentEntity(type);
    final List<Descriptor> propertyDescriptors = new ArrayList<Descriptor>();
    final JacksonMetadata jackson = new JacksonMetadata(mapper, type);
    final ResourceMetadata metadata = associations.getMetadataFor(entity.getType());

    entity.doWithProperties(new SimplePropertyHandler() {

        @Override/*from  w  w  w .j  ava2 s .  com*/
        public void doWithPersistentProperty(PersistentProperty<?> property) {

            BeanPropertyDefinition propertyDefinition = jackson.getDefinitionFor(property);
            ResourceMapping propertyMapping = metadata.getMappingFor(property);

            if (propertyDefinition != null) {

                if (property.isIdProperty() && !configuration.isIdExposedFor(property.getOwner().getType())) {
                    return;
                }

                propertyDescriptors.add(//
                        descriptor(). //
                type(Type.SEMANTIC).//
                name(propertyDefinition.getName()).//
                doc(getDocFor(propertyMapping.getDescription(), property)).//
                build());
            }
        }
    });

    entity.doWithAssociations(new SimpleAssociationHandler() {

        @Override
        public void doWithAssociation(Association<? extends PersistentProperty<?>> association) {

            PersistentProperty<?> property = association.getInverse();

            if (!jackson.isExported(property) || !associations.isLinkableAssociation(property)) {
                return;
            }

            ResourceMapping mapping = metadata.getMappingFor(property);

            DescriptorBuilder builder = descriptor().//
            name(mapping.getRel()).doc(getDocFor(mapping.getDescription()));

            ResourceMetadata targetTypeMetadata = associations.getMetadataFor(property.getActualType());

            String href = ProfileController.getPath(configuration, targetTypeMetadata) + "#"
                    + getRepresentationDescriptorId(targetTypeMetadata);

            Link link = new Link(href).withSelfRel();

            builder.//
            type(Type.SAFE).//
            rt(link.getHref());

            propertyDescriptors.add(builder.build());
        }
    });

    return propertyDescriptors;
}

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;//  w ww. jav  a2  s  .c  o  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);
        }
    }
}

From source file:org.midonet.cluster.rest_api.serialization.MidonetObjectMapper.java

static JsonError getError(DeserializationConfig config, JavaType valueType, String fieldName) {
    try {//from  w  w w .  j  av a 2  s .  c  o  m
        BasicBeanDescription beanDesc = config.introspect(valueType);
        for (BeanPropertyDefinition property : beanDesc.findProperties()) {
            if (property.getName().equals(fieldName) && null != property.getField()
                    && null != property.getField().getAnnotated()) {
                return property.getField().getAnnotated().getAnnotation(JsonError.class);
            }
        }
        return null;
    } catch (Exception e) {
        return null;
    }
}

From source file:uk.gov.gchq.gaffer.rest.service.GraphConfigurationService.java

@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Need to wrap all runtime exceptions before they are given to the user")
@Override/*from w  w w  .  j  a va  2  s.c om*/
public Set<String> getSerialisedFields(final String className) {
    final Class<?> clazz;
    try {
        clazz = Class.forName(className);
    } catch (final Exception e) {
        throw new IllegalArgumentException("Class name was not recognised: " + className, e);
    }

    final ObjectMapper mapper = new ObjectMapper();
    final JavaType type = mapper.getTypeFactory().constructType(clazz);
    final BeanDescription introspection = mapper.getSerializationConfig().introspect(type);
    final List<BeanPropertyDefinition> properties = introspection.findProperties();

    final Set<String> fields = new HashSet<>();
    for (final BeanPropertyDefinition property : properties) {
        fields.add(property.getName());
    }

    return fields;
}