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);
}
}
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."));
}
}
}
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);
}
}
}
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();
}
Aggregations