private static <T> T createObject(Class<T> clazz) { try { return clazz.newInstance(); } catch (InstantiationException e) { throw new DynamoDBMappingException( "Failed to instantiate new instance of class", e); } catch (IllegalAccessException e) { throw new DynamoDBMappingException( "Failed to instantiate new instance of class", e); } }
@Override public final T unconvert(final String object) { try { return mapper.readValue(object, targetType); } catch (final Exception e) { throw new DynamoDBMappingException("Unable to read JSON string", e); } } }
private static Class<?> resolveClass(Type type) { Type localType = type; if (localType instanceof ParameterizedType) { localType = ((ParameterizedType) type).getRawType(); } if (!(localType instanceof Class)) { throw new DynamoDBMappingException("Cannot resolve class for type " + type); } return (Class<?>) localType; }
@Override public final String convert(final T object) { try { return mapper.writeValueAsString(object); } catch (final Exception e) { throw new DynamoDBMappingException("Unable to write object to JSON", e); } }
/** * Returns the {@link DynamoDBTable} annotation of the class given, throwing * a runtime exception if it isn't annotated. */ <T> DynamoDBTable getTable(Class<T> clazz) { DynamoDBTable table = clazz.getAnnotation(DynamoDBTable.class); if ( table == null ) throw new DynamoDBMappingException("Class " + clazz + " must be annotated with " + DynamoDBTable.class); return table; }
@SuppressWarnings({ "rawtypes" }) private static DynamoDBMarshaller createUnmarshaller(Class<?> clazz) { try { return (DynamoDBMarshaller) clazz.newInstance(); } catch (InstantiationException e) { throw new DynamoDBMappingException( "Failed to instantiate custom marshaler for class " + clazz, e); } catch (IllegalAccessException e) { throw new DynamoDBMappingException( "Failed to instantiate custom marshaler for class " + clazz, e); } } }
private DynamoDBMarshaller<T> marshaller() { try { return marshallerClass.newInstance(); } catch (final Exception e) { throw new DynamoDBMappingException("Unable to instantiate marshaller " + marshallerClass, e); } } }
static <T> T newInstance(Class<T> targetType) { try { return targetType.newInstance(); } catch (final Exception e) { throw new DynamoDBMappingException("could not instantiate " + targetType, e); } } }
@SuppressWarnings("unchecked") private static DynamoDBMarshaller<Object> createMarshaller(Class<?> clazz) { try { return (DynamoDBMarshaller<Object>) clazz.newInstance(); } catch (InstantiationException e) { throw new DynamoDBMappingException( "Failed to instantiate custom marshaler for class " + clazz, e); } catch (IllegalAccessException e) { throw new DynamoDBMappingException( "Failed to instantiate custom marshaler for class " + clazz, e); } } }
@Override public V get(T object) { try { return (V)getter.invoke(object); } catch (final Exception e) { throw new DynamoDBMappingException("could not invoke " + getter + " on " + object.getClass(), e); } }
@Override public void set(T object, V value) { try { setter.invoke(object, value); } catch (final Exception e) { throw new DynamoDBMappingException("could not invoke " + setter + " on " + object.getClass() + " with value " + value + " of type " + (value == null ? null : value.getClass()), e); } }
/** * Gets the range key field model for the specified type. * @param <R> The range key type. * @return The range key field model. * @throws DynamoDBMappingException If the range key is not present. */ @SuppressWarnings("unchecked") public <R> DynamoDBMapperFieldModel<T,R> rangeKey() { final DynamoDBMapperFieldModel<T,R> field = (DynamoDBMapperFieldModel<T,R>)keys.get(RANGE); if (field == null) { throw new DynamoDBMappingException( targetType.getSimpleName() + "; no mapping for RANGE key" ); } return field; }
/** * Gets the field model for a given attribute. * @param <V> The field model's value type. * @param attributeName The attribute name. * @return The field model. */ @SuppressWarnings("unchecked") public <V> DynamoDBMapperFieldModel<T,V> field(final String attributeName) { final DynamoDBMapperFieldModel<T,V> field = (DynamoDBMapperFieldModel<T,V>)fields.get(attributeName); if (field == null) { throw new DynamoDBMappingException( targetType.getSimpleName() + "[" + attributeName + "]; no mapping for attribute by name" ); } return field; }
/** * Gets the hash key field model for the specified type. * @param <H> The hash key type. * @return The hash key field model. * @throws DynamoDBMappingException If the hash key is not present. */ @SuppressWarnings("unchecked") public <H> DynamoDBMapperFieldModel<T,H> hashKey() { final DynamoDBMapperFieldModel<T,H> field = (DynamoDBMapperFieldModel<T,H>)keys.get(HASH); if (field == null) { throw new DynamoDBMappingException( targetType.getSimpleName() + "; no mapping for HASH key" ); } return field; }
private ArgumentMarshaller getScalar(Method getter, Class<?> type) { ArgumentMarshaller marshaller = find(type, marshallers); if (marshaller == null) { String className = "?"; String methodName = "?"; if (getter != null) { className = getter.getDeclaringClass().toString(); methodName = getter.getName(); } throw new DynamoDBMappingException( "Cannot marshall return type " + type + " of method " + className + "." + methodName + " without a custom marshaler."); } return marshaller; }
private ArgumentMarshaller getSet(Method getter, Class<?> memberType) { ArgumentMarshaller marshaller = find(memberType, setMarshallers); if (marshaller == null) { String className = "?"; String methodName = "?"; if (getter != null) { className = getter.getDeclaringClass().toString(); methodName = getter.getName(); } throw new DynamoDBMappingException( "Cannot marshall return type Set<" + memberType + "> of method " + className + "." + methodName + " without a custom marshaller."); } return marshaller; } }
private ArgumentUnmarshaller getScalar(Method setter, Class<?> type) { ArgumentUnmarshaller unmarshaller = find(type, unmarshallers); String className = "?"; String methodName = "?"; if (setter != null) { className = setter.getDeclaringClass().toString(); methodName = setter.getName(); } if (unmarshaller == null) { throw new DynamoDBMappingException( "Cannot unmarshall to parameter type " + type + "of method " + className + "." + methodName + " without a custom unmarshaler."); } return unmarshaller; } }
<S,T> Set<T> unconvert(Collection<S> o, DynamoDBTypeConverter<S,T> scalar) { final Set<T> vector = new LinkedHashSet<T>(); for (final S s : o) { if (vector.add(scalar.unconvert(s)) == false) { throw new DynamoDBMappingException("duplicate value (" + s + ")"); } } return vector; }
@Override public void typeCheck(AttributeValue value, Method setter) { if ( value.getM() == null ) { throw new DynamoDBMappingException("Expected M in value " + value + " when invoking " + setter); } }
@Override public void typeCheck(AttributeValue value, Method setter) { if ( value.getBS() == null ) { throw new DynamoDBMappingException("Expected BS in value " + value + " when invoking " + setter); } }