Example usage for java.lang.reflect Modifier isTransient

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

Introduction

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

Prototype

public static boolean isTransient(int mod) 

Source Link

Document

Return true if the integer argument includes the transient modifier, false otherwise.

Usage

From source file:de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderTest.java

private void assertEqualDeclaredFields(final Class<? extends Object> clazz, final Object one,
        final Object another, final Map<Object, Object> alreadyChecked)
        throws Exception, IllegalAccessException {
    for (final Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);/*from w  w w  .ja v a  2s . co  m*/
        if (!Modifier.isTransient(field.getModifiers())) {
            assertDeepEquals(field.get(one), field.get(another), alreadyChecked);
        }
    }
}

From source file:com.erudika.para.utils.Utils.java

/**
 * Returns a list of all declared fields in a class. Transient and serialVersionUID fields are skipped.
 * This method scans parent classes as well.
 * @param clazz a class to scan/*from  www  . ja v  a2s.  c om*/
 * @return a list of fields including those of the parent classes excluding the Object class.
 */
public static List<Field> getAllDeclaredFields(Class<? extends ParaObject> clazz) {
    LinkedList<Field> fields = new LinkedList<Field>();
    if (clazz == null) {
        return fields;
    }
    Class<?> parent = clazz;
    do {
        for (Field field : parent.getDeclaredFields()) {
            if (!Modifier.isTransient(field.getModifiers()) && !field.getName().equals("serialVersionUID")) {
                fields.add(field);
            }
        }
        parent = parent.getSuperclass();
    } while (!parent.equals(Object.class));
    return fields;
}

From source file:com.clarkparsia.empire.annotation.RdfGenerator.java

/**
 * Return the given Java bean as a set of RDF triples
 * @param theObj the object//from   w w w  .  ja  v  a  2s. c  om
 * @return the object represented as RDF triples
 * @throws InvalidRdfException thrown if the object cannot be transformed into RDF.
 */
public static Graph asRdf(final Object theObj) throws InvalidRdfException {
    if (theObj == null) {
        return null;
    }

    Object aObj = theObj;

    if (aObj instanceof ProxyHandler) {
        aObj = ((ProxyHandler) aObj).mProxy.value();
    } else {
        try {
            if (aObj.getClass().getDeclaredField("handler") != null) {
                Field aProxy = aObj.getClass().getDeclaredField("handler");
                aObj = ((ProxyHandler) BeanReflectUtil.safeGet(aProxy, aObj)).mProxy.value();
            }
        } catch (InvocationTargetException e) {
            // this is probably an error, we know its a proxy object, but can't get the proxied object
            throw new InvalidRdfException("Could not access proxy object", e);
        } catch (NoSuchFieldException e) {
            // this is probably ok.
        }
    }

    RdfsClass aClass = asValidRdfClass(aObj);

    Resource aSubj = id(aObj);

    addNamespaces(aObj.getClass());

    GraphBuilder aBuilder = new GraphBuilder();

    Collection<AccessibleObject> aAccessors = new HashSet<AccessibleObject>();
    aAccessors.addAll(getAnnotatedFields(aObj.getClass()));
    aAccessors.addAll(getAnnotatedGetters(aObj.getClass(), true));

    try {
        ResourceBuilder aRes = aBuilder.instance(
                aBuilder.getValueFactory().createURI(PrefixMapping.GLOBAL.uri(aClass.value())), aSubj);

        for (AccessibleObject aAccess : aAccessors) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Getting rdf for : {}", aAccess);
            }

            AsValueFunction aFunc = new AsValueFunction(aAccess);

            if (aAccess.isAnnotationPresent(Transient.class)
                    || (aAccess instanceof Field && Modifier.isTransient(((Field) aAccess).getModifiers()))) {

                // transient fields or accessors with the Transient annotation do not get converted.
                continue;
            }

            RdfProperty aPropertyAnnotation = BeanReflectUtil.getAnnotation(aAccess, RdfProperty.class);
            String aBase = "urn:empire:clark-parsia:";
            if (aRes instanceof URI) {
                aBase = ((URI) aRes).getNamespace();
            }

            URI aProperty = aPropertyAnnotation != null
                    ? aBuilder.getValueFactory()
                            .createURI(PrefixMapping.GLOBAL.uri(aPropertyAnnotation.value()))
                    : (aAccess instanceof Field
                            ? aBuilder.getValueFactory().createURI(aBase + ((Field) aAccess).getName())
                            : null);

            boolean aOldAccess = aAccess.isAccessible();
            setAccessible(aAccess, true);

            Object aValue = get(aAccess, aObj);

            setAccessible(aAccess, aOldAccess);

            if (aValue == null || aValue.toString().equals("")) {
                continue;
            } else if (Collection.class.isAssignableFrom(aValue.getClass())) {
                @SuppressWarnings("unchecked")
                List<Value> aValueList = asList(aAccess, (Collection<?>) Collection.class.cast(aValue));

                if (aValueList.isEmpty()) {
                    continue;
                }

                if (aPropertyAnnotation.isList()) {
                    aRes.addProperty(aProperty, aValueList);
                } else {
                    for (Value aVal : aValueList) {
                        aRes.addProperty(aProperty, aVal);
                    }
                }
            } else {
                aRes.addProperty(aProperty, aFunc.apply(aValue));
            }
        }
    } catch (IllegalAccessException e) {
        throw new InvalidRdfException(e);
    } catch (RuntimeException e) {
        throw new InvalidRdfException(e);
    } catch (InvocationTargetException e) {
        throw new InvalidRdfException("Cannot invoke method", e);
    }

    return aBuilder.graph();
}

From source file:phex.prefs.OldCfg.java

private void deserializeSimpleFields() {
    Field[] fields = this.getClass().getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        String name = fields[i].getName();
        int modifiers = fields[i].getModifiers();
        Class type = fields[i].getType();
        String value = "";

        if (!Modifier.isPublic(modifiers) || Modifier.isTransient(modifiers) || Modifier.isStatic(modifiers)) {
            continue;
        }//from  ww  w  . ja  v a  2 s  . co m

        try {
            // if this is a map, don't continue after deserialising
            if (deserialiseMap(fields[i]))
                continue;
        } catch (Exception ex) {
            // exception may be thrown if it's not a map. Nothing to worry about.
        }

        try {
            // if this is a list, don't continue after deserialising
            if (deserialiseList(fields[i]))
                continue;
        } catch (Exception ex) {
            // exception may be thrown if it's not a list. Nothing to worry about.
        }

        try {
            // if this is a set, don't continue after deserialising
            if (deserialiseSet(fields[i]))
                continue;
        } catch (Exception ex) {
            // exception may be thrown if it's not a list. Nothing to worry about.
        }

        try {
            // Load value by field name.
            value = get(name);
            if (value == null) {
                try {
                    value = (String) this.getClass().getDeclaredField("default_" + fields[i].getName())
                            .get(this);
                } catch (NoSuchFieldException exp) {// no such field can be ignored..
                } catch (Exception ex) {
                    ex.printStackTrace();
                } // don't worry if there's no default
            }
            if (value == null) {
                continue; // no default value either, so don't do anything!
            }

            if (type.getName().equals("int")) {
                fields[i].setInt(this, Integer.parseInt(value));
            } else if (type.getName().equals("short")) {
                fields[i].setShort(this, Short.parseShort(value));
            } else if (type.getName().equals("long")) {
                fields[i].setLong(this, Long.parseLong(value));
            } else if (type.getName().equals("float")) {
                fields[i].setFloat(this, Float.parseFloat(value));
            } else if (type.getName().equals("boolean")) {
                fields[i].setBoolean(this, value.equals("true"));
            } else if (type.getName().equals("java.lang.String")) {
                fields[i].set(this, value);
            }
        } catch (Exception exp) {
            NLogger.error(OldCfg.class, "Error in field: " + name + ", value: " + value, exp);
        }
    }
}

From source file:com.hc.wx.server.common.bytecode.ReflectUtils.java

public static Map<String, Field> getBeanPropertyFields(Class cl) {
    Map<String, Field> properties = new HashMap<String, Field>();
    for (; cl != null; cl = cl.getSuperclass()) {
        Field[] fields = cl.getDeclaredFields();
        for (Field field : fields) {
            if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) {
                continue;
            }/*from   ww w .ja  va  2  s. c  o m*/

            field.setAccessible(true);

            properties.put(field.getName(), field);
        }
    }

    return properties;
}

From source file:phex.common.Cfg.java

private void serializeSimpleFields() {
    Field[] fields = this.getClass().getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        String name = fields[i].getName();
        int modifiers = fields[i].getModifiers();
        Class type = fields[i].getType();

        if (!Modifier.isPublic(modifiers) || Modifier.isTransient(modifiers) || Modifier.isStatic(modifiers)) {
            continue;
        }//from w w w.  jav  a2 s  . c o m

        try {
            if (type.getName().equals("int")) {
                set(name, fields[i].getInt(this));
            } else if (type.getName().equals("short")) {
                set(name, fields[i].getShort(this));
            } else if (type.getName().equals("long")) {
                set(name, fields[i].getLong(this));
            } else if (type.getName().equals("float")) {
                set(name, fields[i].getFloat(this));
            } else if (type.getName().equals("boolean")) {
                set(name, fields[i].getBoolean(this));
            } else if (type.getName().equals("java.lang.String")) {
                set(name, (String) fields[i].get(this));
            } else if (type.getName().equals("java.util.ArrayList")) {
                List myList = (List) fields[i].get(this);
                for (int j = 0; j < myList.size(); j++) {
                    String key = new String(fields[i].getName() + LIST_PREFIX + (j + 1));
                    set(key, myList.get(j).toString());
                }
            } else if (type.getName().equals("java.util.HashMap")) {
                Map myMap = (Map) fields[i].get(this);
                if (myMap.keySet().size() == 0)
                    continue; // nothing in this map to save.
                for (Iterator it = myMap.keySet().iterator(); it.hasNext();) {
                    String key = (String) it.next();
                    String entry = new String(fields[i].getName() + LIST_PREFIX + key);
                    set(entry, (String) myMap.get(key));
                }
            } else if (type.getName().equals("java.util.HashSet")) {
                Set mySet = (Set) fields[i].get(this);
                if (mySet.size() == 0)
                    continue; // nothing in this set to save.
                int counter = 0;
                for (Iterator it = mySet.iterator(); it.hasNext();) {
                    counter++;
                    String entry = new String(fields[i].getName() + LIST_PREFIX + "SET" + counter);
                    set(entry, (String) it.next());
                }
            }

            /*
                           else
                           {
            throw new Exception( "Don't know how to serialize a " + type.getName() + " field!"  );
                           }
             */
        } catch (Exception exp) {
            NLogger.error(NLoggerNames.GLOBAL, "Error in field: " + name, exp);
        }
    }
}

From source file:de.javakaffee.kryoserializers.KryoTest.java

private static void assertEqualDeclaredFields(final Class<? extends Object> clazz, final Object one,
        final Object another, final Map<Object, Object> alreadyChecked)
        throws Exception, IllegalAccessException {
    for (final Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);// ww  w.ja  v a2s  .  c  om
        if (!Modifier.isTransient(field.getModifiers())) {
            assertDeepEquals(field.get(one), field.get(another), alreadyChecked);
        }
    }
}

From source file:phex.common.Cfg.java

private void deserializeSimpleFields() {
    Field[] fields = this.getClass().getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {
        String name = fields[i].getName();
        int modifiers = fields[i].getModifiers();
        Class type = fields[i].getType();
        String value = "";

        if (!Modifier.isPublic(modifiers) || Modifier.isTransient(modifiers) || Modifier.isStatic(modifiers)) {
            continue;
        }/*  www. ja  va2  s . c  o  m*/

        try {
            // if this is a map, don't continue after deserialising
            if (deserialiseMap(fields[i]))
                continue;
        } catch (Exception ex) {
            // exception may be thrown if it's not a map. Nothing to worry about.
        }

        try {
            // if this is a list, don't continue after deserialising
            if (deserialiseList(fields[i]))
                continue;
        } catch (Exception ex) {
            // exception may be thrown if it's not a list. Nothing to worry about.
        }

        try {
            // if this is a set, don't continue after deserialising
            if (deserialiseSet(fields[i]))
                continue;
        } catch (Exception ex) {
            // exception may be thrown if it's not a list. Nothing to worry about.
        }

        try {
            // Load value by field name.
            value = get(name);
            if (value == null) {
                try {
                    value = (String) this.getClass().getDeclaredField("default_" + fields[i].getName())
                            .get(this);
                } catch (NoSuchFieldException exp) {// no such field can be ignored..
                } catch (Exception ex) {
                    ex.printStackTrace();
                } // don't worry if there's no default
            }
            if (value == null) {
                continue; // no default value either, so don't do anything!
            }

            if (type.getName().equals("int")) {
                fields[i].setInt(this, Integer.parseInt(value));
            } else if (type.getName().equals("short")) {
                fields[i].setShort(this, Short.parseShort(value));
            } else if (type.getName().equals("long")) {
                fields[i].setLong(this, Long.parseLong(value));
            } else if (type.getName().equals("float")) {
                fields[i].setFloat(this, Float.parseFloat(value));
            } else if (type.getName().equals("boolean")) {
                fields[i].setBoolean(this, value.equals("true"));
            } else if (type.getName().equals("java.lang.String")) {
                fields[i].set(this, value);
            }
        } catch (Exception exp) {
            NLogger.error(NLoggerNames.GLOBAL, "Error in field: " + name + ", value: " + value, exp);
        }
    }
}

From source file:ca.oson.json.Oson.java

private boolean ignoreModifiers(int modifiers, Set<MODIFIER> includeFieldsWithModifiers) {
    //Set<MODIFIER> includeFieldsWithModifiers = getIncludeFieldsWithModifiers();
    if (includeFieldsWithModifiers == null || includeFieldsWithModifiers.size() == 0) {
        // by default, transient and volatile are ignored
        // unless you specify otherwise, by using MODIFIER.Transient enum, or all
        if (Modifier.isTransient(modifiers)) {
            return true;
        }// w w w .j  av a  2s  .c o  m
        if (Modifier.isVolatile(modifiers)) {
            return true;
        }

        return false;
    }

    if (includeFieldsWithModifiers.contains(MODIFIER.All)) {
        return false;
    }

    for (MODIFIER modifier : includeFieldsWithModifiers) {
        switch (modifier) {
        case Abstract:
            if (Modifier.isAbstract(modifiers)) {
                return false;
            }
            break;
        case Final:
            if (Modifier.isFinal(modifiers)) {
                return false;
            }
            break;
        case Interface:
            if (Modifier.isInterface(modifiers)) {
                return false;
            }
            break;
        case Native:
            if (Modifier.isNative(modifiers)) {
                return false;
            }
            break;
        case Private:
            if (Modifier.isPrivate(modifiers)) {
                return false;
            }
            break;
        case Protected:
            if (Modifier.isProtected(modifiers)) {
                return false;
            }
            break;
        case Public:
            if (Modifier.isPublic(modifiers)) {
                return false;
            }
            break;
        case Package:
            if (ObjectUtil.isPackage(modifiers)) {
                return false;
            }
            break;
        case Static:
            if (Modifier.isStatic(modifiers)) {
                return false;
            }
            break;
        case Strict:
            if (Modifier.isStrict(modifiers)) {
                return false;
            }
            break;
        case Synchronized:
            if (Modifier.isSynchronized(modifiers)) {
                return false;
            }
            break;
        case Transient:
            if (Modifier.isTransient(modifiers)) {
                return false;
            }
            break;
        case Volatile:
            if (Modifier.isVolatile(modifiers)) {
                return false;
            }
            break;
        }
    }

    return true;
}