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:Main.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = String.class;
    int modifier = clazz.getModifiers();

    if (Modifier.isTransient(modifier)) {
        System.out.println("isTransient");
    }/*  w  ww .jav a 2 s  .co m*/
}

From source file:Main.java

/**
 * Only contains transient or static fields, which is the case for a model that is empty, but
 * says it "implements Parcelable"/*from   w w w.j a  va  2s.  co m*/
 * @param fields the list of fields
 * @return true if contains only transient and static fields, false otherwise
 */
private static boolean containsOnlyTransientAndStaticFields(List<Field> fields) {
    boolean containsNormalField = false;
    for (Field field : fields) {
        if (!Modifier.isTransient(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())) {
            containsNormalField = true;
        }
    }
    return !containsNormalField;
}

From source file:Main.java

/**
 * Get a list of the name of variables of the class received
 * @param klass Class to get the variable names
 * @return List<String>[] of length 3 with variable names: [0]: primitives, [1]: arrays, [2]: objects
 *//*from w w w  . j av  a 2s  .  com*/
public static List<String>[] getVariableNames(Class klass) {

    //array to return
    List<String>[] varNames = new List[3];
    for (int i = 0; i < 3; i++) {
        varNames[i] = new ArrayList<>();
    }

    //add all valid fields
    do {
        Field[] fields = klass.getDeclaredFields();
        for (Field field : fields) {
            if (!Modifier.isTransient(field.getModifiers())) {

                //get the type
                Class type = field.getType();

                if (type.isPrimitive() || (type == Integer.class) || (type == Float.class)
                        || (type == Double.class) || (type == Boolean.class) || (type == String.class)) {
                    varNames[0].add(field.getName());
                } else if (type.isArray()) {
                    varNames[1].add(field.getName());
                } else {
                    varNames[2].add(field.getName());
                }
            }
        }

        klass = klass.getSuperclass();
    } while (klass != null);

    //return array
    return varNames;

}

From source file:Main.java

public static int hashCode(Object target) {
    if (target == null)
        return 0;
    final int prime = 31;
    int result = 1;

    Class<?> current = target.getClass();
    do {//from   ww w.ja  v a  2 s . com
        for (Field f : current.getDeclaredFields()) {
            if (Modifier.isStatic(f.getModifiers()) || Modifier.isTransient(f.getModifiers())
                    || f.isSynthetic()) {
                continue;
            }

            Object self;
            try {
                f.setAccessible(true);
                self = f.get(target);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException(e);
            }
            if (self == null) {
                result = prime * result + 0;
            } else if (self.getClass().isArray()) {
                if (self.getClass().equals(boolean[].class)) {
                    result = prime * result + Arrays.hashCode((boolean[]) self);
                } else if (self.getClass().equals(char[].class)) {
                    result = prime * result + Arrays.hashCode((char[]) self);
                } else if (self.getClass().equals(byte[].class)) {
                    result = prime * result + Arrays.hashCode((byte[]) self);
                } else if (self.getClass().equals(short[].class)) {
                    result = prime * result + Arrays.hashCode((short[]) self);
                } else if (self.getClass().equals(int[].class)) {
                    result = prime * result + Arrays.hashCode((int[]) self);
                } else if (self.getClass().equals(long[].class)) {
                    result = prime * result + Arrays.hashCode((long[]) self);
                } else if (self.getClass().equals(float[].class)) {
                    result = prime * result + Arrays.hashCode((float[]) self);
                } else if (self.getClass().equals(double[].class)) {
                    result = prime * result + Arrays.hashCode((double[]) self);
                } else {
                    result = prime * result + Arrays.hashCode((Object[]) self);
                }
            } else {
                result = prime * result + self.hashCode();
            }

            System.out.println(f.getName() + ": " + result);
        }
        current = current.getSuperclass();
    } while (!Object.class.equals(current));

    return result;
}

From source file:DeclarationInfoDemo.java

public static void printModifiers(final Class dataType) {
    final int modifiers = dataType.getModifiers();
    if (Modifier.isPrivate(modifiers)) {
        System.out.print("private ");
    }/*from  www. j  a v  a  2 s  .  c o m*/
    if (Modifier.isPrivate(modifiers)) {
        System.out.print("private ");
    }
    if (Modifier.isPublic(modifiers)) {
        System.out.print("private ");
    }
    if (Modifier.isAbstract(modifiers)) {
        System.out.print("abstract ");
    }
    if (Modifier.isFinal(modifiers)) {
        System.out.print("final ");
    }
    if (Modifier.isNative(modifiers)) {
        System.out.print("native ");
    }
    if (Modifier.isInterface(modifiers)) {
        System.out.print("interface ");
    }
    if (Modifier.isStatic(modifiers)) {
        System.out.print("static ");
    }
    if (Modifier.isStrict(modifiers)) {
        System.out.print("strict ");
    }
    if (Modifier.isSynchronized(modifiers)) {
        System.out.print("synchronized ");
    }
    if (Modifier.isTransient(modifiers)) {
        System.out.print("transient ");
    }
    if (Modifier.isVolatile(modifiers)) {
        System.out.print("volatile ");
    }
    System.out.println(dataType.getName());
}

From source file:me.prokopyl.storagemonitor.beans.JavaBean.java

@Override
public String toJSONString() {
    JSONObject obj = new JSONObject();
    obj.put("BeanType", this.getBeanName());
    Field[] fields = this.getClass().getDeclaredFields();

    for (Field field : fields) {
        if (Modifier.isTransient(field.getModifiers()))
            continue;
        field.setAccessible(true);/*from  ww  w.  ja va 2  s  . c om*/
        try {
            obj.put(field.getName(), field.get(this));
        } catch (IllegalAccessException ex) {
        }
    }

    return obj.toJSONString();
}

From source file:ModifierUtil.java

public static boolean isTransient(Member member) {
    return Modifier.isTransient(member.getModifiers());
}

From source file:org.eclipse.smarthome.config.core.internal.ConfigMapper.java

/**
 * Use this method to automatically map a configuration collection to a Configuration holder object. A common
 * use-case would be within a service. Usage example:
 *
 * <pre>/*ww w  . ja v  a 2s. c o m*/
 * {@code
 *   public void modified(Map<String, Object> properties) {
 *     YourConfig config = ConfigMapper.as(properties, YourConfig.class);
 *   }
 * }
 * </pre>
 *
 *
 * @param properties The configuration map.
 * @param configurationClass The configuration holder class. An instance of this will be created so make sure that
 *            a default constructor is available.
 * @return The configuration holder object. All fields that matched a configuration option are set. If a required
 *         field is not set, null is returned.
 */
public static <T> @Nullable T as(Map<String, Object> properties, Class<T> configurationClass) {
    T configuration = null;
    try {
        configuration = configurationClass.newInstance();
    } catch (InstantiationException | IllegalAccessException ex) {
        return null;
    }

    List<Field> fields = getAllFields(configurationClass);
    for (Field field : fields) {
        // Don't try to write to final fields and ignore transient fields
        if (Modifier.isFinal(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) {
            continue;
        }
        String fieldName = field.getName();
        String configKey = fieldName;
        Class<?> type = field.getType();

        Object value = properties.get(configKey);

        // Consider RequiredField annotations
        if (value == null) {
            logger.trace("Skipping field '{}', because config has no entry for {}", fieldName, configKey);
            continue;
        }

        // Allows to have List<int>, List<Double>, List<String> etc
        if (value instanceof Collection) {
            Collection<?> c = (Collection<?>) value;
            Class<?> innerClass = (Class<?>) ((ParameterizedType) field.getGenericType())
                    .getActualTypeArguments()[0];
            final List<Object> lst = new ArrayList<>(c.size());
            for (final Object it : c) {
                final Object normalized = objectConvert(it, innerClass);
                lst.add(normalized);
            }
            value = lst;
        }

        try {
            value = objectConvert(value, type);
            logger.trace("Setting value ({}) {} to field '{}' in configuration class {}", type.getSimpleName(),
                    value, fieldName, configurationClass.getName());
            FieldUtils.writeField(configuration, fieldName, value, true);

        } catch (Exception ex) {
            logger.warn("Could not set field value for field '{}': {}", fieldName, ex.getMessage(), ex);
        }
    }

    return configuration;
}

From source file:com.abiquo.model.util.ModelTransformer.java

public static <T> void transform(final Class sourceClass, final Class<T> targetClass, final Object source,
        final T target) throws Exception {
    Field[] transportFields = sourceClass.getDeclaredFields();
    Class superClass = sourceClass.getSuperclass();
    while (!superClass.getSimpleName().equalsIgnoreCase("SingleResourceTransportDto")) {
        transportFields = (Field[]) ArrayUtils.addAll(transportFields, superClass.getDeclaredFields());
        superClass = superClass.getSuperclass();
    }/*from   w  w  w.j  a va 2  s . c  om*/

    for (Field field : transportFields) {

        int modifiers = field.getModifiers();
        if (!Modifier.isTransient(modifiers) && !Modifier.isStatic(modifiers)) {
            String name = field.getName();
            try {
                if (fieldExist(name, targetClass) && fieldExist(name, source.getClass())
                        || getterExist(name, source.getClass())
                                && setterExist(name, targetClass, field.getType())) {
                    Object value = getter(name, source.getClass()).invoke(source, new Object[0]);

                    if (setterExist(name, targetClass, field.getType())) {
                        setter(name, targetClass, field.getType()).invoke(target, new Object[] { value });
                    }
                }
            } catch (InvocationTargetException e) {
                // Ignore invalid field
            }
        }

    }

}

From source file:ch.algotrader.util.FieldUtil.java

/**
 * Returns all non-static / non-transient Fields including Fields defined by superclasses of the defined {@code type}
 *///from   www. j  av  a 2 s  .c om
public static List<Field> getAllFields(Class<?> type) {

    List<Field> fields = new ArrayList<>();

    while (true) {

        for (Field field : type.getDeclaredFields()) {
            if (!Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers())) {
                setAccessible(field);
                fields.add(field);
            }
        }

        type = type.getSuperclass();
        if (type == Object.class || type == null) {
            break;
        }
    }

    return fields;
}