Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2011-2015 Amazon Technologies, Inc.
   * 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:
  * 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.
Utilities for reflecting field or method annotations in a DynamoDB table POJO.
 class ReflectionUtils {
     static Object safeInvoke(
             Method method,
             Object object,
             Object... arguments) {
         try {
             return method.invoke(objectarguments);
         } catch (IllegalAccessException e) {
             throw new DynamoDBMappingException(
                     "Couldn't invoke " + methode);
         } catch (IllegalArgumentException e) {
             throw new DynamoDBMappingException(
                     "Couldn't invoke " + methode);
         } catch (InvocationTargetException e) {
             throw new DynamoDBMappingException(
                     "Couldn't invoke " + methode);

Returns the field name that corresponds to the given getter method, according to the Java naming convention.

getter The getter method.
forceCamelCase True if the returned field name should be in camel-case, i.e. the first letter is lower-cased.
     static String getFieldNameByGetter(Method getterboolean forceCamelCase) {
         String getterName = getter.getName();
         String fieldNameWithUpperCamelCase = "";
         if ( getterName.startsWith("get") ) {
             fieldNameWithUpperCamelCase = getterName.substring("get".length());
         } else if ( getterName.startsWith("is") ) {
             fieldNameWithUpperCamelCase = getterName.substring("is".length());
         if (fieldNameWithUpperCamelCase.length() == 0) {
             throw new DynamoDBMappingException(
                     "Getter must begin with 'get' or 'is', and the field name must contain at least one character.");
         if (forceCamelCase) {
             // Lowercase the first letter of the name
             return fieldNameWithUpperCamelCase.substring(0, 1).toLowerCase() + fieldNameWithUpperCamelCase.substring(1);
         } else {
             return fieldNameWithUpperCamelCase;

Returns the Field object for the specified field name declared in the specified class. Returns null if no such field can be found.

clazz The declaring class where the field will be reflected. This method will NOT attempt to reflect its superclass if such field is not found in this class.
fieldName The case-sensitive name of the field to be searched.
     static Field getClassFieldByName(Class<?> clazzString fieldName) {
         try {
             return clazz.getDeclaredField(fieldName);
         } catch (SecurityException e) {
            throw new DynamoDBMappingException(
                    "Denied access to the [" + fieldName + "] field in class [" + clazz + "]."e);
        } catch (NoSuchFieldException e) {
            return null;

This method searches for a specific type of annotation that is applied to either the specified getter method or its corresponding class field. Returns the annotation if it is found, else null.
    static <T extends Annotation> T getAnnotationFromGetterOrField(
            Method getterClass<T> annotationClass) {
        // Check annotation on the getter method
        T onGetter = getter.getAnnotation(annotationClass);
        if (onGetter != null) {
            return onGetter;
        // Check annotation on the corresponding field
        String fieldName = getFieldNameByGetter(gettertrue);
        // Only consider the field declared in the same class where getter is defined.
        Field field = getClassFieldByName(getter.getDeclaringClass(), fieldName);
        T onField = null;
        if (field != null) {
            onField = field.getAnnotation(annotationClass);
        return onField;

Returns true if an annotation for the specified type is found on the getter method or its corresponding class field.
    static <T extends Annotationboolean getterOrFieldHasAnnotation(
            Method getterClass<T> annotationClass) {
        return getAnnotationFromGetterOrField(getterannotationClass) != null;

Resolve the raw class for the given type.
    static Class<?> resolveClass(Type type) {
        Type localType = type;
        if (localType instanceof ParameterizedType) {
            localType = ((ParameterizedTypetype).getRawType();
        if (!(localType instanceof Class)) {
            throw new DynamoDBMappingException("Cannot resolve class for type "
                    + type);
        return (Class<?>) localType;
New to GrepCode? Check out our FAQ X