List of usage examples for java.lang.reflect Modifier isTransient
public static boolean isTransient(int mod)
From source file:org.projectforge.framework.xstream.XmlObjectWriter.java
/** * Reader and Writer will ignore static and final fields. * @return true if the field has to be ignored by the writer. *//*from www . ja va2 s . co m*/ public static boolean ignoreField(final Field field) { final int modifiers = field.getModifiers(); return Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers) == true || Modifier.isTransient(modifiers); }
From source file:org.tinygroup.commons.tools.HashCodeUtil.java
private static void reflectionAppend(Object object, Class clazz, HashCodeBuilder builder, boolean useTransients, String[] excludeFields) { if (isRegistered(object)) { return;/*from ww w . j a va 2 s . c o m*/ } try { register(object); Field[] fields = clazz.getDeclaredFields(); List excludedFieldList = excludeFields != null ? Arrays.asList(excludeFields) : Collections.EMPTY_LIST; AccessibleObject.setAccessible(fields, true); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (!excludedFieldList.contains(field.getName()) && (field.getName().indexOf('$') == -1) && (useTransients || !Modifier.isTransient(field.getModifiers())) && (!Modifier.isStatic(field.getModifiers()))) { try { Object fieldValue = field.get(object); builder.append(fieldValue); } catch (IllegalAccessException e) { // this can't happen. Would get a Security exception // instead // throw a runtime exception in case the impossible // happens. throw new InternalError("Unexpected IllegalAccessException"); } } } } finally { unregister(object); } }
From source file:io.fabric8.cxf.endpoint.IgnorePropertiesBackedByTransientFields.java
/** * Returns false if the getter method has a field of the same name which is transient * @return/* w w w . j av a2s. c o m*/ */ protected boolean isGetterMethodWithFieldVisible(Object method, String fieldName, Class<?> declaringClass) { Field field = findField(fieldName, declaringClass); if (field != null) { int fieldModifiers = field.getModifiers(); if (Modifier.isTransient(fieldModifiers)) { if (LOG.isLoggable(Level.FINE)) { LOG.fine("Ignoring getter " + method + " due to transient field called " + fieldName); } return false; } } return true; }
From source file:com.Da_Technomancer.crossroads.API.packets.Message.java
private static boolean acceptField(Field f, Class<?> type) { int mods = f.getModifiers(); if (Modifier.isFinal(mods) || Modifier.isStatic(mods) || Modifier.isTransient(mods)) return false; return handlers.containsKey(type); }
From source file:org.alex73.skarynka.scan.Book2.java
private void set(Object obj, String fieldName, String value, String debug) { try {// w w w.jav a 2 s . co m Field f = obj.getClass().getField(fieldName); if (!Modifier.isPublic(f.getModifiers()) || Modifier.isStatic(f.getModifiers()) || Modifier.isTransient(f.getModifiers())) { errors.add("Field is not public for '" + debug + "'"); return; } if (f.getType() == int.class) { f.setInt(obj, Integer.parseInt(value)); } else if (f.getType() == boolean.class) { f.setBoolean(obj, Boolean.parseBoolean(value)); } else if (f.getType() == String.class) { f.set(obj, value); } else if (Set.class.isAssignableFrom(f.getType())) { TreeSet<String> v = new TreeSet<>(Arrays.asList(value.split(";"))); f.set(obj, v); } else { errors.add("Unknown field class for set '" + debug + "'"); return; } } catch (NoSuchFieldException ex) { errors.add("Unknown field for set '" + debug + "'"); } catch (IllegalAccessException ex) { errors.add("Wrong field for set '" + debug + "'"); } catch (Exception ex) { errors.add("Error set value to field for '" + debug + "'"); } }
From source file:HashCodeAssist.java
/** * <p>//from w w w .j a v a 2s . c o m * Appends the fields and values defined by the given object of the given * <code>Class</code>. * </p> * * @param object * the object to append details of * @param clazz * the class to append details of * @param builder * the builder to append to * @param useTransients * whether to use transient fields * @param excludeFields * Collection of String field names to exclude from use in * calculation of hash code */ private static void reflectionAppend(Object object, Class<?> clazz, HashCodeAssist builder, boolean useTransients, String... excludeFields) { if (isRegistered(object)) { return; } try { register(object); Field[] fields = clazz.getDeclaredFields(); List<String> excludedFieldList = excludeFields != null ? Arrays.asList(excludeFields) : Collections.EMPTY_LIST; AccessibleObject.setAccessible(fields, true); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (!excludedFieldList.contains(field.getName()) && (field.getName().indexOf('$') == -1) && (useTransients || !Modifier.isTransient(field.getModifiers())) && (!Modifier.isStatic(field.getModifiers()))) { try { Object fieldValue = field.get(object); builder.append(fieldValue); } catch (IllegalAccessException e) { // this can't happen. Would get a Security exception // instead // throw a runtime exception in case the impossible // happens. throw new InternalError("Unexpected IllegalAccessException"); } } } } finally { unregister(object); } }
From source file:com.jk.util.JKObjectUtil.java
/** * Checks if is transient./* w w w . j a va 2 s . com*/ * * @param field * the field * @return true, if is transient */ public static boolean isTransient(final Field field) { return Modifier.isTransient(field.getModifiers()); }
From source file:org.tinygroup.commons.tools.EqualsUtil.java
/** * <p>Appends the fields and values defined by the given object of the * given Class.</p>//from w w w .j a v a 2 s. co m * * @param lhs the left hand object * @param rhs the right hand object * @param clazz the class to append details of * @param builder the builder to append to * @param useTransients whether to test transient fields * @param excludeFields array of field names to exclude from testing */ private static void reflectionCompareAppend(Object lhs, Object rhs, Class clazz, EqualsBuilder builder, boolean useTransients, String[] compareFields) { Field[] fields = clazz.getDeclaredFields(); List compareFieldList = compareFields != null ? Arrays.asList(compareFields) : Collections.EMPTY_LIST; AccessibleObject.setAccessible(fields, true); for (int i = 0; i < fields.length && builder.isEquals(); i++) { Field f = fields[i]; if (compareFieldList.contains(f.getName()) && (f.getName().indexOf('$') == -1) && (useTransients || !Modifier.isTransient(f.getModifiers())) && (!Modifier.isStatic(f.getModifiers()))) { try { builder.append(f.get(lhs), f.get(rhs)); } catch (IllegalAccessException e) { //this can't happen. Would get a Security exception instead //throw a runtime exception in case the impossible happens. throw new InternalError("Unexpected IllegalAccessException"); } } } }
From source file:io.s4.comm.util.JSONUtil.java
public static Map<String, Object> getMap(Object obj) { Map<String, Object> map = new HashMap<String, Object>(); if (obj != null) { if (Map.class.isAssignableFrom(obj.getClass())) { return (Map) obj; } else {//from w w w. j a va 2s . c om Field[] fields = obj.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (!fields[i].isAccessible()) { fields[i].setAccessible(true); } try { String name = fields[i].getName(); Object val = fields[i].get(obj); if (!Modifier.isStatic(fields[i].getModifiers()) && !Modifier.isTransient(fields[i].getModifiers())) { if (fields[i].getType().isPrimitive() || knownTypes.contains(fields[i].getType())) { map.put(name, val); } else if (fields[i].getType().isArray()) { int length = Array.getLength(val); Object vals[] = new Object[length]; for (int j = 0; j < length; j++) { Object arrVal = Array.get(val, j); if (arrVal.getClass().isPrimitive() || knownTypes.contains(arrVal.getClass())) { vals[j] = arrVal; } else { vals[j] = getMap(arrVal); } } map.put(name, vals); } else { map.put(name, getMap(val)); } } } catch (Exception e) { throw new RuntimeException("Exception while getting value of " + fields[i], e); } } } } return map; }
From source file:Main.java
public static Collection<Field> getDeepDeclaredFields(Class<?> c) { if (_reflectedFields.containsKey(c)) { return _reflectedFields.get(c); }/*from ww w. ja v a 2 s. c om*/ Collection<Field> fields = new ArrayList<Field>(); Class<?> curr = c; while (curr != null) { try { Field[] local = curr.getDeclaredFields(); for (Field field : local) { if (!field.isAccessible()) { try { field.setAccessible(true); } catch (Exception ignored) { } } int modifiers = field.getModifiers(); if (!Modifier.isStatic(modifiers) && !field.getName().startsWith("this$") && !Modifier.isTransient(modifiers)) { fields.add(field); } } } catch (ThreadDeath t) { throw t; } catch (Throwable ignored) { } curr = curr.getSuperclass(); } _reflectedFields.put(c, fields); return fields; }