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:
   *
   *    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 java.util.List;
 import java.util.Map;
 
Reflection assistant for DynamoDBMapper
 
 
     /*
      * Several caches for performance. Collectively, they can make this class
      * over twice as fast.
      */
     private final Map<Class<?>, Collection<Method>> getterCache = new HashMap<Class<?>, Collection<Method>>();
     private final Map<Class<?>, MethodprimaryHashKeyGetterCache = new HashMap<Class<?>, Method>();
     private final Map<Class<?>, MethodprimaryRangeKeyGetterCache = new HashMap<Class<?>, Method>();
 
     /*
      * All caches keyed by a Method use the getter for a particular mapped
      * property
      */
     private final Map<MethodMethodsetterCache = new HashMap<MethodMethod>();
 
     @GuardedBy("readWriteLockAttrName")
     private final Map<MethodStringattributeNameCache = new HashMap<MethodString>();
 
     private final Map<MethodBooleanversionAttributeGetterCache = new HashMap<MethodBoolean>();
     private final Map<MethodBooleanautoGeneratedKeyGetterCache = new HashMap<MethodBoolean>();
 
     private final ReadLock readLockAttrName = .readLock();
     private final WriteLock writeLockAttrName = .writeLock();

    
Returns the set of getter methods which are relevant when marshalling or unmarshalling an object.
 
     Collection<MethodgetRelevantGetters(Class<?> clazz) {
         synchronized () {
             if ( !.containsKey(clazz) ) {
                 List<MethodrelevantGetters = findRelevantGetters(clazz);
                 .put(clazzrelevantGetters);
             }
             return .get(clazz);
         }
     }
 
     static List<MethodfindRelevantGetters(Class<?> clazz) {
         List<MethodrelevantGetters = new LinkedList<Method>();
         for ( Method m : clazz.getMethods() ) {
             if ( isRelevantGetter(m) ) {
                 relevantGetters.add(m);
             }
         }
         return relevantGetters;
     }

    
Returns whether the method given is a getter method we should serialize / deserialize to the service. The method must begin with "get" or "is", have no arguments, belong to a class that declares its table, and not be marked ignored.
 
     private static boolean isRelevantGetter(Method m) {
         return (m.getName().startsWith("get") || m.getName().startsWith("is"))
                 && m.getParameterTypes().length == 0
                 && ! (m.isBridge() || m.isSynthetic())
                 && isDocumentType(m.getDeclaringClass())
                 && !ReflectionUtils.getterOrFieldHasAnnotation(mDynamoDBIgnore.class);
     }
 
     private static boolean isDocumentType(Class<?> clazz) {
         return (clazz.getAnnotation(DynamoDBTable.class) != null)
                 || (clazz.getAnnotation(DynamoDBDocument.class) != null);
     }

    
Returns the annotated DynamoDBRangeKey getter for the class given, or null if the class doesn't have one.
    <T> Method getPrimaryRangeKeyGetter(Class<T> clazz) {
        synchronized () {
            if ( !.containsKey(clazz) ) {
                Method rangeKeyMethod = null;
                for ( Method method : getRelevantGetters(clazz) ) {
                    if ( method.getParameterTypes().length == 0
                            && ReflectionUtils.getterOrFieldHasAnnotation(methodDynamoDBRangeKey.class)) {
                        rangeKeyMethod = method;
                        break;
                    }
                }
                .put(clazzrangeKeyMethod);
            }
            return .get(clazz);
        }
    }

    
Returns all annotated DynamoDBHashKey and DynamoDBRangeKey getters for the class given, throwing an exception if there isn't one. TODO: caching
    <T> Collection<MethodgetPrimaryKeyGetters(Class<T> clazz) {
        List<MethodkeyGetters = new LinkedList<Method>();
        for (Method getter : getRelevantGetters(clazz)) {
            if (ReflectionUtils.getterOrFieldHasAnnotation(getterDynamoDBHashKey.class)
                    || ReflectionUtils.getterOrFieldHasAnnotation(getterDynamoDBRangeKey.class)) {
                keyGetters.add(getter);
            }
        }
        return keyGetters;
    }


    
Returns the annotated DynamoDBHashKey getter for the class given, throwing an exception if there isn't one.
    <T> Method getPrimaryHashKeyGetter(Class<T> clazz) {
        Method hashKeyMethod;
        synchronized () {
            if ( !.containsKey(clazz) ) {
                for ( Method method : getRelevantGetters(clazz) ) {
                    if ( method.getParameterTypes().length == 0
                            && ReflectionUtils.getterOrFieldHasAnnotation(methodDynamoDBHashKey.class)) {
                        .put(clazzmethod);
                        break;
                    }
                }
            }
            hashKeyMethod = .get(clazz);
        }
        if ( hashKeyMethod == null ) {
            throw new DynamoDBMappingException("Public, zero-parameter hash key property must be annotated with "
                    + DynamoDBHashKey.class);
        }
        return hashKeyMethod;
    }

    
Returns the 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;
    }

    
Returns the attribute name corresponding to the given getter method.
    String getAttributeName(Method getter) {
        String attributeName;
        .lock();
        try {
            attributeName = .get(getter);
        } finally {
            .unlock();
        }
        if ( attributeName != null )
            return attributeName;
        DynamoDBHashKey hashKeyAnnotation = ReflectionUtils.getAnnotationFromGetterOrField(getterDynamoDBHashKey.class);
        if ( hashKeyAnnotation != null ) {
            attributeName = hashKeyAnnotation.attributeName();
            if ( attributeName != null && attributeName.length() > 0 )
                return cacheAttributeName(getterattributeName);
        }
        DynamoDBIndexHashKey indexHashKey = ReflectionUtils.getAnnotationFromGetterOrField(getterDynamoDBIndexHashKey.class);
        if ( indexHashKey != null ) {
            attributeName = indexHashKey.attributeName();
            if ( attributeName != null && attributeName.length() > 0 )
                return cacheAttributeName(getterattributeName);
        }
        DynamoDBRangeKey rangeKey = ReflectionUtils.getAnnotationFromGetterOrField(getterDynamoDBRangeKey.class);
        if ( rangeKey != null ) {
            attributeName = rangeKey.attributeName();
            if ( attributeName != null && attributeName.length() > 0 )
                return cacheAttributeName(getterattributeName);
        }
        DynamoDBIndexRangeKey indexRangeKey = ReflectionUtils.getAnnotationFromGetterOrField(getterDynamoDBIndexRangeKey.class);
        if ( indexRangeKey != null ) {
            attributeName = indexRangeKey.attributeName();
            if ( attributeName != null && attributeName.length() > 0 )
                return cacheAttributeName(getterattributeName);
        }
        DynamoDBAttribute attribute = ReflectionUtils.getAnnotationFromGetterOrField(getterDynamoDBAttribute.class);
        if ( attribute != null ) {
            attributeName = attribute.attributeName();
            if ( attributeName != null && attributeName.length() > 0 )
                return cacheAttributeName(getterattributeName);
        }
        DynamoDBVersionAttribute version = ReflectionUtils.getAnnotationFromGetterOrField(getterDynamoDBVersionAttribute.class);
        if ( version != null ) {
            attributeName = version.attributeName();
            if ( attributeName != null && attributeName.length() > 0 )
                return cacheAttributeName(getterattributeName);
        }
        // Default to the camel-cased field name of the getter method, inferred
        // according to the Java naming convention.
        attributeName = ReflectionUtils.getFieldNameByGetter(gettertrue);
        return cacheAttributeName(getterattributeName);
    }
    private String cacheAttributeName(Method getterString attributeName) {
        .lock();
        try {
            .put(getterattributeName);
        } finally {
            .unlock();
        }
        return attributeName;
    }

    
Returns the setter corresponding to the getter given, or null if no such setter exists.
    Method getSetter(Method getter) {
        synchronized () {
            if ( !.containsKey(getter) ) {
                String fieldName = ReflectionUtils.getFieldNameByGetter(getterfalse);
                String setterName = "set" + fieldName;
                Method setter = null;
                try {
                    setter = getter.getDeclaringClass().getMethod(setterNamegetter.getReturnType());
                } catch ( NoSuchMethodException e ) {
                    throw new DynamoDBMappingException("Expected a public, one-argument method called " + setterName
                            + " on class " + getter.getDeclaringClass(), e);
                } catch ( SecurityException e ) {
                    throw new DynamoDBMappingException("No access to public, one-argument method called " + setterName
                            + " on class " + getter.getDeclaringClass(), e);
                }
                .put(gettersetter);
            }
            return .get(getter);
        }
    }

    
Returns whether the method given is an annotated, no-args getter of a version attribute.
    boolean isVersionAttributeGetter(Method getter) {
        synchronized () {
            if ( !.containsKey(getter) ) {
                .put(
                        getter,
                        getter.getName().startsWith("get") && getter.getParameterTypes().length == 0
                                && ReflectionUtils.getterOrFieldHasAnnotation(getterDynamoDBVersionAttribute.class));
            }
            return .get(getter);
        }
    }

    
Returns whether the method given is an assignable key getter.
    boolean isAssignableKey(Method getter) {
        synchronized () {
            if ( !.containsKey(getter) ) {
                .put(
                        getter,
                        ReflectionUtils.getterOrFieldHasAnnotation(getterDynamoDBAutoGeneratedKey.class)
                                && ( ReflectionUtils.getterOrFieldHasAnnotation(getterDynamoDBHashKey.class) ||
                                     ReflectionUtils.getterOrFieldHasAnnotation(getterDynamoDBRangeKey.class)));
            }
            return .get(getter);
        }
    }

    
Returns the name of the primary hash key.
    String getPrimaryHashKeyName(Class<?> clazz) {
        return getAttributeName(getPrimaryHashKeyGetter(clazz));
    }

    
Returns the name of the primary range key, or null if the table does not one.
    String getPrimaryRangeKeyName(Class<?> clazz) {
        Method primaryRangeKeyGetter = getPrimaryHashKeyGetter(clazz);
        return primaryRangeKeyGetter == null ?
                null
                :
                getAttributeName(getPrimaryRangeKeyGetter(clazz));
    }

    
Returns true if and only if the specified class has declared a primary range key.
    boolean hasPrimaryRangeKey(Class<?> clazz) {
        return getPrimaryRangeKeyGetter(clazz) != null;
    }
New to GrepCode? Check out our FAQ X