private void checkHasAnExecutable() { if (constructor == null && method == null) { throw new CodecConfigurationException(format("Cannot find a public constructor for '%s'.", clazz.getSimpleName())); } }
@Override public T decode(final BsonReader reader, final DecoderContext decoderContext) { try { return pojoCodec.decode(reader, decoderContext); } catch (CodecConfigurationException e) { throw new CodecConfigurationException( format("An exception occurred when decoding using the AutomaticPojoCodec.%n" + "Decoding into a '%s' failed with the following exception:%n%n%s%n%n" + "A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.", pojoCodec.getEncoderClass().getSimpleName(), e.getMessage()), e); } }
private static CodecConfigurationException getError(final Class<?> clazz, final boolean isConstructor, final String msg) { return new CodecConfigurationException(format("Invalid @BsonCreator %s in %s. %s", isConstructor ? "constructor" : "method", clazz.getSimpleName(), msg)); }
@Override public void encode(final BsonWriter writer, final T value, final EncoderContext encoderContext) { try { pojoCodec.encode(writer, value, encoderContext); } catch (CodecConfigurationException e) { throw new CodecConfigurationException( format("An exception occurred when encoding using the AutomaticPojoCodec.%n" + "Encoding a %s: '%s' failed with the following exception:%n%n%s%n%n" + "A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.", getEncoderClass().getSimpleName(), value, e.getMessage()), e); } }
private void checkForDuplicates(final String propertyType, final String propertyName, final Map<String, Integer> propertyNameMap, final String declaringClass) { if (propertyNameMap.containsKey(propertyName)) { throw new CodecConfigurationException(format("Duplicate %s named '%s' found in %s.", propertyType, propertyName, declaringClass)); } propertyNameMap.put(propertyName, 1); }
private <S> void encodeValue(final BsonWriter writer, final EncoderContext encoderContext, final PropertyModel<S> propertyModel, final S propertyValue) { if (propertyModel.shouldSerialize(propertyValue)) { writer.writeName(propertyModel.getReadName()); if (propertyValue == null) { writer.writeNull(); } else { try { encoderContext.encodeWithChildContext(propertyModel.getCachedCodec(), writer, propertyValue); } catch (CodecConfigurationException e) { throw new CodecConfigurationException(format("Failed to encode '%s'. Encoding '%s' errored with: %s", classModel.getName(), propertyModel.getReadName(), e.getMessage()), e); } } } }
public PropertyMetadata<T> addReadAnnotation(final Annotation annotation) { if (readAnnotations.containsKey(annotation.annotationType())) { throw new CodecConfigurationException(format("Read annotation %s for '%s' already exists in %s", annotation.annotationType(), name, declaringClassName)); } readAnnotations.put(annotation.annotationType(), annotation); return this; }
@SuppressWarnings("unchecked") private <S> void decodePropertyModel(final BsonReader reader, final DecoderContext decoderContext, final InstanceCreator<T> instanceCreator, final String name, final PropertyModel<S> propertyModel) { if (propertyModel != null) { try { S value = null; if (reader.getCurrentBsonType() == BsonType.NULL) { reader.readNull(); } else { value = decoderContext.decodeWithChildContext(propertyModel.getCachedCodec(), reader); } if (propertyModel.isWritable()) { instanceCreator.set(value, propertyModel); } } catch (BsonInvalidOperationException e) { throw new CodecConfigurationException(format("Failed to decode '%s'. Decoding '%s' errored with: %s", classModel.getName(), name, e.getMessage()), e); } catch (CodecConfigurationException e) { throw new CodecConfigurationException(format("Failed to decode '%s'. Decoding '%s' errored with: %s", classModel.getName(), name, e.getMessage()), e); } } else { if (LOGGER.isTraceEnabled()) { LOGGER.trace(format("Found property not present in the ClassModel: %s", name)); } reader.skipValue(); } }
private Map<String, T> getInstance() { if (encoderClass.isInterface()) { return new HashMap<String, T>(); } try { return encoderClass.getDeclaredConstructor().newInstance(); } catch (final Exception e) { throw new CodecConfigurationException(e.getMessage(), e); } } }
public PropertyMetadata<T> addWriteAnnotation(final Annotation annotation) { if (writeAnnotations.containsKey(annotation.annotationType())) { throw new CodecConfigurationException(format("Write annotation %s for '%s' already exists in %s", annotation.annotationType(), name, declaringClassName)); } writeAnnotations.put(annotation.annotationType(), annotation); return this; }
@Override public NamedCoordinateReferenceSystem decode(final BsonReader reader, final DecoderContext decoderContext) { CoordinateReferenceSystem crs = decodeCoordinateReferenceSystem(reader); if (crs == null || !(crs instanceof NamedCoordinateReferenceSystem)) { throw new CodecConfigurationException("Invalid NamedCoordinateReferenceSystem."); } return (NamedCoordinateReferenceSystem) crs; } }
private Collection<T> getInstance() { if (encoderClass.isInterface()) { if (encoderClass.isAssignableFrom(ArrayList.class)) { return new ArrayList<T>(); } else if (encoderClass.isAssignableFrom(HashSet.class)) { return new HashSet<T>(); } else { throw new CodecConfigurationException(format("Unsupported Collection interface of %s!", encoderClass.getName())); } } try { return encoderClass.getDeclaredConstructor().newInstance(); } catch (final Exception e) { throw new CodecConfigurationException(e.getMessage(), e); } } }
private static void validateIsArray(final BsonReader reader) { if (reader.getCurrentBsonType() != BsonType.ARRAY) { throw new CodecConfigurationException("Invalid BsonType expecting an Array"); } }
@SuppressWarnings("unchecked") T getInstance(final Object[] params) { checkHasAnExecutable(); try { if (constructor != null) { return constructor.newInstance(params); } else { return (T) method.invoke(clazz, params); } } catch (Exception e) { throw new CodecConfigurationException(e.getMessage(), e); } }
@SuppressWarnings("unchecked") T getInstance() { checkHasAnExecutable(); try { if (constructor != null) { return constructor.newInstance(); } else { return (T) method.invoke(clazz); } } catch (Exception e) { throw new CodecConfigurationException(e.getMessage(), e); } }
private void constructInstanceAndProcessCachedValues() { try { newInstance = creatorExecutable.getInstance(params); } catch (Exception e) { throw new CodecConfigurationException(e.getMessage(), e); } for (Map.Entry<PropertyModel<?>, Object> entry : cachedValues.entrySet()) { setPropertyValue(entry.getKey(), entry.getValue()); } }
/** * Build a codec from {@link TypeAdapter}. Opposite of {@link #typeAdapterFromCodec(Codec)}. * * @param type type handled by this adapter * @param adapter existing adapter * @param <T> codec value type * @throws CodecConfigurationException if adapter is not supported * @return new instance of the codec which handles {@code type}. */ public static <T> Codec<T> codecFromTypeAdapter(Class<T> type, TypeAdapter<T> adapter) { if (isReflectiveTypeAdapter(adapter)) { throw new CodecConfigurationException(String.format("%s can't be build from %s " + "(for type %s)", TypeAdapterCodec.class.getSimpleName(), adapter.getClass().getName(), type.getName())); } return new TypeAdapterCodec<>(type, adapter); }
@SuppressWarnings("unchecked") // hopefully correct @Override public <T> Codec<T> get(Class<T> clazz) { if (!Somebody.class.isAssignableFrom(clazz)) { throw new CodecConfigurationException("Not supported " + clazz); } return (Codec<T>) this; } }
@SuppressWarnings("unchecked") public <T> Codec<T> getOrThrow(final Class<T> clazz) { if (codecCache.containsKey(clazz)) { Optional<? extends Codec<?>> optionalCodec = codecCache.get(clazz); if (!optionalCodec.isEmpty()) { return (Codec<T>) optionalCodec.get(); } } throw new CodecConfigurationException(format("Can't find a codec for %s.", clazz)); } }
/** * Gets the Codec mapped to the given bson type. * * @param bsonType the non-null BsonType * @return the non-null Codec */ public Codec<?> get(final BsonType bsonType) { Codec<?> codec = codecs[bsonType.getValue()]; if (codec == null) { Class<?> clazz = bsonTypeClassMap.get(bsonType); if (clazz == null) { throw new CodecConfigurationException(format("No class mapped for BSON type %s.", bsonType)); } else { throw new CodecConfigurationException(format("Can't find a codec for %s.", clazz)); } } return codec; } }