Example usage for java.lang.reflect Field getModifiers

List of usage examples for java.lang.reflect Field getModifiers

Introduction

In this page you can find the example usage for java.lang.reflect Field getModifiers.

Prototype

public int getModifiers() 

Source Link

Document

Returns the Java language modifiers for the field represented by this Field object, as an integer.

Usage

From source file:org.apache.airavata.sharing.registry.server.SharingRegistryServerHandler.java

private <T> T getUpdatedObject(T oldEntity, T newEntity) throws SharingRegistryException {
    Field[] newEntityFields = newEntity.getClass().getDeclaredFields();
    Hashtable newHT = fieldsToHT(newEntityFields, newEntity);

    Class oldEntityClass = oldEntity.getClass();
    Field[] oldEntityFields = oldEntityClass.getDeclaredFields();

    for (Field field : oldEntityFields) {
        if (!Modifier.isFinal(field.getModifiers())) {
            field.setAccessible(true);/*from   ww w. j av  a 2 s . c  om*/
            Object o = newHT.get(field.getName());
            if (o != null) {
                Field f = null;
                try {
                    f = oldEntityClass.getDeclaredField(field.getName());
                    f.setAccessible(true);
                    logger.debug("setting " + f.getName());
                    f.set(oldEntity, o);
                } catch (Exception e) {
                    throw new SharingRegistryException(e.getMessage());
                }
            }
        }
    }
    return oldEntity;
}

From source file:hd3gtv.mydmam.db.orm.CassandraOrm.java

private ArrayList<Field> getOrmobjectUsableFields() {
    ArrayList<Field> result = new ArrayList<Field>();

    Field[] fields = this.reference.getFields();
    Field field;
    int mod;/*from w ww .j av a  2 s  .  co m*/
    for (int pos_df = 0; pos_df < fields.length; pos_df++) {
        field = fields[pos_df];
        if (field.isAnnotationPresent(Transient.class)) {
            /**
             * Is transient ?
             */
            continue;
        }
        if (field.getName().equals("key")) {
            /**
             * Not this (primary key)
             */
            continue;
        }
        mod = field.getModifiers();

        if ((mod & Modifier.PROTECTED) != 0)
            continue;
        if ((mod & Modifier.PRIVATE) != 0)
            continue;
        if ((mod & Modifier.ABSTRACT) != 0)
            continue;
        if ((mod & Modifier.STATIC) != 0)
            continue;
        if ((mod & Modifier.FINAL) != 0)
            continue;
        if ((mod & Modifier.TRANSIENT) != 0)
            continue;
        if ((mod & Modifier.INTERFACE) != 0)
            continue;

        try {
            result.add(field);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }

    return result;
}

From source file:com.pastekit.twist.gae.GaeMarshaller.java

/**
 *
 * Create entity objects that can be persisted into the GAE Datastore,
 * including its Parent-Child relationships (if necessary).
 *
 * @param parent parent of the generated entity or Entities
 * @param instance to marshall/*w  w w.  j av  a  2  s  .c  om*/
 * @return
 */
@Override
public IdentityHashMap marshall(Key parent, Object instance) {
    if (instance == null) {
        throw new RuntimeException("Object cannot be null");
    }
    Entity e = null;
    // Its possible that a Entity to be saved without id and just a parent key
    if (parent != null && hasNoIdField(instance)) {
        String kind = getKindOf(instance);
        e = new Entity(kind, parent);
    } else {
        Key key = createKeyFrom(parent, instance); // inspect kind and create key
        e = new Entity(key);
    }

    boolean indexed = !AnnotationUtil.isClassAnnotated(GaeObjectStore.unIndexed(), instance);

    Map<String, Object> props = new LinkedHashMap<String, Object>();
    List<Entity> target = null;
    // Marshall java.util.Map
    if (instance instanceof Map) {
        Map map = (Map) instance;
        if (String.class.getName().equals(MapHelper.getKeyType(map))) {
            Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                String entryKey = entry.getKey();
                Object entryVal = entry.getValue();
                if (!entryKey.equals(GaeObjectStore.KEY_RESERVED_PROPERTY)
                        && !entryKey.equals(GaeObjectStore.KIND_RESERVED_PROPERTY)
                        && !entryKey.equals(GaeObjectStore.NAMESPACE_RESERVED_PROPERTY)) {
                    if (entryVal instanceof Map) {
                        setProperty(e, entryKey, createEmbeddedEntityFromMap((Map) entryVal), indexed);
                    } else if (entryVal instanceof List) {
                        setProperty(e, entryKey, createEmbeddedEntityFromList((List) entryVal), indexed);
                    } else {
                        setProperty(e, entryKey, entryVal, indexed);
                    }
                }
            }
        } else {
            throw new RuntimeException(
                    String.class.getName() + " is the only supported " + Map.class.getName() + " key");
        }
    } else {
        // Marshall all other object types
        Field[] fields = instance.getClass().getDeclaredFields();
        GeoPt geoPt = null;
        for (Field field : fields) {
            if (target == null) {
                target = new LinkedList<Entity>();
            }
            if ((field.getModifiers() & java.lang.reflect.Modifier.FINAL) == java.lang.reflect.Modifier.FINAL) {
                // do nothing for a final field
                // usually static UID fields
                continue;
            }
            String fieldName = field.getName();
            if (field.isAnnotationPresent(GaeObjectStore.key())) {
                // skip
                continue;
            } else if (field.isAnnotationPresent(GaeObjectStore.kind())) {
                continue;
            } else if (field.isAnnotationPresent(Volatile.class)) {
                // skip
                continue;
            }
            try {
                boolean isAccessible = field.isAccessible();
                field.setAccessible(true);
                Class<?> fieldType = field.getType();
                Object fieldValue = field.get(instance);
                if (fieldValue == null) {
                    e.setProperty(fieldName, null);
                } else if (fieldValue instanceof String) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Number || fieldValue instanceof Long
                        || fieldValue instanceof Integer || fieldValue instanceof Short) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Boolean) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Date) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof User) { // GAE support this type
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof List) {
                    LOG.debug("Processing List valueType");
                    if (field.isAnnotationPresent(Embedded.class)) {
                        setProperty(e, fieldName, createEmbeddedEntityFromList((List) fieldValue), indexed);
                    } else {
                        boolean supported = true;
                        List list = (List) fieldValue;
                        for (Object item : list) {
                            if (!GAE_SUPPORTED_TYPES.contains(item.getClass())) {
                                supported = false;
                            }
                        }
                        if (supported) {
                            setProperty(e, fieldName, fieldValue, indexed);
                        } else {
                            throw new RuntimeException("List should only include GAE supported types "
                                    + GAE_SUPPORTED_TYPES + " otherwise annotate the List with @Embedded");
                        }
                    }
                } else if (fieldValue instanceof GeoPt) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Map) {
                    LOG.debug("Processing Map valueType");
                    if (field.isAnnotationPresent(Embedded.class)) {
                        setProperty(e, fieldName, createEmbeddedEntityFromMap((Map) fieldValue), indexed);
                    } else if (field.isAnnotationPresent(Flat.class)) {
                        Map<String, Object> flat = (Map) fieldValue;
                        Iterator<Map.Entry<String, Object>> it = flat.entrySet().iterator();
                        if (!it.hasNext()) {
                            LOG.debug("Iterator is empty");
                        }
                        while (it.hasNext()) {
                            Object entry = it.next();
                            try {
                                Map.Entry<Object, Object> mapEntry = (Map.Entry<Object, Object>) entry;
                                Object entryKey = mapEntry.getKey();
                                Object entryVal = mapEntry.getValue();
                                if (entryVal == null) {
                                    setProperty(e, (String) entryKey, null, indexed);
                                } else if (entryVal instanceof Map) {
                                    setProperty(e, (String) entryKey,
                                            createEmbeddedEntityFromMap((Map) entryVal), indexed);
                                } else if (entryVal instanceof List) {
                                    throw new RuntimeException("List values are not yet supported");
                                } else if (entryVal instanceof String || entryVal instanceof Number
                                        || entryVal instanceof Boolean || entryVal instanceof Date
                                        || entryVal instanceof User) {
                                    setProperty(e, (String) entryKey, entryVal, indexed);
                                } else {
                                    throw new RuntimeException(
                                            "Unsupported GAE property type: " + entryVal.getClass().getName());
                                }
                                if (e == null) {
                                    throw new RuntimeException("Entity is null");
                                }
                            } catch (ClassCastException ex) {
                                // Something is wrong here
                                ex.printStackTrace();
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    } else {
                        throw new TwistException("Map type should be annotated with @Embedded or @Flat");
                    }
                } else {
                    // For primitives
                    if (fieldType.equals(int.class)) {
                        int i = (Integer) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(boolean.class)) {
                        boolean i = (Boolean) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(byte.class)) {
                        byte i = (Byte) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(short.class)) {
                        short i = (Short) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(long.class)) {
                        long i = (Long) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(float.class)) {
                        float i = (Float) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(double.class)) {
                        double i = (Double) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(byte.class)) {
                        byte b = (byte) fieldValue;
                        Blob blob = new Blob(new byte[b]);
                        setProperty(e, fieldName, blob, indexed);
                    } else if (fieldType.equals(byte[].class)) {
                        byte[] bytes = (byte[]) fieldValue;
                        Blob blob = new Blob(bytes);
                        setProperty(e, fieldName, blob, indexed);
                    } else { // POJO
                        if (field.isAnnotationPresent(Embedded.class)) {
                            Map<String, Object> map = createMapFrom(fieldValue);
                            EmbeddedEntity ee = createEmbeddedEntityFromMap(map);
                            setProperty(e, fieldName, ee, indexed);
                        } else if (field.isAnnotationPresent(GaeObjectStore.parent())) {
                            // @Parent first before @Child, don't switch. @Child needs parent Key.
                            if (field.getType().equals(Key.class)) {
                                // skip it
                                continue;
                            } else {
                                if (parent != null) {
                                    // Case where a Parent entity is marshalled then during the
                                    // iteration process, a @Child entity is found
                                    Entity _target = new Entity(createKeyFrom(parent, instance));
                                    _target.setPropertiesFrom(e);
                                    e = _target;
                                } else {
                                    // Case where a Child entity is first marshalled
                                    // at this point this child is not yet in the stack
                                    // and the Parent entity is not yet also in the stack
                                    // so we will create a Key from the "not yet marshalled" parent instance
                                    // or is it not? Let's check.
                                    Object parentField = field.get(instance);
                                    Entity parentEntity = stack.get(parentField);
                                    if (parentEntity != null) {
                                        Entity _target = new Entity(
                                                createKeyFrom(parentEntity.getKey(), instance));
                                        _target.setPropertiesFrom(e);
                                        e = _target;
                                    } else {
                                        Key generatedParentKey = createKeyFrom(null, parentField);
                                        Entity _target = new Entity(
                                                createKeyFrom(generatedParentKey, instance));
                                        _target.setPropertiesFrom(e);
                                        e = _target;
                                        marshall(null, parentField);
                                    }
                                }
                            }
                        } else if (field.isAnnotationPresent(GaeObjectStore.child())) {
                            Object childField = field.get(instance);
                            marshall(e.getKey(), childField);
                            Entity childEntity = stack.get(childField);
                            Key childEntityKey = childEntity.getKey();
                            setProperty(e, fieldName, childEntityKey, indexed);
                        } else if (field.isAnnotationPresent(GaeObjectStore.ancestor())) {
                            // already processed above, skip it
                        } else {
                            throw new RuntimeException(
                                    "POJO's must be annotated with @Embedded, @Parent or @Child annotations for field "
                                            + fieldName);
                        }
                    }
                }
                field.setAccessible(isAccessible);
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            }
        }
    }
    stack.put(instance, e);
    return stack;
}

From source file:com.hunchee.twist.gae.GaeMarshaller.java

/**
 *
 * Create entity objects that can be persisted into the GAE Datastore,
 * including its Parent-Child relationships (if necessary).
 *
 * @param parent parent of the generated entity or Entities
 * @param instance to marshall/*from  w w  w  .j a  v  a2  s  . c  o m*/
 * @return
 */
@Override
public IdentityHashMap marshall(Key parent, Object instance) {
    if (instance == null) {
        throw new RuntimeException("Object cannot be null");
    }
    Entity e = null;
    // Its possible that a Entity to be saved without id and just a parent key
    if (parent != null && hasNoIdField(instance)) {
        String kind = getKindOf(instance);
        e = new Entity(kind, parent);
    } else {
        Key key = createKeyFrom(parent, instance); // inspect kind and create key
        e = new Entity(key);
    }

    boolean indexed = !AnnotationUtil.isClassAnnotated(GaeObjectStore.unIndexed(), instance);

    Map<String, Object> props = new LinkedHashMap<String, Object>();
    List<Entity> target = null;
    // Marshall java.util.Map
    if (instance instanceof Map) {
        Map map = (Map) instance;
        if (String.class.getName().equals(MapHelper.getKeyType(map))) {
            Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                String entryKey = entry.getKey();
                Object entryVal = entry.getValue();
                if (!entryKey.equals(GaeObjectStore.KEY_RESERVED_PROPERTY)
                        && !entryKey.equals(GaeObjectStore.KIND_RESERVED_PROPERTY)
                        && !entryKey.equals(GaeObjectStore.NAMESPACE_RESERVED_PROPERTY)) {
                    if (entryVal instanceof Map) {
                        setProperty(e, entryKey, createEmbeddedEntityFromMap((Map) entryVal), indexed);
                    } else if (entryVal instanceof List) {
                        setProperty(e, entryKey, createEmbeddedEntityFromList((List) entryVal), indexed);
                    } else {
                        setProperty(e, entryKey, entryVal, indexed);
                    }
                }
            }
        } else {
            throw new RuntimeException(
                    String.class.getName() + " is the only supported " + Map.class.getName() + " key");
        }
    } else {
        // Marshall all other object types
        Field[] fields = instance.getClass().getDeclaredFields();
        GeoPt geoPt = null;
        for (Field field : fields) {
            if (target == null) {
                target = new LinkedList<Entity>();
            }
            if ((field.getModifiers() & java.lang.reflect.Modifier.FINAL) == java.lang.reflect.Modifier.FINAL) {
                // do nothing for a final field
                // usually static UID fields
                continue;
            }
            String fieldName = field.getName();
            if (field.isAnnotationPresent(GaeObjectStore.key())) {
                // skip
                continue;
            } else if (field.isAnnotationPresent(GaeObjectStore.kind())) {
                continue;
            } else if (field.isAnnotationPresent(Volatile.class)) {
                // skip
                continue;
            }
            try {
                boolean isAccessible = field.isAccessible();
                field.setAccessible(true);
                Class<?> fieldType = field.getType();
                Object fieldValue = field.get(instance);
                if (fieldValue == null) {
                    e.setProperty(fieldName, null);
                } else if (fieldValue instanceof String) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Number || fieldValue instanceof Long
                        || fieldValue instanceof Integer || fieldValue instanceof Short) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Boolean) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Date) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof User) { // GAE support this type
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Blob) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof List) {
                    LOG.debug("Processing List valueType");
                    if (field.isAnnotationPresent(Embedded.class)) {
                        setProperty(e, fieldName, createEmbeddedEntityFromList((List) fieldValue), indexed);
                    } else {
                        boolean supported = true;
                        List list = (List) fieldValue;
                        for (Object item : list) {
                            if (!GAE_SUPPORTED_TYPES.contains(item.getClass())) {
                                supported = false;
                            }
                        }
                        if (supported) {
                            setProperty(e, fieldName, fieldValue, indexed);
                        } else {
                            throw new RuntimeException("List should only include GAE supported types "
                                    + GAE_SUPPORTED_TYPES + " otherwise annotate the List with @Embedded");
                        }
                    }
                } else if (fieldValue instanceof GeoPt) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Enum) {
                    String enumValue = fieldValue.toString();
                    LOG.info("Enum marshalled as \"" + enumValue + "\"");
                    setProperty(e, fieldName, enumValue, indexed);
                } else if (fieldValue instanceof Map) {
                    LOG.debug("Processing Map valueType");
                    if (field.isAnnotationPresent(Embedded.class)) {
                        setProperty(e, fieldName, createEmbeddedEntityFromMap((Map) fieldValue), indexed);
                    } else if (field.isAnnotationPresent(Flat.class)) {
                        Map<String, Object> flat = (Map) fieldValue;
                        Iterator<Map.Entry<String, Object>> it = flat.entrySet().iterator();
                        if (!it.hasNext()) {
                            LOG.debug("Iterator is empty");
                        }
                        while (it.hasNext()) {
                            Object entry = it.next();
                            try {
                                Map.Entry<Object, Object> mapEntry = (Map.Entry<Object, Object>) entry;
                                Object entryKey = mapEntry.getKey();
                                Object entryVal = mapEntry.getValue();
                                if (entryVal == null) {
                                    setProperty(e, (String) entryKey, null, indexed);
                                } else if (entryVal instanceof Map) {
                                    setProperty(e, (String) entryKey,
                                            createEmbeddedEntityFromMap((Map) entryVal), indexed);
                                } else if (entryVal instanceof List) {
                                    throw new RuntimeException("List values are not yet supported");
                                } else if (entryVal instanceof String || entryVal instanceof Number
                                        || entryVal instanceof Boolean || entryVal instanceof Date
                                        || entryVal instanceof User) {
                                    setProperty(e, (String) entryKey, entryVal, indexed);
                                } else {
                                    throw new RuntimeException(
                                            "Unsupported GAE property type: " + entryVal.getClass().getName());
                                }
                                if (e == null) {
                                    throw new RuntimeException("Entity is null");
                                }
                            } catch (ClassCastException ex) {
                                // Something is wrong here
                                ex.printStackTrace();
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    } else {
                        throw new TwistException("Map type should be annotated with @Embedded or @Flat");
                    }
                } else {
                    // For primitives
                    if (fieldType.equals(int.class)) {
                        int i = (Integer) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(boolean.class)) {
                        boolean i = (Boolean) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(byte.class)) {
                        byte i = (Byte) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(short.class)) {
                        short i = (Short) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(long.class)) {
                        long i = (Long) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(float.class)) {
                        float i = (Float) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(double.class)) {
                        double i = (Double) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(byte.class)) {
                        byte b = (byte) fieldValue;
                        Blob blob = new Blob(new byte[b]);
                        setProperty(e, fieldName, blob, indexed);
                    } else if (fieldType.equals(byte[].class)) {
                        byte[] bytes = (byte[]) fieldValue;
                        Blob blob = new Blob(bytes);
                        setProperty(e, fieldName, blob, indexed);
                    } else if (fieldType.equals(Enum.class)) {
                        throw new RuntimeException("Enum primitive type not yet implemented");
                    } else { // POJO
                        if (field.isAnnotationPresent(Embedded.class)) {
                            Map<String, Object> map = createMapFrom(fieldValue);
                            EmbeddedEntity ee = createEmbeddedEntityFromMap(map);
                            setProperty(e, fieldName, ee, indexed);
                        } else if (field.isAnnotationPresent(GaeObjectStore.parent())) {
                            // @Parent first before @Child, don't switch. @Child needs parent Key.
                            if (field.getType().equals(Key.class)) {
                                // skip it
                                continue;
                            } else {
                                if (parent != null) {
                                    // Case where a Parent entity is marshalled then during the
                                    // iteration process, a @Child entity is found
                                    Entity _target = new Entity(createKeyFrom(parent, instance));
                                    _target.setPropertiesFrom(e);
                                    e = _target;
                                } else {
                                    // Case where a Child entity is first marshalled
                                    // at this point this child is not yet in the stack
                                    // and the Parent entity is not yet also in the stack
                                    // so we will create a Key from the "not yet marshalled" parent instance
                                    // or is it not? Let's check.
                                    Object parentField = field.get(instance);
                                    Entity parentEntity = stack.get(parentField);
                                    if (parentEntity != null) {
                                        Entity _target = new Entity(
                                                createKeyFrom(parentEntity.getKey(), instance));
                                        _target.setPropertiesFrom(e);
                                        e = _target;
                                    } else {
                                        Key generatedParentKey = createKeyFrom(null, parentField);
                                        Entity _target = new Entity(
                                                createKeyFrom(generatedParentKey, instance));
                                        _target.setPropertiesFrom(e);
                                        e = _target;
                                        marshall(null, parentField);
                                    }
                                }
                            }
                        } else if (field.isAnnotationPresent(GaeObjectStore.child())) {
                            Object childField = field.get(instance);
                            marshall(e.getKey(), childField);
                            Entity childEntity = stack.get(childField);
                            Key childEntityKey = childEntity.getKey();
                            setProperty(e, fieldName, childEntityKey, indexed);
                        } else if (field.isAnnotationPresent(GaeObjectStore.ancestor())) {
                            // already processed above, skip it
                        } else {
                            throw new RuntimeException(
                                    "POJO's must be annotated with @Embedded, @Parent or @Child annotations for field "
                                            + fieldName);
                        }
                    }
                }
                field.setAccessible(isAccessible);
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            }
        }
    }
    stack.put(instance, e);
    return stack;
}

From source file:com.dotweblabs.twirl.gae.GaeMarshaller.java

/**
 *
 * Create entity objects that can be persisted into the GAE Datastore,
 * including its Parent-Child relationships (if necessary).
 *
 * @param parent parent of the generated entity or Entities
 * @param instance to marshall/* ww  w  .  ja  v a 2  s .  c  o m*/
 * @return
 */
@Override
public IdentityHashMap marshall(Key parent, Object instance) {
    if (instance == null) {
        throw new RuntimeException("Object cannot be null");
    }
    Entity e = null;
    // Its possible that a Entity to be saved without id and just a parent key
    if (parent != null && hasNoIdField(instance)) {
        String kind = getKindOf(instance);
        e = new Entity(kind, parent);
    } else {
        Key key = createKeyFrom(parent, instance); // inspect kind and create key
        e = new Entity(key);
    }

    boolean indexed = !AnnotationUtil.isClassAnnotated(GaeObjectStore.unIndexed(), instance);

    Map<String, Object> props = new LinkedHashMap<String, Object>();
    List<Entity> target = null;
    // Marshall java.util.Map
    if (instance instanceof Map) {
        Map map = (Map) instance;
        if (String.class.getName().equals(MapHelper.getKeyType(map))) {
            Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                String entryKey = entry.getKey();
                Object entryVal = entry.getValue();
                if (!entryKey.equals(GaeObjectStore.KEY_RESERVED_PROPERTY)
                        && !entryKey.equals(GaeObjectStore.KIND_RESERVED_PROPERTY)
                        && !entryKey.equals(GaeObjectStore.NAMESPACE_RESERVED_PROPERTY)) {
                    if (entryVal instanceof Map) {
                        setProperty(e, entryKey, createEmbeddedEntityFromMap((Map) entryVal), indexed);
                    } else if (entryVal instanceof List) {
                        setProperty(e, entryKey, createEmbeddedEntityFromList((List) entryVal), indexed);
                    } else {
                        setProperty(e, entryKey, entryVal, indexed);
                    }
                }
            }
        } else {
            throw new RuntimeException(
                    String.class.getName() + " is the only supported " + Map.class.getName() + " key");
        }
    } else {
        // Marshall all other object types
        Field[] fields = instance.getClass().getDeclaredFields();
        GeoPt geoPt = null;
        for (Field field : fields) {
            if (target == null) {
                target = new LinkedList<Entity>();
            }
            if ((field.getModifiers() & java.lang.reflect.Modifier.FINAL) == java.lang.reflect.Modifier.FINAL) {
                // do nothing for a final field
                // usually static UID fields
                continue;
            }
            String fieldName = field.getName();
            if (field.isAnnotationPresent(GaeObjectStore.key())) {
                // skip
                continue;
            } else if (field.isAnnotationPresent(GaeObjectStore.objectId())) {
                // skip
                continue;
            } else if (field.isAnnotationPresent(GaeObjectStore.kind())) {
                // skip
                continue;
            } else if (field.isAnnotationPresent(Volatile.class)) {
                // skip
                continue;
            }
            try {
                boolean isAccessible = field.isAccessible();
                field.setAccessible(true);
                Class<?> fieldType = field.getType();
                Object fieldValue = field.get(instance);
                if (fieldValue == null) {
                    e.setProperty(fieldName, null);
                } else if (fieldValue instanceof String) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Number || fieldValue instanceof Long
                        || fieldValue instanceof Integer || fieldValue instanceof Short) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Boolean) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Date) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof User) { // GAE support this type
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Blob) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof List) {
                    LOG.debug("Processing List valueType");
                    if (field.isAnnotationPresent(Embedded.class)) {
                        setProperty(e, fieldName, createEmbeddedEntityFromList((List) fieldValue), indexed);
                    } else {
                        boolean supported = true;
                        List list = (List) fieldValue;
                        for (Object item : list) {
                            if (!GAE_SUPPORTED_TYPES.contains(item.getClass())) {
                                supported = false;
                            }
                        }
                        if (supported) {
                            setProperty(e, fieldName, fieldValue, indexed);
                        } else {
                            throw new RuntimeException("List should only include GAE supported types "
                                    + GAE_SUPPORTED_TYPES + " otherwise annotate the List with @Embedded");
                        }
                    }
                } else if (fieldValue instanceof GeoPt) {
                    setProperty(e, fieldName, fieldValue, indexed);
                } else if (fieldValue instanceof Enum) {
                    String enumValue = fieldValue.toString();
                    LOG.info("Enum marshalled as \"" + enumValue + "\"");
                    setProperty(e, fieldName, enumValue, indexed);
                } else if (fieldValue instanceof Map) {
                    LOG.debug("Processing Map valueType");
                    if (field.isAnnotationPresent(Embedded.class)) {
                        setProperty(e, fieldName, createEmbeddedEntityFromMap((Map) fieldValue), indexed);
                    } else if (field.isAnnotationPresent(Flat.class)) {
                        Map<String, Object> flat = (Map) fieldValue;
                        Iterator<Map.Entry<String, Object>> it = flat.entrySet().iterator();
                        if (!it.hasNext()) {
                            LOG.debug("Iterator is empty");
                        }
                        while (it.hasNext()) {
                            Object entry = it.next();
                            try {
                                Map.Entry<Object, Object> mapEntry = (Map.Entry<Object, Object>) entry;
                                Object entryKey = mapEntry.getKey();
                                Object entryVal = mapEntry.getValue();
                                if (entryVal == null) {
                                    setProperty(e, (String) entryKey, null, indexed);
                                } else if (entryVal instanceof Map) {
                                    setProperty(e, (String) entryKey,
                                            createEmbeddedEntityFromMap((Map) entryVal), indexed);
                                } else if (entryVal instanceof List) {
                                    throw new RuntimeException("List values are not yet supported");
                                } else if (entryVal instanceof String || entryVal instanceof Number
                                        || entryVal instanceof Boolean || entryVal instanceof Date
                                        || entryVal instanceof User || entryVal instanceof EmbeddedEntity) {
                                    setProperty(e, (String) entryKey, entryVal, indexed);
                                } else {
                                    throw new RuntimeException(
                                            "Unsupported GAE property type: " + entryVal.getClass().getName());
                                }
                                if (e == null) {
                                    throw new RuntimeException("Entity is null");
                                }
                            } catch (ClassCastException ex) {
                                // Something is wrong here
                                ex.printStackTrace();
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    } else {
                        throw new TwirlException("Map type should be annotated with @Embedded or @Flat");
                    }
                } else {
                    // For primitives
                    if (fieldType.equals(int.class)) {
                        int i = (Integer) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(boolean.class)) {
                        boolean i = (Boolean) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(byte.class)) {
                        byte i = (Byte) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(short.class)) {
                        short i = (Short) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(long.class)) {
                        long i = (Long) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(float.class)) {
                        float i = (Float) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(double.class)) {
                        double i = (Double) fieldValue;
                        setProperty(e, fieldName, i, indexed);
                    } else if (fieldType.equals(byte.class)) {
                        byte b = (byte) fieldValue;
                        Blob blob = new Blob(new byte[b]);
                        setProperty(e, fieldName, blob, indexed);
                    } else if (fieldType.equals(byte[].class)) {
                        byte[] bytes = (byte[]) fieldValue;
                        Blob blob = new Blob(bytes);
                        setProperty(e, fieldName, blob, indexed);
                    } else if (fieldType.equals(Enum.class)) {
                        throw new RuntimeException("Enum primitive type not yet implemented");
                    } else { // POJO
                        if (field.isAnnotationPresent(Embedded.class)) {
                            Map<String, Object> map = createMapFrom(fieldValue);
                            EmbeddedEntity ee = createEmbeddedEntityFromMap(map);
                            setProperty(e, fieldName, ee, indexed);
                        } else if (field.isAnnotationPresent(GaeObjectStore.parent())) {
                            // @Parent first before @Child, don't switch. @Child needs parent Key.
                            if (field.getType().equals(Key.class)) {
                                // skip it
                                continue;
                            } else {
                                if (parent != null) {
                                    // Case where a Parent entity is marshalled then during the
                                    // iteration process, a @Child entity is found
                                    Entity _target = new Entity(createKeyFrom(parent, instance));
                                    _target.setPropertiesFrom(e);
                                    e = _target;
                                } else {
                                    // Case where a Child entity is first marshalled
                                    // at this point this child is not yet in the stack
                                    // and the Parent entity is not yet also in the stack
                                    // so we will create a Key from the "not yet marshalled" parent instance
                                    // or is it not? Let's check.
                                    Object parentField = field.get(instance);
                                    Entity parentEntity = stack.get(parentField);
                                    if (parentEntity != null) {
                                        Entity _target = new Entity(
                                                createKeyFrom(parentEntity.getKey(), instance));
                                        _target.setPropertiesFrom(e);
                                        e = _target;
                                    } else {
                                        Key generatedParentKey = createKeyFrom(null, parentField);
                                        Entity _target = new Entity(
                                                createKeyFrom(generatedParentKey, instance));
                                        _target.setPropertiesFrom(e);
                                        e = _target;
                                        marshall(null, parentField);
                                    }
                                }
                            }
                        } else if (field.isAnnotationPresent(GaeObjectStore.child())) {
                            Object childField = field.get(instance);
                            marshall(e.getKey(), childField);
                            Entity childEntity = stack.get(childField);
                            Key childEntityKey = childEntity.getKey();
                            setProperty(e, fieldName, childEntityKey, indexed);
                        } else if (field.isAnnotationPresent(GaeObjectStore.ancestor())) {
                            // already processed above, skip it
                        } else {
                            throw new RuntimeException(
                                    "POJO's must be annotated with @Embedded, @Parent or @Child annotations for field "
                                            + fieldName);
                        }
                    }
                }
                field.setAccessible(isAccessible);
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            }
        }
    }
    stack.put(instance, e);
    return stack;
}

From source file:com.sxj.mybatis.orm.builder.GenericStatementBuilder.java

public GenericStatementBuilder(Configuration configuration, final Class<?> entityClass) {
    super(configuration);
    this.entityClass = entityClass;
    sharded = ConfigurationProperties.isSharded(configuration);
    String resource = entityClass.getName().replace('.', '/') + ".java (best guess)";
    assistant = new MapperBuilderAssistant(configuration, resource);
    entity = entityClass.getAnnotation(Entity.class);
    mapperType = entity.mapper();//  w  w  w  .  j av  a2 s  .  c om

    if (!mapperType.isAssignableFrom(Void.class)) {
        namespace = mapperType.getName();
    } else {
        namespace = entityClass.getName();
    }
    assistant.setCurrentNamespace(namespace);
    Collection<String> cacheNames = configuration.getCacheNames();
    for (String name : cacheNames)
        if (namespace.equals(name)) {
            assistant.useCacheRef(name);
            break;
        }

    databaseId = super.getConfiguration().getDatabaseId();
    lang = super.getConfiguration().getDefaultScriptingLanuageInstance();

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Table table = entityClass.getAnnotation(Table.class);
    if (table == null) {
        tableName = CaseFormatUtils.camelToUnderScore(entityClass.getSimpleName());
    } else {
        tableName = table.name();
    }

    ///~~~~~~~~~~~~~~~~~~~~~~
    idField = AnnotationUtils.findDeclaredFieldWithAnnoation(Id.class, entityClass);
    if (!sharded && (this.idField.isAnnotationPresent(GeneratedValue.class))
            && (((GeneratedValue) this.idField.getAnnotation(GeneratedValue.class))
                    .strategy() == GenerationType.UUID))
        columnFields.add(idField);
    else
        columnFields.add(idField);
    versionField = AnnotationUtils.findDeclaredFieldWithAnnoation(Version.class, entityClass);

    ReflectionUtils.doWithFields(entityClass, new FieldCallback() {

        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            if (field.isAnnotationPresent(Column.class))
                columnFields.add(field);
            if (field.isAnnotationPresent(Sn.class))
                containSn = true;

        }
    }, new FieldFilter() {

        public boolean matches(Field field) {
            if (Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) {
                return false;
            }

            for (Annotation annotation : field.getAnnotations()) {
                if (Transient.class.isAssignableFrom(annotation.getClass())
                        || Id.class.isAssignableFrom(annotation.getClass())) {
                    return false;
                }
            }

            return true;
        }
    });
}

From source file:com.eucalyptus.objectstorage.pipeline.binding.ObjectStorageRESTBinding.java

protected Map<String, String> buildFieldMap(final Class targetType) {
    Map<String, String> fieldMap = new HashMap<String, String>();
    Field[] fields = targetType.getDeclaredFields();
    for (Field f : fields)
        if (Modifier.isStatic(f.getModifiers()))
            continue;
        else if (f.isAnnotationPresent(HttpParameterMapping.class)) {
            for (final String parameter : f.getAnnotation(HttpParameterMapping.class).parameter())
                fieldMap.put(parameter, f.getName());
            fieldMap.put(f.getName().substring(0, 1).toUpperCase().concat(f.getName().substring(1)),
                    f.getName());/*from   w ww.  java  2s  . c  om*/
        } else
            fieldMap.put(f.getName().substring(0, 1).toUpperCase().concat(f.getName().substring(1)),
                    f.getName());
    return fieldMap;
}

From source file:com.eucalyptus.objectstorage.pipeline.WalrusRESTBinding.java

private Map<String, String> buildFieldMap(final Class targetType) {
    Map<String, String> fieldMap = new HashMap<String, String>();
    Field[] fields = targetType.getDeclaredFields();
    for (Field f : fields)
        if (Modifier.isStatic(f.getModifiers()))
            continue;
        else if (f.isAnnotationPresent(HttpParameterMapping.class)) {
            for (final String parameter : f.getAnnotation(HttpParameterMapping.class).parameter())
                fieldMap.put(parameter, f.getName());
            fieldMap.put(f.getName().substring(0, 1).toUpperCase().concat(f.getName().substring(1)),
                    f.getName());/* www . ja v  a2s.co  m*/
        } else
            fieldMap.put(f.getName().substring(0, 1).toUpperCase().concat(f.getName().substring(1)),
                    f.getName());
    return fieldMap;
}

From source file:com.eucalyptus.ws.handlers.WalrusRESTBinding.java

private Map<String, String> buildFieldMap(final Class targetType) {
    Map<String, String> fieldMap = new HashMap<String, String>();
    Field[] fields = targetType.getDeclaredFields();
    for (Field f : fields)
        if (Modifier.isStatic(f.getModifiers()))
            continue;
        else if (f.isAnnotationPresent(HttpParameterMapping.class)) {
            fieldMap.put(f.getAnnotation(HttpParameterMapping.class).parameter(), f.getName());
            fieldMap.put(f.getName().substring(0, 1).toUpperCase().concat(f.getName().substring(1)),
                    f.getName());/*w  w w  .j a v  a  2  s .  c o m*/
        } else
            fieldMap.put(f.getName().substring(0, 1).toUpperCase().concat(f.getName().substring(1)),
                    f.getName());
    return fieldMap;
}