Example usage for com.fasterxml.jackson.databind BeanDescription getClassInfo

List of usage examples for com.fasterxml.jackson.databind BeanDescription getClassInfo

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind BeanDescription getClassInfo.

Prototype

public abstract AnnotatedClass getClassInfo();

Source Link

Usage

From source file:org.bonitasoft.engine.business.data.impl.jackson.EntityBeanSerializerModifier.java

@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc,
        List<BeanPropertyWriter> beanProperties) {
    LOG.trace("Changing list of property writers for {}", beanDesc.getClassInfo());
    List<BeanPropertyWriter> newProperties = new ArrayList<>();

    if (shouldBeIgnored(beanDesc)) {
        LOG.trace("Ignoring all properties of this bean");
        return newProperties;
    }/*from w w w. j  ava 2  s  . co  m*/

    for (BeanPropertyWriter beanPropertyWriter : beanProperties) {
        LOG.trace("{}", beanPropertyWriter);
        LOG.trace("Bean type {}", beanPropertyWriter.getType());

        if (shouldBeReplacedByLink(beanPropertyWriter)) {
            LOG.trace("Has to be replaced by link");
            BeanPropertyWriter ignoredPropertyWriter = new IgnoredPropertyWriter(beanPropertyWriter);
            LOG.trace("Adding only an ignored property writer {}", ignoredPropertyWriter);
            newProperties.add(ignoredPropertyWriter);
        } else {
            newProperties.add(beanPropertyWriter);
            if (ExtraPropertyUtils.shouldAddExtraProperty(beanPropertyWriter)) {
                LOG.trace("Will have an additional property");
                BeanPropertyWriter additionalPropertyWriter = ExtraBeanPropertyWriter
                        .newWriter(beanPropertyWriter);
                LOG.trace("Adding new property {}", additionalPropertyWriter);
                newProperties.add(additionalPropertyWriter);
            }
        }
    }
    return newProperties;
}

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

public Model resolve(Type rawType, ModelConverterContext context, Iterator<ModelConverter> next) {
    if (this.shouldIgnoreClass(rawType)) {
        return null;
    }// w w w .j ava  2 s.co  m

    /**
     * See java doc of NestedComplexType. This unwrapping makes sure that a
     * real type of field used throughout the method. At the same time flag
     * 'isNested' helps to indicate later in the method that this type may
     * be introspected as Hateoas enabled field
     */
    boolean isNested = false;
    if (rawType instanceof NestedComplexType) {
        isNested = true;
        NestedComplexType nested = (NestedComplexType) rawType;
        rawType = nested.getOriginalType();
    }
    JavaType type = pMapper.constructType(rawType);

    if (type.isEnumType() || PrimitiveType.fromType(type) != null) {
        // We don't build models for primitive types
        return null;
    }

    final BeanDescription beanDesc = pMapper.getSerializationConfig().introspect(type);
    // Couple of possibilities for defining
    String name = isNested ? "Nested" : "";
    name += pTypeName(type, beanDesc);

    if ("Object".equals(name)) {
        return new ModelImpl();
    }

    final ModelImpl model = new ModelImpl().type(ModelImpl.OBJECT).name(name)
            .description(pDescription(beanDesc.getClassInfo()));

    if (!type.isContainerType()) {
        // define the model here to support self/cyclic referencing of
        // models
        context.defineModel(name, model, type, null);
    }

    if (type.isContainerType()) {
        // We treat collections as primitive types, just need to add models
        // for values (if any)
        context.resolve(type.getContentType());
        return null;
    }
    // if XmlRootElement annotation, construct an Xml object and attach it
    // to the model
    XmlRootElement rootAnnotation = beanDesc.getClassAnnotations().get(XmlRootElement.class);
    if (rootAnnotation != null && !"".equals(rootAnnotation.name())
            && !"##default".equals(rootAnnotation.name())) {
        log.debug(rootAnnotation.toString());
        Xml xml = new Xml().name(rootAnnotation.name());
        if (rootAnnotation.namespace() != null && !"".equals(rootAnnotation.namespace())
                && !"##default".equals(rootAnnotation.namespace())) {
            xml.namespace(rootAnnotation.namespace());
        }
        model.xml(xml);
    }

    // see if @JsonIgnoreProperties exist
    Set<String> propertiesToIgnore = new HashSet<String>();
    JsonIgnoreProperties ignoreProperties = beanDesc.getClassAnnotations().get(JsonIgnoreProperties.class);
    if (ignoreProperties != null) {
        propertiesToIgnore.addAll(Arrays.asList(ignoreProperties.value()));
    }

    final ApiModel apiModel = beanDesc.getClassAnnotations().get(ApiModel.class);
    String disc = (apiModel == null) ? "" : apiModel.discriminator();

    if (apiModel != null && StringUtils.isNotEmpty(apiModel.reference())) {
        model.setReference(apiModel.reference());
    }

    if (disc.isEmpty()) {
        // longer method would involve
        // AnnotationIntrospector.findTypeResolver(...) but:
        JsonTypeInfo typeInfo = beanDesc.getClassAnnotations().get(JsonTypeInfo.class);
        if (typeInfo != null) {
            disc = typeInfo.property();
        }
    }
    if (!disc.isEmpty()) {
        model.setDiscriminator(disc);
    }

    List<Property> props = new ArrayList<Property>();
    for (BeanPropertyDefinition propDef : beanDesc.findProperties()) {
        parseProperty(context, isNested, beanDesc, propertiesToIgnore, props, propDef);
    }

    Collections.sort(props, getPropertyComparator());

    Map<String, Property> modelProps = new LinkedHashMap<String, Property>();
    for (Property prop : props) {
        modelProps.put(prop.getName(), prop);
    }
    model.setProperties(modelProps);

    /**
     * This must be done after model.setProperties so that the model's set
     * of properties is available to filter from any subtypes
     **/
    if (!resolveSubtypes(model, beanDesc, context)) {
        model.setDiscriminator(null);
    }

    return model;
}

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

private boolean resolveSubtypes(ModelImpl model, BeanDescription bean, ModelConverterContext context) {
    final List<NamedType> types = pIntr.findSubtypes(bean.getClassInfo());
    if (types == null) {
        return false;
    }//from   w  w  w  . ja va  2s . c o m
    int count = 0;
    final Class<?> beanClass = bean.getClassInfo().getAnnotated();
    for (NamedType subtype : types) {
        final Class<?> subtypeType = subtype.getType();
        if (!beanClass.isAssignableFrom(subtypeType)) {
            continue;
        }

        final Model subtypeModel = context.resolve(subtypeType);

        if (subtypeModel instanceof ModelImpl) {
            final ModelImpl impl = (ModelImpl) subtypeModel;

            // check if model name was inherited
            if (impl.getName().equals(model.getName())) {
                impl.setName(pTypeNameResolver.nameForType(pMapper.constructType(subtypeType),
                        TypeNameResolver.Options.SKIP_API_MODEL));
            }

            // remove shared properties defined in the parent
            final Map<String, Property> baseProps = model.getProperties();
            final Map<String, Property> subtypeProps = impl.getProperties();
            if (baseProps != null && subtypeProps != null) {
                for (Map.Entry<String, Property> entry : baseProps.entrySet()) {
                    if (entry.getValue().equals(subtypeProps.get(entry.getKey()))) {
                        subtypeProps.remove(entry.getKey());
                    }
                }
            }

            impl.setDiscriminator(null);
            ComposedModel child = new ComposedModel().parent(new RefModel(model.getName())).child(impl);
            context.defineModel(impl.getName(), child);
            ++count;
        }
    }
    return count != 0;
}