
com.amazonaws.services.dynamodbv2.datamodeling.StandardMethodReflects Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-java-sdk-dynamodb Show documentation
Show all versions of aws-java-sdk-dynamodb Show documentation
The AWS Java SDK for Amazon DynamoDB module holds the client classes that are used for communicating with Amazon DynamoDB Service
/*
* Copyright 2016-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://aws.amazon.com/apache2.0
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and
* limitations under the License.
*/
package com.amazonaws.services.dynamodbv2.datamodeling;
import com.amazonaws.annotation.SdkInternalApi;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperFieldModel.Reflect;
import java.lang.reflect.Method;
/**
* Reflection assistant for {@link DynamoDBMapper}
*/
@SdkInternalApi
final class StandardMethodReflects {
/**
* Gets the method reflector for the given getter.
*/
static final Reflect of(final Method getter) {
Method setter = null;
try {
final String name = "set" + getter.getName().replaceFirst("^(get|is)","");
setter = getter.getDeclaringClass().getMethod(name, getter.getReturnType());
} catch (final Exception no) {}
return new MethodReflect(getter, setter);
}
/**
* Gets the method reflector for the given getter and declaring reflector.
*/
static final Reflect of(final Method getter, final Reflect declaring, final Class targetType) {
final Reflect reflect = StandardMethodReflects.of(getter);
return new DeclaringReflect(reflect, declaring, targetType);
}
/**
* Get/set reflection operations.
*/
static final class MethodReflect implements Reflect {
private final Method getter, setter;
/**
* Constructs the method reflector.
*/
private MethodReflect(final Method getter, final Method setter) {
this.getter = getter;
this.setter = setter;
}
/**
* {@inheritDoc}
*/
@Override
public V get(final T object) {
try {
return (V)getter.invoke(object);
} catch (final Exception e) {
throw new DynamoDBMappingException("could not invoke " + getter + " on " + object.getClass(), e);
}
}
/**
* {@inheritDoc}
*/
@Override
public void set(T object, final V value) {
try {
setter.invoke(object, value);
} catch (final Exception e) {
throw new DynamoDBMappingException("could not invoke " + setter + " on " + object.getClass(), e);
}
}
}
/**
* Get/set reflection operations with a declaring property.
*/
static final class DeclaringReflect implements Reflect {
private final Reflect reflect;
private final Reflect declaring;
private final Class targetType;
/**
* Constructs the declaring method reflector.
*/
private DeclaringReflect(final Reflect reflect, final Reflect declaring, final Class targetType) {
this.reflect = reflect;
this.declaring = declaring;
this.targetType = targetType;
}
/**
* {@inheritDoc}
*/
@Override
public final V get(final T object) {
final T declaringObject = declaring.get(object);
if (declaringObject == null) {
return null;
}
return reflect.get(declaringObject);
}
/**
* {@inheritDoc}
*/
@Override
public final void set(final T object, final V value) {
T declaringObject = declaring.get(object);
if (declaringObject == null) {
try {
declaringObject = targetType.newInstance();
} catch (final Exception e) {
throw new DynamoDBMappingException("could not instantiate " + targetType, e);
}
declaring.set(object, declaringObject);
}
reflect.set(declaringObject, value);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy