use of dev.morphia.mapping.MappingException in project morphia by mongodb.
the class FieldDiscovery method apply.
@Override
public void apply(Mapper mapper, EntityModelBuilder builder) {
if (builder.propertyModels().isEmpty()) {
List<Class<?>> list = new ArrayList<>(List.of(builder.type()));
list.addAll(builder.classHierarchy());
for (Class<?> type : list) {
for (Field field : type.getDeclaredFields()) {
TypeData<?> typeData = builder.getTypeData(type, TypeData.newInstance(field), field.getGenericType());
try {
builder.addProperty().name(field.getName()).typeData(typeData).annotations(List.of(field.getDeclaredAnnotations())).accessor(getAccessor(getTargetField(builder, field), typeData)).modifiers(field.getModifiers()).discoverMappedName();
} catch (NoSuchFieldException e) {
throw new MappingException(Sofia.mismatchedFieldOnExternalType(field.getName(), builder.type().getName(), builder.targetType().getName()));
}
}
}
}
}
use of dev.morphia.mapping.MappingException in project morphia by mongodb.
the class ReferenceCodec method processId.
/**
* Decodes an ID value
*
* @param datastore the Datastore to use
* @param decode the value to decode
* @param decoderContext the decoder context
* @return the decoded value
*/
@NonNull
public static Object processId(Datastore datastore, Object decode, DecoderContext decoderContext) {
Object id = decode;
if (id instanceof Iterable) {
Iterable<?> iterable = (Iterable<?>) id;
List<Object> ids = new ArrayList<>();
for (Object o : iterable) {
ids.add(processId(datastore, o, decoderContext));
}
id = ids;
} else if (id instanceof Document) {
Document document = (Document) id;
if (document.containsKey("$ref")) {
id = processId(datastore, new DBRef(document.getString("$db"), document.getString("$ref"), document.get("$id")), decoderContext);
} else if (document.containsKey(datastore.getMapper().getOptions().getDiscriminatorKey())) {
try {
id = datastore.getCodecRegistry().get(datastore.getMapper().getClass(document)).decode(new DocumentReader(document), decoderContext);
} catch (CodecConfigurationException e) {
throw new MappingException(Sofia.cannotFindTypeInDocument(), e);
}
}
} else if (id instanceof DBRef) {
DBRef ref = (DBRef) id;
Object refId = ref.getId();
if (refId instanceof Document) {
refId = datastore.getCodecRegistry().get(Object.class).decode(new DocumentReader((Document) refId), decoderContext);
}
id = new DBRef(ref.getDatabaseName(), ref.getCollectionName(), refId);
}
return id;
}
use of dev.morphia.mapping.MappingException in project morphia by mongodb.
the class ReferenceCodec method createProxy.
private <T> T createProxy(MorphiaReference<?> reference) {
ReferenceProxy referenceProxy = new ReferenceProxy(reference);
PropertyModel propertyModel = getPropertyModel();
try {
Class<?> type = propertyModel.getType();
// Get or create proxy class
Class<T> proxyClass = (Class<T>) typeCache.findOrInsert(type.getClassLoader(), getCacheKey(type), () -> makeProxy(), typeCache);
// ... instantiate it
final T proxy = proxyClass.getDeclaredConstructor().newInstance();
// .. and set the invocation handler
final Field field = proxyClass.getDeclaredField(FIELD_INVOCATION_HANDLER);
field.setAccessible(true);
field.set(proxy, referenceProxy);
return proxy;
} catch (ReflectiveOperationException | IllegalArgumentException e) {
throw new MappingException(e.getMessage(), e);
}
}
use of dev.morphia.mapping.MappingException in project morphia by mongodb.
the class ClassMethodPair method getOrCreateInstance.
private Object getOrCreateInstance(Class<?> type) {
try {
Constructor<?> declaredConstructor = type.getDeclaredConstructor();
declaredConstructor.setAccessible(true);
return declaredConstructor.newInstance();
} catch (ReflectiveOperationException e) {
throw new MappingException(Sofia.cannotInstantiate(type, e.getMessage()));
}
}
use of dev.morphia.mapping.MappingException in project morphia by mongodb.
the class ObjectCodec method decode.
@Override
public Object decode(BsonReader reader, DecoderContext decoderContext) {
BsonType bsonType = reader.getCurrentBsonType();
Class<?> clazz;
if (bsonType == BsonType.DOCUMENT) {
clazz = Document.class;
String discriminatorField = datastore.getMapper().getOptions().getDiscriminatorKey();
BsonReaderMark mark = reader.getMark();
reader.readStartDocument();
while (clazz.equals(Document.class) && reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
if (reader.readName().equals(discriminatorField)) {
try {
clazz = datastore.getMapper().getClass(reader.readString());
} catch (CodecConfigurationException e) {
throw new MappingException(e.getMessage(), e);
}
} else {
reader.skipValue();
}
}
mark.reset();
} else {
clazz = bsonTypeClassMap.get(bsonType);
}
return datastore.getCodecRegistry().get(clazz).decode(reader, decoderContext);
}
Aggregations