com.googlecode.objectify.impl
Class TypeUtils

java.lang.Object
  extended by com.googlecode.objectify.impl.TypeUtils

public class TypeUtils
extends java.lang.Object


Nested Class Summary
static class TypeUtils.FieldMetadata
          Simple container that groups the names associated with fields.
static class TypeUtils.MethodMetadata
          Simple container that groups the names associated with @AlsoLoad methods.
 
Constructor Summary
TypeUtils()
           
 
Method Summary
static java.lang.reflect.Type climbTypeHierarchy(java.lang.Class<?> here, java.lang.Class<?> parentClass)
          Climb the type hierarchy in the direction of parentClass.
static java.util.Collection<java.lang.Object> createCollection(java.lang.Class<?> type, int size)
          Create a collection of the appropriate type.
static java.lang.String extendPropertyPath(java.lang.String prefix, java.lang.String name)
          Extend a property path, adding a '.' separator but also checking for the first element.
static java.lang.Object field_get(java.lang.reflect.Field field, java.lang.Object obj)
          Checked exceptions are LAME.
static void field_set(java.lang.reflect.Field field, java.lang.Object obj, java.lang.Object value)
          Checked exceptions are LAME.
static java.util.List<TypeUtils.MethodMetadata> getAlsoLoadMethods(java.lang.Class<?> clazz)
          Get all the methods that are appropriate for saving into on this class and all superclasses.
static java.lang.Class<?> getClass(java.lang.reflect.Type type)
          Get the underlying class for a type, or null if the type is a variable type.
static java.lang.Class<?> getComponentType(java.lang.Class<?> type, java.lang.reflect.Type genericType)
          If getType() is an array or Collection, returns the component type - otherwise null
static
<T> java.lang.reflect.Constructor<T>
getConstructor(java.lang.Class<T> clazz, java.lang.Class<?>... args)
          Gets a constructor that has the specified types of arguments.
static java.lang.reflect.Field getDeclaredField(java.lang.Class<?> clazz, java.lang.String fieldName)
          A recursive version of Class.getDeclaredField, goes up the hierarchy looking
static java.lang.Class<?> getMapValueType(java.lang.reflect.Type genericType)
          Returns the value type, i.e.
static
<T> java.lang.reflect.Constructor<T>
getNoArgConstructor(java.lang.Class<T> clazz)
          Throw an IllegalStateException if the class does not have a no-arg constructor.
static java.util.Set<java.lang.Integer> getNullIndexes(com.google.appengine.api.datastore.Entity entity, java.lang.String pathBase)
          Gets the embedded null indexes property in an entity.
static java.lang.String getNullIndexPath(Path pathBase)
           
static java.lang.String getNullIndexPath(java.lang.String pathBase)
           
static java.util.List<TypeUtils.FieldMetadata> getPesistentFields(java.lang.Class<?> clazz, boolean embedded)
          Get all the persistent fields on a class, checking the superclasses as well.
static
<T> java.util.List<java.lang.Class<?>>
getTypeArguments(java.lang.Class<T> baseClass, java.lang.Class<? extends T> childClass)
          Get the actual type arguments a child class has used to extend a generic base class.
static boolean isArrayOrCollection(java.lang.Class<?> clazz)
           
static boolean isAssignableFrom(java.lang.Class<?> to, java.lang.Class<?> from)
          Just like Class.isAssignableFrom(), but does the right thing when considering autoboxing.
static java.lang.Boolean isClassIndexed(java.lang.Class<?> clazz)
          Inspects a specific class and determines if there is a default indexed state; @Indexed = true, @Unindexed = false, nothing specified = null.
static boolean isEmbedded(java.lang.reflect.Field field)
          Determines if the field is embedded or not.
static boolean isSaveable(java.lang.reflect.Field field)
           
static
<T> T
newInstance(java.lang.Class<T> clazz)
          Checked exceptions are LAME.
static
<T> T
newInstance(java.lang.reflect.Constructor<T> ctor, java.lang.Object... params)
          Checked exceptions are LAME.
static java.util.Collection<java.lang.Object> prepareCollection(java.lang.Object onPojo, Wrapper collectionField, int size)
          Prepare a collection of the appropriate type and place it on the pojo's field.
static void setNullIndexes(com.google.appengine.api.datastore.Entity entity, Path subPath, java.util.Collection<java.lang.Integer> value)
          Sets the embedded null indexes property in an entity, which tracks which elements of a collection are null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TypeUtils

public TypeUtils()
Method Detail

getNoArgConstructor

public static <T> java.lang.reflect.Constructor<T> getNoArgConstructor(java.lang.Class<T> clazz)
Throw an IllegalStateException if the class does not have a no-arg constructor.


getConstructor

public static <T> java.lang.reflect.Constructor<T> getConstructor(java.lang.Class<T> clazz,
                                                                  java.lang.Class<?>... args)
Gets a constructor that has the specified types of arguments. Throw an IllegalStateException if the class does not have such a constructor.


isSaveable

public static boolean isSaveable(java.lang.reflect.Field field)
Returns:
true if the field can be saved (is persistable), false if it is static, final, @Transient, etc.

getComponentType

public static java.lang.Class<?> getComponentType(java.lang.Class<?> type,
                                                  java.lang.reflect.Type genericType)
If getType() is an array or Collection, returns the component type - otherwise null


getMapValueType

public static java.lang.Class<?> getMapValueType(java.lang.reflect.Type genericType)
Returns the value type, i.e. the argument T for a generic Map<String, T>.


extendPropertyPath

public static java.lang.String extendPropertyPath(java.lang.String prefix,
                                                  java.lang.String name)
Extend a property path, adding a '.' separator but also checking for the first element.


prepareCollection

public static java.util.Collection<java.lang.Object> prepareCollection(java.lang.Object onPojo,
                                                                       Wrapper collectionField,
                                                                       int size)

Prepare a collection of the appropriate type and place it on the pojo's field. The rules are described in createCollection(), with the addition that any collection already found in the object's field will be returned as-is; a new collection will not be created.

Parameters:
collectionField - is a Collection-derived field on the pojo.
onPojo - is the object whose field should be set

createCollection

public static java.util.Collection<java.lang.Object> createCollection(java.lang.Class<?> type,
                                                                      int size)

Create a collection of the appropriate type. The rules are thus:

Parameters:
type - is a Collection-derived type

setNullIndexes

public static void setNullIndexes(com.google.appengine.api.datastore.Entity entity,
                                  Path subPath,
                                  java.util.Collection<java.lang.Integer> value)

Sets the embedded null indexes property in an entity, which tracks which elements of a collection are null. For a base of "foo.bar", the state property will be "foo.bar^null". The value, if present, will be a list of indexes in an embedded collection which are null.

If there are no nulls, this property does not need to be set.


getNullIndexes

public static java.util.Set<java.lang.Integer> getNullIndexes(com.google.appengine.api.datastore.Entity entity,
                                                              java.lang.String pathBase)

Gets the embedded null indexes property in an entity.

Returns:
null if there is no such property
See Also:
setNullIndexes(Entity, Path, Collection)

getNullIndexPath

public static java.lang.String getNullIndexPath(Path pathBase)
Returns:
the path where you will find the null indexes for a base path

getNullIndexPath

public static java.lang.String getNullIndexPath(java.lang.String pathBase)
Returns:
the path where you will find the null indexes for a base path

isArrayOrCollection

public static boolean isArrayOrCollection(java.lang.Class<?> clazz)
Returns:
true if clazz is an array type or a collection type

isEmbedded

public static boolean isEmbedded(java.lang.reflect.Field field)
Determines if the field is embedded or not. Today this checks for an @Embedded annotation, but in the future it could check the type (or component type) is one of the natively persistable classes.

Returns:
true if field is an embedded class, collection, or array.

newInstance

public static <T> T newInstance(java.lang.Class<T> clazz)
Checked exceptions are LAME.


newInstance

public static <T> T newInstance(java.lang.reflect.Constructor<T> ctor,
                                java.lang.Object... params)
Checked exceptions are LAME.


field_get

public static java.lang.Object field_get(java.lang.reflect.Field field,
                                         java.lang.Object obj)
Checked exceptions are LAME.


field_set

public static void field_set(java.lang.reflect.Field field,
                             java.lang.Object obj,
                             java.lang.Object value)
Checked exceptions are LAME.


getPesistentFields

public static java.util.List<TypeUtils.FieldMetadata> getPesistentFields(java.lang.Class<?> clazz,
                                                                         boolean embedded)
Get all the persistent fields on a class, checking the superclasses as well.

Parameters:
embedded - will add fields marked with @Id and @Parent to the persistent fields. You only do this when you are embedding entities within other entities.
Returns:
the fields we load and save, *not* including @Id & @Parent fields. All fields will be set accessable, and returned in order starting with superclass fields.

getAlsoLoadMethods

public static java.util.List<TypeUtils.MethodMetadata> getAlsoLoadMethods(java.lang.Class<?> clazz)
Get all the methods that are appropriate for saving into on this class and all superclasses. Validates that @AlsoLoad methods are properly created (one parameter, not @Embedded).

Returns:
all the correctly specified @AlsoLoad and @OldName methods. Methods will be set accessable. Key will be the immediate name in the annotation.

getDeclaredField

public static java.lang.reflect.Field getDeclaredField(java.lang.Class<?> clazz,
                                                       java.lang.String fieldName)
                                                throws java.lang.NoSuchFieldException
A recursive version of Class.getDeclaredField, goes up the hierarchy looking

Throws:
java.lang.NoSuchFieldException

getClass

public static java.lang.Class<?> getClass(java.lang.reflect.Type type)
Get the underlying class for a type, or null if the type is a variable type. See http://www.artima.com/weblogs/viewpost.jsp?thread=208860


getTypeArguments

public static <T> java.util.List<java.lang.Class<?>> getTypeArguments(java.lang.Class<T> baseClass,
                                                                      java.lang.Class<? extends T> childClass)
Get the actual type arguments a child class has used to extend a generic base class. See http://www.artima.com/weblogs/viewpost.jsp?thread=208860 This has additionally been modified to handle base interfaces.

Parameters:
baseClass - the base class (or interface)
childClass - the child class
Returns:
a list of the raw classes for the actual type arguments.

climbTypeHierarchy

public static java.lang.reflect.Type climbTypeHierarchy(java.lang.Class<?> here,
                                                        java.lang.Class<?> parentClass)
Climb the type hierarchy in the direction of parentClass. Gets the immediate superclass/superinterface.

Returns:
null if parentClass is not in the parent hierarchy of here.

isAssignableFrom

public static boolean isAssignableFrom(java.lang.Class<?> to,
                                       java.lang.Class<?> from)
Just like Class.isAssignableFrom(), but does the right thing when considering autoboxing.


isClassIndexed

public static java.lang.Boolean isClassIndexed(java.lang.Class<?> clazz)
Inspects a specific class and determines if there is a default indexed state; @Indexed = true, @Unindexed = false, nothing specified = null.



Copyright © 2011 Jeff Schnitzer and a gang of pirates. All Rights Reserved. Build version: 3.1