use of dev.morphia.mapping.codec.pojo.PropertyModel in project morphia by mongodb.
the class EntityModelTest method testGenericFields.
@Test
public void testGenericFields() {
EntityModel model = getDs().getMapper().map(Base.class).get(0);
assertEquals(model.getProperties().size(), 3, model.getProperties().stream().map(PropertyModel::getName).collect(joining(", ")));
model = getDs().getMapper().map(Parent.class).get(0);
assertEquals(model.getProperties().size(), 4, model.getProperties().stream().map(PropertyModel::getName).collect(joining(", ")));
model = getDs().getMapper().map(Child.class).get(0);
assertEquals(model.getProperties().size(), 5, model.getProperties().stream().map(PropertyModel::getName).collect(joining(", ")));
assertEquals(model.getProperty("t").getType(), String.class);
assertEquals(model.getProperty("someField").getType(), LocalDate.class);
}
use of dev.morphia.mapping.codec.pojo.PropertyModel 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.codec.pojo.PropertyModel in project morphia by mongodb.
the class ReferenceCodec method makeProxy.
private <T> Class<T> makeProxy() {
PropertyModel propertyModel = getPropertyModel();
Class<?> type = propertyModel.getType();
Builder<?> builder = new ByteBuddy().subclass(type).implement(MorphiaProxy.class).name(format("%s$%s$$ReferenceProxy", propertyModel.getEntityModel().getName(), propertyModel.getName()));
Junction<ByteCodeElement> matcher = ElementMatchers.isDeclaredBy(type);
if (!type.isInterface()) {
type = type.getSuperclass();
while (type != null && !type.equals(Object.class)) {
matcher = matcher.or(ElementMatchers.isDeclaredBy(type));
type = type.getSuperclass();
}
}
return (Class<T>) builder.invokable(matcher.or(ElementMatchers.isDeclaredBy(MorphiaProxy.class))).intercept(InvocationHandlerAdapter.toField(FIELD_INVOCATION_HANDLER)).defineField(FIELD_INVOCATION_HANDLER, InvocationHandler.class, Visibility.PRIVATE).make().load(Thread.currentThread().getContextClassLoader(), Default.WRAPPER).getLoaded();
}
use of dev.morphia.mapping.codec.pojo.PropertyModel in project morphia by mongodb.
the class KeyCodec method decode.
@Override
public Key decode(BsonReader reader, DecoderContext decoderContext) {
reader.readStartDocument();
final String ref = reader.readString("$ref");
reader.readName();
final BsonReaderMark mark = reader.getMark();
Object idValue = null;
EntityModel model = null;
final Iterator<EntityModel> iterator = datastore.getMapper().getClassesMappedToCollection(ref).iterator();
while (idValue == null && iterator.hasNext()) {
model = iterator.next();
try {
final PropertyModel idField = model.getIdProperty();
if (idField != null) {
final Class<?> idType = idField.getType();
idValue = datastore.getCodecRegistry().get(idType).decode(reader, decoderContext);
}
} catch (Exception e) {
mark.reset();
}
}
if (idValue == null) {
throw new MappingException("Could not map the Key to a type.");
}
reader.readEndDocument();
return new Key<>(model.getType(), ref, idValue);
}
use of dev.morphia.mapping.codec.pojo.PropertyModel in project morphia by mongodb.
the class TestGenerics method testGenericEntities.
@Test
public void testGenericEntities() {
EntityModel entityModel = getMapper().map(SpecializedEntity.class).get(0);
PropertyModel test = entityModel.getProperty("test");
assertEquals(test.getType(), UUID.class);
SpecializedEntity beforeDB = new SpecializedEntity();
beforeDB.setId(UUID.randomUUID());
beforeDB.setTest(UUID.randomUUID());
getDs().save(beforeDB);
SpecializedEntity loaded = getDs().find(SpecializedEntity.class).filter(eq("_id", beforeDB.getId())).first();
assertEquals(loaded.getId(), beforeDB.getId());
assertEquals(loaded.getTest(), beforeDB.getTest());
}
Aggregations