Example usage for java.lang.reflect Modifier TRANSIENT

List of usage examples for java.lang.reflect Modifier TRANSIENT

Introduction

In this page you can find the example usage for java.lang.reflect Modifier TRANSIENT.

Prototype

int TRANSIENT

To view the source code for java.lang.reflect Modifier TRANSIENT.

Click Source Link

Document

The int value representing the transient modifier.

Usage

From source file:com.shenit.commons.utils.GsonUtils.java

private static final GsonBuilder createBuilder() {
    return new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").disableHtmlEscaping()
            .excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT);
}

From source file:Spy.java

private static int modifierFromString(String s) {
    int m = 0x0;/*from   ww w .  j  av  a2s  .c  om*/
    if ("public".equals(s))
        m |= Modifier.PUBLIC;
    else if ("protected".equals(s))
        m |= Modifier.PROTECTED;
    else if ("private".equals(s))
        m |= Modifier.PRIVATE;
    else if ("static".equals(s))
        m |= Modifier.STATIC;
    else if ("final".equals(s))
        m |= Modifier.FINAL;
    else if ("transient".equals(s))
        m |= Modifier.TRANSIENT;
    else if ("volatile".equals(s))
        m |= Modifier.VOLATILE;
    return m;
}

From source file:org.bitpipeline.lib.friendlyjson.JSONEntity.java

/** Des-serialize a JSON object.
 * @throws JSONException/*from www.  j a  v a 2s. com*/
 * @throws JSONMappingException */
public JSONEntity(JSONObject json) throws JSONMappingException {
    if (json == null)
        return;
    Class<?> clazz = this.getClass();
    List<Field> declaredFields = new ArrayList<Field>();
    do {
        Field[] fields = clazz.getDeclaredFields();
        declaredFields.addAll(Arrays.asList(fields));
        clazz = clazz.getSuperclass();
    } while (clazz != null && !clazz.isAssignableFrom(JSONEntity.class));
    for (Field field : declaredFields) {
        if ((field.getModifiers() & Modifier.TRANSIENT) != 0) { // don't care about transient fields.
            continue;
        }

        String fieldName = field.getName();
        if (fieldName.equals("this$0"))
            continue;

        boolean accessible = field.isAccessible();
        if (!accessible)
            field.setAccessible(true);

        Class<?> type = field.getType();

        if (type.isArray()) {
            Class<?> componentClass = type.getComponentType();
            JSONArray jsonArray = null;
            try {
                jsonArray = json.getJSONArray(fieldName);
            } catch (JSONException e) {
                // no data for this field found.
                continue;
            }

            int size = jsonArray.length();

            Object array = Array.newInstance(componentClass, size);
            for (int i = 0; i < size; i++) {
                try {
                    Object item = fromJson(componentClass, jsonArray.get(i));
                    Array.set(array, i, item);
                } catch (Exception e) {
                    System.err.println("Invalid array component for class " + this.getClass().getName()
                            + " field " + field.getName() + "::" + field.getType().getName());
                }
            }

            try {
                field.set(this, array);
            } catch (Exception e) {
                throw new JSONMappingException(e);
            }
        } else if (JSONEntity.class.isAssignableFrom(type)) {
            try {
                Object entity = readJSONEntity(type, json.getJSONObject(fieldName));
                field.set(this, entity);
            } catch (JSONException e) {
                // keep going.. the json representation doesn't have value for this field.
            } catch (Exception e) {
                throw new JSONMappingException(e);
            }
        } else {
            FieldSetter setter = JSON_READERS.get(type.getName());
            if (setter != null) {
                try {
                    setter.setField(this, field, json, fieldName);
                } catch (JSONException e) {
                    // do nothing. We just didn't receive data for this field
                } catch (Exception e) {
                    throw new JSONMappingException(e);
                }
            } else {
                Object jsonObj;
                try {
                    jsonObj = json.get(fieldName);
                } catch (Exception e) {
                    jsonObj = null;
                }
                if (jsonObj != null) {
                    Object value = fromJson(type, jsonObj);
                    try {
                        field.set(this, value);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    System.err.println("No setter for " + field);
                }

            }
        }

        if (!accessible)
            field.setAccessible(false);
    }
}

From source file:org.eclipse.php.composer.api.entities.JsonEntity.java

@SuppressWarnings("rawtypes")
protected ArrayList<Field> getFields(Class entity) {
    ArrayList<Field> fields = new ArrayList<Field>();
    Class superClass = entity;/*from   www . ja  v a  2  s  . c  om*/

    while (superClass != null) {
        for (Field field : superClass.getDeclaredFields()) {
            if (!((field.getModifiers() & Modifier.TRANSIENT) == Modifier.TRANSIENT)) {
                fields.add(field);
            }
        }
        superClass = superClass.getSuperclass();
    }

    return fields;
}

From source file:org.springframework.data.keyvalue.riak.RiakMappedClass.java

private void initFields() {
    ReflectionUtils.doWithFields(this.clazz, new FieldCallback() {

        @Override/*  www  . ja va2s . c  om*/
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            if (!field.isAccessible())
                ReflectionUtils.makeAccessible(field);

            if (field.isAnnotationPresent(Transient.class) || field.isSynthetic()
                    || field.getModifiers() == Modifier.FINAL || field.getModifiers() == Modifier.TRANSIENT) {
                return;
            }

            // Field can only have one of these, if more than one throw an
            // error
            List<Annotation> annots = org.springframework.data.keyvalue.riak.util.AnnotationUtils
                    .getFoundAnnotation(Id.class, Column.class, Embedded.class, Version.class, ManyToOne.class,
                            OneToMany.class, Basic.class);

            // Probably allow auto generation at some point, but for now
            // have to add one of the annotations
            if (annots.size() > 1)
                throw new IllegalArgumentException(String.format(
                        "The field %s must have only one of the following annotations: "
                                + "@Id, @Basic, @Column, @Embedded, @Version, @ManyToOne, @OneToMany",
                        field.getName()));

            Annotation annot = annots.get(0);

            if (annot.annotationType().equals(Id.class))
                RiakMappedClass.this.id = field;

            // Create a new mapped field here and then add to a list of
            // property MappedFields
            propertyFields.add(new RiakMappedField(field, annot));
        }
    });
    Map<Class<? extends Annotation>, Annotation> fieldAnnotMap = new HashMap<Class<? extends Annotation>, Annotation>();
    for (Class<? extends Annotation> a : entityAnnotations) {
        Target target = a.getAnnotation(Target.class);
        if (target != null && (ArrayUtils.contains(target.value(), ElementType.FIELD)
                || ArrayUtils.contains(target.value(), ElementType.METHOD))) {
            Annotation fieldAnnot;
            if ((fieldAnnot = this.clazz.getAnnotation(a)) != null) {
                fieldAnnotMap.put(a, fieldAnnot);
            }
        }
    }
}

From source file:hu.bme.mit.sette.common.model.snippet.SnippetContainer.java

/**
 * Validates the fields of the class./* w  w w .  jav a2s  .c o  m*/
 *
 * @param validator
 *            a validator
 */
private void validateFields(final AbstractValidator<?> validator) {
    // check: only constant ("public static final") or synthetic fields
    for (Field field : javaClass.getDeclaredFields()) {
        if (field.isSynthetic()) {
            // skip for synthetic fields (e.g. switch for enum generates
            // synthetic methods and fields)
            continue;
        }

        FieldValidator v = new FieldValidator(field);
        v.withModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL);
        v.withoutModifiers(Modifier.SYNCHRONIZED | Modifier.TRANSIENT | Modifier.VOLATILE);

        validator.addChildIfInvalid(v);
    }
}

From source file:com.ryan.ryanreader.jsonwrap.JsonBufferedObject.java

public void populateObject(final Object o) throws InterruptedException, IOException, IllegalArgumentException,
        InstantiationException, NoSuchMethodException, InvocationTargetException {

    if (join() != Status.LOADED) {
        throwFailReasonException();//from  w ww. ja  va  2s .c o  m
    }

    final Field[] objectFields = o.getClass().getFields();

    try {

        for (final Field objectField : objectFields) {

            if ((objectField.getModifiers() & Modifier.TRANSIENT) != 0) {
                continue;
            }

            final JsonValue val;

            if (properties.containsKey(objectField.getName())) {
                val = properties.get(objectField.getName());

            } else if (objectField.getName().startsWith("_json_")) {
                val = properties.get(objectField.getName().substring("_json_".length()));
            } else {
                val = null;
            }

            if (val == null) {
                continue;
            }

            objectField.setAccessible(true);

            final Class<?> fieldType = objectField.getType();

            if (fieldType == Long.class || fieldType == Long.TYPE) {
                objectField.set(o, val.asLong());

            } else if (fieldType == Double.class || fieldType == Double.TYPE) {
                objectField.set(o, val.asDouble());

            } else if (fieldType == Integer.class || fieldType == Integer.TYPE) {
                objectField.set(o, val.isNull() ? null : val.asLong().intValue());

            } else if (fieldType == Float.class || fieldType == Float.TYPE) {
                objectField.set(o, val.isNull() ? null : val.asDouble().floatValue());

            } else if (fieldType == Boolean.class || fieldType == Boolean.TYPE) {
                objectField.set(o, val.asBoolean());

            } else if (fieldType == String.class) {
                objectField.set(o, val.asString());

            } else if (fieldType == JsonBufferedArray.class) {
                objectField.set(o, val.asArray());

            } else if (fieldType == JsonBufferedObject.class) {
                objectField.set(o, val.asObject());

            } else if (fieldType == JsonValue.class) {
                objectField.set(o, val);

            } else if (fieldType == Object.class) {

                final Object result;

                switch (val.getType()) {
                case BOOLEAN:
                    result = val.asBoolean();
                    break;
                case INTEGER:
                    result = val.asLong();
                    break;
                case STRING:
                    result = val.asString();
                    break;
                case FLOAT:
                    result = val.asDouble();
                    break;
                default:
                    result = val;
                }

                objectField.set(o, result);

            } else {
                objectField.set(o, val.asObject(fieldType));
            }
        }

    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.quantumbadger.redreader.jsonwrap.JsonBufferedObject.java

public void populateObject(final Object o) throws InterruptedException, IOException, IllegalArgumentException,
        InstantiationException, NoSuchMethodException, InvocationTargetException {

    if (join() != STATUS_LOADED) {
        throwFailReasonException();/*from w  w w . ja  va 2s. com*/
    }

    final Field[] objectFields = o.getClass().getFields();

    try {

        for (final Field objectField : objectFields) {

            if ((objectField.getModifiers() & Modifier.TRANSIENT) != 0) {
                continue;
            }

            final JsonValue val;

            if (properties.containsKey(objectField.getName())) {
                val = properties.get(objectField.getName());

            } else if (objectField.getName().startsWith("_json_")) {
                val = properties.get(objectField.getName().substring("_json_".length()));
            } else {
                val = null;
            }

            if (val == null) {
                continue;
            }

            objectField.setAccessible(true);

            final Class<?> fieldType = objectField.getType();

            if (fieldType == Long.class || fieldType == Long.TYPE) {
                objectField.set(o, val.asLong());

            } else if (fieldType == Double.class || fieldType == Double.TYPE) {
                objectField.set(o, val.asDouble());

            } else if (fieldType == Integer.class || fieldType == Integer.TYPE) {
                objectField.set(o, val.isNull() ? null : val.asLong().intValue());

            } else if (fieldType == Float.class || fieldType == Float.TYPE) {
                objectField.set(o, val.isNull() ? null : val.asDouble().floatValue());

            } else if (fieldType == Boolean.class || fieldType == Boolean.TYPE) {
                objectField.set(o, val.asBoolean());

            } else if (fieldType == String.class) {
                objectField.set(o, val.asString());

            } else if (fieldType == JsonBufferedArray.class) {
                objectField.set(o, val.asArray());

            } else if (fieldType == JsonBufferedObject.class) {
                objectField.set(o, val.asObject());

            } else if (fieldType == JsonValue.class) {
                objectField.set(o, val);

            } else if (fieldType == Object.class) {

                final Object result;

                switch (val.getType()) {
                case JsonValue.TYPE_BOOLEAN:
                    result = val.asBoolean();
                    break;
                case JsonValue.TYPE_INTEGER:
                    result = val.asLong();
                    break;
                case JsonValue.TYPE_STRING:
                    result = val.asString();
                    break;
                case JsonValue.TYPE_FLOAT:
                    result = val.asDouble();
                    break;
                default:
                    result = val;
                }

                objectField.set(o, result);

            } else {
                objectField.set(o, val.asObject(fieldType));
            }
        }

    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

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;/*  w  ww. j av  a2 s  .c o  m*/
    int mod;
    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:de.micromata.genome.tpsb.CommonTestBuilder.java

/**
 * Copy this to test context./*  w w w . j a va2s .  co  m*/
 */
@TpsbIgnore
protected void copyThisToTestContext() {
    testContext.putAll(PrivateBeanUtils.getAllFields(this, 0, Modifier.STATIC | Modifier.TRANSIENT));
    testContext.put("builder", this);
}