Search in sources :

Example 16 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class ConventionAnnotationImpl method processCreatorAnnotation.

@SuppressWarnings("unchecked")
private <T> void processCreatorAnnotation(final ClassModelBuilder<T> classModelBuilder) {
    Class<T> clazz = classModelBuilder.getType();
    CreatorExecutable<T> creatorExecutable = null;
    for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
        if (isPublic(constructor.getModifiers()) && !constructor.isSynthetic()) {
            for (Annotation annotation : constructor.getDeclaredAnnotations()) {
                if (annotation.annotationType().equals(BsonCreator.class)) {
                    if (creatorExecutable != null) {
                        throw new CodecConfigurationException("Found multiple constructors annotated with @BsonCreator");
                    }
                    creatorExecutable = new CreatorExecutable<T>(clazz, (Constructor<T>) constructor);
                }
            }
        }
    }
    Class<?> bsonCreatorClass = clazz;
    boolean foundStaticBsonCreatorMethod = false;
    while (bsonCreatorClass != null && !foundStaticBsonCreatorMethod) {
        for (Method method : bsonCreatorClass.getDeclaredMethods()) {
            if (isStatic(method.getModifiers()) && !method.isSynthetic() && !method.isBridge()) {
                for (Annotation annotation : method.getDeclaredAnnotations()) {
                    if (annotation.annotationType().equals(BsonCreator.class)) {
                        if (creatorExecutable != null) {
                            throw new CodecConfigurationException("Found multiple constructors / methods annotated with @BsonCreator");
                        } else if (!bsonCreatorClass.isAssignableFrom(method.getReturnType())) {
                            throw new CodecConfigurationException(format("Invalid method annotated with @BsonCreator. Returns '%s', expected %s", method.getReturnType(), bsonCreatorClass));
                        }
                        creatorExecutable = new CreatorExecutable<T>(clazz, method);
                        foundStaticBsonCreatorMethod = true;
                    }
                }
            }
        }
        bsonCreatorClass = bsonCreatorClass.getSuperclass();
    }
    if (creatorExecutable != null) {
        List<BsonProperty> properties = creatorExecutable.getProperties();
        List<Class<?>> parameterTypes = creatorExecutable.getParameterTypes();
        List<Type> parameterGenericTypes = creatorExecutable.getParameterGenericTypes();
        if (properties.size() != parameterTypes.size()) {
            throw creatorExecutable.getError(clazz, "All parameters in the @BsonCreator method / constructor must be annotated " + "with a @BsonProperty.");
        }
        for (int i = 0; i < properties.size(); i++) {
            boolean isIdProperty = creatorExecutable.getIdPropertyIndex() != null && creatorExecutable.getIdPropertyIndex().equals(i);
            Class<?> parameterType = parameterTypes.get(i);
            Type genericType = parameterGenericTypes.get(i);
            PropertyModelBuilder<?> propertyModelBuilder = null;
            if (isIdProperty) {
                propertyModelBuilder = classModelBuilder.getProperty(classModelBuilder.getIdPropertyName());
            } else {
                BsonProperty bsonProperty = properties.get(i);
                // Find the property using write name and falls back to read name
                for (PropertyModelBuilder<?> builder : classModelBuilder.getPropertyModelBuilders()) {
                    if (bsonProperty.value().equals(builder.getWriteName())) {
                        propertyModelBuilder = builder;
                        break;
                    } else if (bsonProperty.value().equals(builder.getReadName())) {
                        // When there is a property that matches the read name of the parameter, save it but continue to look
                        // This is just in case there is another property that matches the write name.
                        propertyModelBuilder = builder;
                    }
                }
                // Support legacy options, when BsonProperty matches the actual POJO property name (e.g. method name or field name).
                if (propertyModelBuilder == null) {
                    propertyModelBuilder = classModelBuilder.getProperty(bsonProperty.value());
                }
                if (propertyModelBuilder == null) {
                    propertyModelBuilder = addCreatorPropertyToClassModelBuilder(classModelBuilder, bsonProperty.value(), parameterType);
                } else {
                    // If not using a legacy BsonProperty reference to the property set the write name to be the annotated name.
                    if (!bsonProperty.value().equals(propertyModelBuilder.getName())) {
                        propertyModelBuilder.writeName(bsonProperty.value());
                    }
                    tryToExpandToGenericType(parameterType, propertyModelBuilder, genericType);
                }
            }
            if (!propertyModelBuilder.getTypeData().isAssignableFrom(parameterType)) {
                throw creatorExecutable.getError(clazz, format("Invalid Property type for '%s'. Expected %s, found %s.", propertyModelBuilder.getWriteName(), propertyModelBuilder.getTypeData().getType(), parameterType));
            }
        }
        classModelBuilder.instanceCreatorFactory(new InstanceCreatorFactoryImpl<T>(creatorExecutable));
    }
}
Also used : Constructor(java.lang.reflect.Constructor) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) BsonType(org.bson.BsonType) Type(java.lang.reflect.Type) BsonProperty(org.bson.codecs.pojo.annotations.BsonProperty)

Example 17 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class BsonCodec method encode.

@Override
public void encode(final BsonWriter writer, final Bson value, final EncoderContext encoderContext) {
    try {
        BsonDocument bsonDocument = value.toBsonDocument(BsonDocument.class, registry);
        BSON_DOCUMENT_CODEC.encode(writer, bsonDocument, encoderContext);
    } catch (Exception e) {
        throw new CodecConfigurationException(format("Unable to encode a Bson implementation: %s", value), e);
    }
}
Also used : BsonDocument(org.bson.BsonDocument) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException)

Example 18 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class GeometryDecoderHelper method decodeLineString.

private static LineString decodeLineString(final BsonReader reader) {
    String type = null;
    List<Position> coordinates = null;
    CoordinateReferenceSystem crs = null;
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String key = reader.readName();
        if (key.equals("type")) {
            type = reader.readString();
        } else if (key.equals("coordinates")) {
            coordinates = decodeCoordinates(reader);
        } else if (key.equals("crs")) {
            crs = decodeCoordinateReferenceSystem(reader);
        } else {
            throw new CodecConfigurationException(format("Unexpected key '%s' found when decoding a GeoJSON Polygon", key));
        }
    }
    reader.readEndDocument();
    if (type == null) {
        throw new CodecConfigurationException("Invalid LineString, document contained no type information.");
    } else if (!type.equals("LineString")) {
        throw new CodecConfigurationException(format("Invalid LineString, found type '%s'.", type));
    } else if (coordinates == null) {
        throw new CodecConfigurationException("Invalid LineString, missing coordinates.");
    }
    return crs != null ? new LineString(crs, coordinates) : new LineString(coordinates);
}
Also used : Position(com.mongodb.client.model.geojson.Position) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString) CoordinateReferenceSystem(com.mongodb.client.model.geojson.CoordinateReferenceSystem) NamedCoordinateReferenceSystem(com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem)

Example 19 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class GeometryDecoderHelper method decodeMultiLineString.

private static MultiLineString decodeMultiLineString(final BsonReader reader) {
    String type = null;
    List<List<Position>> coordinates = null;
    CoordinateReferenceSystem crs = null;
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String key = reader.readName();
        if (key.equals("type")) {
            type = reader.readString();
        } else if (key.equals("coordinates")) {
            coordinates = decodeMultiCoordinates(reader);
        } else if (key.equals("crs")) {
            crs = decodeCoordinateReferenceSystem(reader);
        } else {
            throw new CodecConfigurationException(format("Unexpected key '%s' found when decoding a GeoJSON Polygon", key));
        }
    }
    reader.readEndDocument();
    if (type == null) {
        throw new CodecConfigurationException("Invalid MultiLineString, document contained no type information.");
    } else if (!type.equals("MultiLineString")) {
        throw new CodecConfigurationException(format("Invalid MultiLineString, found type '%s'.", type));
    } else if (coordinates == null) {
        throw new CodecConfigurationException("Invalid MultiLineString, missing coordinates.");
    }
    return crs != null ? new MultiLineString(crs, coordinates) : new MultiLineString(coordinates);
}
Also used : MultiLineString(com.mongodb.client.model.geojson.MultiLineString) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) ArrayList(java.util.ArrayList) List(java.util.List) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString) CoordinateReferenceSystem(com.mongodb.client.model.geojson.CoordinateReferenceSystem) NamedCoordinateReferenceSystem(com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem)

Example 20 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class GeometryDecoderHelper method decodeCoordinateReferenceSystem.

@Nullable
static CoordinateReferenceSystem decodeCoordinateReferenceSystem(final BsonReader reader) {
    String crsName = null;
    validateIsDocument(reader);
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String name = reader.readName();
        if (name.equals("type")) {
            String type = reader.readString();
            if (!type.equals("name")) {
                throw new CodecConfigurationException(format("Unsupported CoordinateReferenceSystem '%s'.", type));
            }
        } else if (name.equals("properties")) {
            crsName = decodeCoordinateReferenceSystemProperties(reader);
        } else {
            throw new CodecConfigurationException(format("Found invalid key '%s' in the CoordinateReferenceSystem.", name));
        }
    }
    reader.readEndDocument();
    return crsName != null ? new NamedCoordinateReferenceSystem(crsName) : null;
}
Also used : NamedCoordinateReferenceSystem(com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString) Nullable(com.mongodb.lang.Nullable)

Aggregations

CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)26 LineString (com.mongodb.client.model.geojson.LineString)11 MultiLineString (com.mongodb.client.model.geojson.MultiLineString)11 NamedCoordinateReferenceSystem (com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem)8 CoordinateReferenceSystem (com.mongodb.client.model.geojson.CoordinateReferenceSystem)7 Position (com.mongodb.client.model.geojson.Position)5 ArrayList (java.util.ArrayList)4 BsonDocument (org.bson.BsonDocument)4 Document (org.bson.Document)4 MultiPoint (com.mongodb.client.model.geojson.MultiPoint)3 MultiPolygon (com.mongodb.client.model.geojson.MultiPolygon)3 PolygonCoordinates (com.mongodb.client.model.geojson.PolygonCoordinates)3 BsonBinary (org.bson.BsonBinary)3 BsonReaderMark (org.bson.BsonReaderMark)3 BsonType (org.bson.BsonType)3 Test (org.junit.Test)3 GeometryCollection (com.mongodb.client.model.geojson.GeometryCollection)2 Point (com.mongodb.client.model.geojson.Point)2 Polygon (com.mongodb.client.model.geojson.Polygon)2 MappingException (dev.morphia.mapping.MappingException)2