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; }
private DynamoDBMarshaller<T> marshaller() { try { return marshallerClass.newInstance(); } catch (final Exception e) { throw new DynamoDBMappingException("Unable to instantiate marshaller " + marshallerClass, 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); } }
<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); } }
@Override public void typeCheck(AttributeValue value, Method setter) { if ( value.getS() == null ) { throw new DynamoDBMappingException("Expected S in value " + value + " when invoking " + setter); } }
@Override public void typeCheck(AttributeValue value, Method setter) { if ( value.getNS() == null ) { throw new DynamoDBMappingException("Expected NS in value " + value + " when invoking " + setter); } }
@Override public void typeCheck(AttributeValue value, Method setter) { if ( value.getL() == null ) { throw new DynamoDBMappingException("Expected L in value " + value + " when invoking " + setter); } }
@Override public void typeCheck(AttributeValue value, Method setter) { if ( value.getN() == null ) { throw new DynamoDBMappingException("Expected N in value " + value + " when invoking " + setter); } }
@Override public void typeCheck(AttributeValue value, Method setter) { if ( value.getB() == null ) { throw new DynamoDBMappingException("Expected B in value " + value + " when invoking " + setter); } }
@Override public void typeCheck(AttributeValue value, Method setter) { if (value.getN() == null && value.getBOOL() == null) { throw new DynamoDBMappingException( "Expected either N or BOOL in value " + value + " when invoking " + setter); } }
@Override public S unconvert(final AttributeValue o) { final S value = get(o); if (value == null && o.isNULL() == null) { throw new DynamoDBMappingException("expected " + attributeType + " in value " + o); } return value; } }
private static final <T> Sequence<T> of(final Class<T> targetType) { for (final Sequences s : Sequences.values()) { if (s.scalar.is(targetType)) { return (Sequence<T>)s.sequence; } } throw new DynamoDBMappingException( "type [" + targetType + "] is not supported; allowed only " + Arrays.toString(Sequences.values()) ); } }
public <S,T> Builder with(Class<S> sourceType, Class<T> targetType, DynamoDBTypeConverter<? extends S,? extends T> converter) { if (Vector.SET.is(sourceType) || Vector.LIST.is(sourceType) || Vector.MAP.is(sourceType)) { throw new DynamoDBMappingException("type [" + sourceType + "] is not supported" + "; type-converter factory only supports scalar conversions"); } overrides.put(sourceType, targetType, converter); return this; }
private void putOrFlatten(FieldMap<V> annotations, Reflect<T,V> reflect, Method getter) { if (annotations.flattened()) { flatten((Class<T>)annotations.targetType(), annotations.attributes(), (Reflect<T,T>)reflect); } else { final Bean<T,V> bean = new Bean<T,V>(annotations, reflect, getter); if (put(bean.properties().attributeName(), bean) != null) { throw new DynamoDBMappingException("duplicate attribute name " + bean.properties().attributeName()); } } }