Search in sources :

Example 1 with TypeData

use of dev.morphia.mapping.codec.pojo.TypeData in project morphia by mongodb.

the class MorphiaReferenceCodec method decode.

@Override
public MorphiaReference decode(BsonReader reader, DecoderContext decoderContext) {
    Object value = getDatastore().getCodecRegistry().get(bsonTypeClassMap.get(reader.getCurrentBsonType())).decode(reader, decoderContext);
    value = processId(getDatastore(), value, decoderContext);
    TypeData typeData = getTypeData().getTypeParameters().get(0);
    EntityModel fieldEntityModel = getEntityModelForField();
    if (Set.class.isAssignableFrom(typeData.getType())) {
        return new SetReference<>(getDatastore(), mapper, fieldEntityModel, (List) value);
    } else if (Collection.class.isAssignableFrom(typeData.getType())) {
        return new ListReference<>(getDatastore(), mapper, fieldEntityModel, (List) value);
    } else if (Map.class.isAssignableFrom(typeData.getType())) {
        return new MapReference<>(getDatastore(), mapper, (Map) value, fieldEntityModel);
    } else {
        return new SingleReference<>(getDatastore(), mapper, fieldEntityModel, value);
    }
}
Also used : TypeData(dev.morphia.mapping.codec.pojo.TypeData) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Collection(java.util.Collection) List(java.util.List)

Example 2 with TypeData

use of dev.morphia.mapping.codec.pojo.TypeData in project morphia by mongodb.

the class MapKeyTypeConstraint method check.

@Override
protected void check(Mapper mapper, EntityModel entityModel, PropertyModel propertyModel, Set<ConstraintViolation> ve) {
    if (propertyModel.isMap()) {
        Class<?> aClass = null;
        List<TypeData<?>> typeParameters = propertyModel.getTypeData().getTypeParameters();
        if (!typeParameters.isEmpty()) {
            TypeData<?> typeData = typeParameters.get(0);
            aClass = typeData.getType();
        }
        // WARN if not parameterized : null or Object...
        if (aClass == null || Object.class.equals(aClass)) {
            ve.add(new ConstraintViolation(Level.WARNING, entityModel, propertyModel, getClass(), "Maps cannot be keyed by Object (Map<Object,?>); Use a parametrized type that is supported " + SUPPORTED));
        } else if (!aClass.equals(String.class) && !aClass.equals(ObjectId.class) && !isPrimitiveLike(aClass)) {
            ve.add(new ConstraintViolation(Level.FATAL, entityModel, propertyModel, getClass(), "Maps must be keyed by a simple type " + SUPPORTED + "; " + aClass + " is not supported as a map key type."));
        }
    }
}
Also used : TypeData(dev.morphia.mapping.codec.pojo.TypeData) ConstraintViolation(dev.morphia.mapping.validation.ConstraintViolation)

Example 3 with TypeData

use of dev.morphia.mapping.codec.pojo.TypeData in project morphia by mongodb.

the class ConfigureProperties method processProperties.

@SuppressWarnings("rawtypes")
void processProperties(EntityModelBuilder modelBuilder, MapperOptions options) {
    Iterator<PropertyModelBuilder> iterator = modelBuilder.propertyModels().iterator();
    while (iterator.hasNext()) {
        final PropertyModelBuilder builder = iterator.next();
        final int modifiers = builder.modifiers();
        if (isStatic(modifiers) || isTransient(builder)) {
            iterator.remove();
        } else {
            Property property = builder.getAnnotation(Property.class);
            if (property != null && !property.concreteClass().equals(Object.class)) {
                TypeData typeData = builder.typeData().withType(property.concreteClass());
                builder.typeData(typeData);
            }
            AlsoLoad alsoLoad = builder.getAnnotation(AlsoLoad.class);
            if (alsoLoad != null) {
                for (String name : alsoLoad.value()) {
                    builder.alternateName(name);
                }
            }
            if (builder.getAnnotation(Id.class) != null) {
                modelBuilder.idPropertyName(builder.name());
            }
            if (builder.getAnnotation(Version.class) != null) {
                modelBuilder.versionPropertyName(builder.name());
            }
            buildProperty(options, builder);
        }
    }
}
Also used : PropertyModelBuilder(dev.morphia.mapping.codec.pojo.PropertyModelBuilder) Version(dev.morphia.annotations.Version) AlsoLoad(dev.morphia.annotations.AlsoLoad) TypeData(dev.morphia.mapping.codec.pojo.TypeData) Id(dev.morphia.annotations.Id) Property(dev.morphia.annotations.Property)

Example 4 with TypeData

use of dev.morphia.mapping.codec.pojo.TypeData in project morphia by mongodb.

the class ReferenceCodec method getEncoderClass.

@Override
public Class getEncoderClass() {
    TypeData type = getTypeData();
    List typeParameters = type.getTypeParameters();
    if (!typeParameters.isEmpty()) {
        type = (TypeData) typeParameters.get(typeParameters.size() - 1);
    }
    return type.getType();
}
Also used : TypeData(dev.morphia.mapping.codec.pojo.TypeData) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

TypeData (dev.morphia.mapping.codec.pojo.TypeData)4 List (java.util.List)2 AlsoLoad (dev.morphia.annotations.AlsoLoad)1 Id (dev.morphia.annotations.Id)1 Property (dev.morphia.annotations.Property)1 Version (dev.morphia.annotations.Version)1 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)1 PropertyModelBuilder (dev.morphia.mapping.codec.pojo.PropertyModelBuilder)1 ConstraintViolation (dev.morphia.mapping.validation.ConstraintViolation)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1