Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
*** THIS VERSION UPLOADED FOR USE WITH CEDAR-COMMON, TO AVOID DEPENDENCIES ON GOOGLE CODE-BASED MAVEN REPOSITORIES. *** The simplest convenient interface to the Google App Engine datastore
package com.googlecode.objectify.impl;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.persistence.Embedded;
import javax.persistence.Id;
import javax.persistence.Transient;
import com.google.appengine.api.datastore.Entity;
import com.googlecode.objectify.annotation.AlsoLoad;
import com.googlecode.objectify.annotation.Indexed;
import com.googlecode.objectify.annotation.Parent;
import com.googlecode.objectify.annotation.Serialized;
import com.googlecode.objectify.annotation.Unindexed;
import com.googlecode.objectify.condition.Always;
import com.googlecode.objectify.impl.save.Path;
/**
*/
public class TypeUtils
{
/** We do not persist fields with any of these modifiers */
static final int NOT_SAVED_MODIFIERS = Modifier.FINAL | Modifier.STATIC;
/** A map of the primitive types to their wrapper types */
static final Map, Class> PRIMITIVE_TO_WRAPPER = new HashMap, Class>();
static {
PRIMITIVE_TO_WRAPPER.put(boolean.class, Boolean.class);
PRIMITIVE_TO_WRAPPER.put(byte.class, Byte.class);
PRIMITIVE_TO_WRAPPER.put(short.class, Short.class);
PRIMITIVE_TO_WRAPPER.put(int.class, Integer.class);
PRIMITIVE_TO_WRAPPER.put(long.class, Long.class);
PRIMITIVE_TO_WRAPPER.put(float.class, Float.class);
PRIMITIVE_TO_WRAPPER.put(double.class, Double.class);
}
/**
* Simple container that groups the names associated with fields. Names
* will include the actual name of the field.
*/
public static class FieldMetadata
{
public Set names = new HashSet();
public Field field;
public FieldMetadata(Field f) { this.field = f; }
}
/**
* Simple container that groups the names associated with @AlsoLoad methods.
*/
public static class MethodMetadata
{
public Set names = new HashSet();
public Method method;
public MethodMetadata(Method meth) { this.method = meth; }
}
/**
* Throw an IllegalStateException if the class does not have a no-arg constructor.
*/
public static Constructor getNoArgConstructor(Class clazz)
{
try
{
Constructor ctor = clazz.getDeclaredConstructor(new Class[0]);
ctor.setAccessible(true);
return ctor;
}
catch (NoSuchMethodException e)
{
// lame there is no way to tell if the class is a nonstatic inner class
if (clazz.isMemberClass() || clazz.isAnonymousClass() || clazz.isLocalClass())
throw new IllegalStateException(clazz.getName() + " must be static and must have a no-arg constructor", e);
else
throw new IllegalStateException(clazz.getName() + " must have a no-arg constructor", e);
}
}
/**
* Gets a constructor that has the specified types of arguments.
* Throw an IllegalStateException if the class does not have such a constructor.
*/
public static Constructor getConstructor(Class clazz, Class... args)
{
try
{
Constructor ctor = clazz.getDeclaredConstructor(args);
ctor.setAccessible(true);
return ctor;
}
catch (NoSuchMethodException e)
{
throw new IllegalStateException(clazz.getName() + " has no constructor with args " + Arrays.toString(args), e);
}
}
/**
* @return true if the field can be saved (is persistable), false if it
* is static, final, @Transient, etc.
*/
public static boolean isSaveable(Field field)
{
return !field.isAnnotationPresent(Transient.class)
&& ((field.getModifiers() & NOT_SAVED_MODIFIERS) == 0)
&& !field.isSynthetic();
}
/**
* If getType() is an array or Collection, returns the component type - otherwise null
*/
public static Class getComponentType(Class type, Type genericType)
{
if (type.isArray())
{
return type.getComponentType();
}
else if (Collection.class.isAssignableFrom(type))
{
while (genericType instanceof Class)
genericType = ((Class) genericType).getGenericSuperclass();
if (genericType instanceof ParameterizedType)
{
Type actualTypeArgument = ((ParameterizedType) genericType).getActualTypeArguments()[0];
if (actualTypeArgument instanceof Class)
return (Class) actualTypeArgument;
else if (actualTypeArgument instanceof ParameterizedType)
return (Class) ((ParameterizedType) actualTypeArgument).getRawType();
else
return null;
}
else
{
return null;
}
}
else // not array or collection
{
return null;
}
}
/**
* Returns the value type, i.e. the argument {@code T} for a generic {@code Map}.
*/
public static Class getMapValueType(Type genericType)
{
if (genericType instanceof ParameterizedType)
{
Type[] actualTypeArguments = ((ParameterizedType) genericType).getActualTypeArguments();
if (actualTypeArguments.length != 2)
{
throw new IllegalStateException("Cannot handle subclass of Map with two type arguments");
}
Type keyType = actualTypeArguments[0];
Type valueType = actualTypeArguments[1];
if (!String.class.equals(keyType))
{
throw new IllegalStateException("Map key type must be string");
}
if (valueType instanceof Class)
{
return (Class) valueType;
}
else if (valueType instanceof ParameterizedType)
{
return (Class) ((ParameterizedType) valueType).getRawType();
}
}
throw new IllegalStateException("unexpected Map type " + genericType);
}
/**
* Extend a property path, adding a '.' separator but also checking
* for the first element.
*/
public static String extendPropertyPath(String prefix, String name)
{
if (prefix == null || prefix.length() == 0)
return name;
else
return prefix + '.' + name;
}
/**
*
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.
*
* @param collectionField is a Collection-derived field on the pojo.
* @param onPojo is the object whose field should be set
*/
public static Collection