Search in sources :

Example 6 with MappingException

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

Example 7 with MappingException

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;
}
Also used : CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) ArrayList(java.util.ArrayList) DBRef(com.mongodb.DBRef) DocumentReader(dev.morphia.mapping.codec.reader.DocumentReader) Document(org.bson.Document) MappingException(dev.morphia.mapping.MappingException) NonNull(com.mongodb.lang.NonNull)

Example 8 with MappingException

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

Example 9 with MappingException

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()));
    }
}
Also used : MappingException(dev.morphia.mapping.MappingException)

Example 10 with MappingException

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);
}
Also used : BsonType(org.bson.BsonType) BsonReaderMark(org.bson.BsonReaderMark) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) Document(org.bson.Document) MappingException(dev.morphia.mapping.MappingException)

Aggregations

MappingException (dev.morphia.mapping.MappingException)12 Document (org.bson.Document)4 ArrayList (java.util.ArrayList)3 DBRef (com.mongodb.DBRef)2 Key (dev.morphia.Key)2 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)2 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)2 Field (java.lang.reflect.Field)2 BsonReaderMark (org.bson.BsonReaderMark)2 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)2 Test (org.testng.annotations.Test)2 NonNull (com.mongodb.lang.NonNull)1 Nullable (com.mongodb.lang.Nullable)1 Index (dev.morphia.annotations.Index)1 Reference (dev.morphia.annotations.Reference)1 Mapper (dev.morphia.mapping.Mapper)1 DocumentReader (dev.morphia.mapping.codec.reader.DocumentReader)1 ListReference (dev.morphia.mapping.experimental.ListReference)1 MapReference (dev.morphia.mapping.experimental.MapReference)1 MorphiaReference (dev.morphia.mapping.experimental.MorphiaReference)1