Search in sources :

Example 16 with PropertyModel

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);
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) TestBase(dev.morphia.test.TestBase) Test(org.testng.annotations.Test)

Example 17 with PropertyModel

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);
    }
}
Also used : Field(java.lang.reflect.Field) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) MappingException(dev.morphia.mapping.MappingException)

Example 18 with PropertyModel

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();
}
Also used : PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) ByteBuddy(net.bytebuddy.ByteBuddy) ByteCodeElement(net.bytebuddy.description.ByteCodeElement)

Example 19 with PropertyModel

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);
}
Also used : BsonReaderMark(org.bson.BsonReaderMark) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) MappingException(dev.morphia.mapping.MappingException) Key(dev.morphia.Key) MappingException(dev.morphia.mapping.MappingException)

Example 20 with PropertyModel

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());
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) MethodMappedSpecializedEntity(dev.morphia.test.models.methods.MethodMappedSpecializedEntity) SpecializedEntity(dev.morphia.test.models.SpecializedEntity) Test(org.testng.annotations.Test)

Aggregations

PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)22 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)10 Test (org.testng.annotations.Test)10 Document (org.bson.Document)4 Nullable (com.mongodb.lang.Nullable)3 Datastore (dev.morphia.Datastore)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 PathTarget (dev.morphia.internal.PathTarget)2 MappingException (dev.morphia.mapping.MappingException)2 PropertyHandler (dev.morphia.mapping.codec.pojo.PropertyHandler)2 DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)2 OperationTarget (dev.morphia.query.OperationTarget)2 MethodMappedSpecializedEntity (dev.morphia.test.models.methods.MethodMappedSpecializedEntity)2 Key (dev.morphia.Key)1 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)1 Mapper (dev.morphia.mapping.Mapper)1 NotMappableException (dev.morphia.mapping.NotMappableException)1 TestBase (dev.morphia.test.TestBase)1 CityPopulation (dev.morphia.test.models.CityPopulation)1