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:de.javakaffee.web.msm.integration.TestUtils.java

public static void assertEqualDeclaredFields(final Class<? extends Object> clazz, final Object one,
        final Object another, final Map<Object, Object> alreadyChecked) {
    for (final Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);//from   www  .j  av a 2s. c  om
        if (!Modifier.isTransient(field.getModifiers())) {
            try {
                assertDeepEquals(field.get(one), field.get(another), alreadyChecked);
            } catch (final IllegalArgumentException e) {
                throw new RuntimeException(e);
            } catch (final IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

From source file:utils.ReflectionService.java

public static Map createClassModel(Class clazz, int maxDeep) {
    Map map = new HashMap();
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);/*from w w  w.java2 s. c o  m*/
        String key = field.getName();
        try {
            Class<?> type;
            if (Collection.class.isAssignableFrom(field.getType())) {
                ParameterizedType collectionType = (ParameterizedType) field.getGenericType();
                type = (Class<?>) collectionType.getActualTypeArguments()[0];
            } else {
                type = field.getType();
            }

            //            System.out.println(key + " ReflectionResource.createClassModel ==== putting type: " + field.getType().getName() + ", static: "
            //            + Modifier.isStatic(field.getModifiers()) + ", enum: " + type.isEnum() + ", java.*: " + type.getName().startsWith("java"));

            if (Modifier.isStatic(field.getModifiers())) {
                continue;
            } else if (type.isEnum()) {
                map.put(key, Enum.class.getName());
            } else if (!type.getName().startsWith("java") && !key.startsWith("_") && maxDeep > 0) {
                map.put(key, createClassModel(type, maxDeep - 1));
            } else {
                map.put(key, type.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return map;
}

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);//from  w ww .  j  a  v a2s .c o  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  w  w w.  j  a  va 2 s  .co  m
 * @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:org.apache.openjpa.enhance.Reflection.java

/**
 * Gets values of all field f the given class such that f exactly 
 * match the given modifiers and are of given type (Object implies any type)
 * unless f is annotated as {@link Reflectable}. 
 *   //from   ww  w.j  av  a 2 s.  com
 */
public static <T> Set<T> getFieldValues(Class c, int mods, Class<T> t) {
    if (c == null)
        return Collections.EMPTY_SET;
    Field[] fields = c.getFields();
    if (fields == null || fields.length == 0)
        return Collections.EMPTY_SET;
    Set<T> result = new TreeSet<T>();
    for (Field f : fields) {
        if (mods == f.getModifiers() && (t == Object.class || t.isAssignableFrom(f.getType()))
                && canReflect(f)) {
            try {
                result.add((T) f.get(null));
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            }
        }
    }
    return result;
}

From source file:org.agiso.core.lang.util.ObjectUtils.java

/**
 * Metoda generujca reprezentacj acuchow obiektu. Przeglda wszystkie
 * pola obiektu i pobiera ich reprezentacj acuchow. Na tej podstawie
 * generuje acuch wynikowy.//from   ww  w  .  j  a  v  a2s. co m
 * 
 * @param clazz
 * @param object
 */
private static void toStringObject(Class<?> clazz, Object object) {
    StringBuffer buffer = (StringBuffer) ThreadUtils.getAttribute(TOSTRING_TCBUFF);

    String hexHash = Integer.toHexString(System.identityHashCode(object));
    if (0 == buffer.length()) {
        buffer.append(clazz.getSimpleName()).append('@').append(hexHash).append(TOSTRING_PREFIX);
    }

    @SuppressWarnings("unchecked")
    Set<String> converted = (Set<String>) ThreadUtils.getAttribute(TOSTRING_TCATTR);
    converted.add(object.getClass().getCanonicalName() + "@" + hexHash);

    // Rekurencyjne przegldanie wszystkich klas nadrzdnych:
    Class<?> superClass = clazz.getSuperclass();
    if (superClass != null) {
        toStringObject(superClass, object);
    }

    String hyphen = "";
    if (TOSTRING_PREFIX.charAt(0) != buffer.charAt(buffer.length() - 1)) {
        hyphen = TOSTRING_HYPHEN;
    }

    for (Field field : clazz.getDeclaredFields()) {
        try {
            field.setAccessible(true);
            if (field.isAnnotationPresent(InToString.class)) {
                InToString inToString = field.getAnnotation(InToString.class);
                if (!inToString.ignore()) {
                    String name = inToString.name();
                    buffer.append(hyphen).append((name.length() > 0) ? name : field.getName())
                            .append(TOSTRING_COLON);
                    toStringField(field.get(object));
                    hyphen = TOSTRING_HYPHEN;
                }
            } else if ((field.getModifiers() & (Modifier.STATIC | Modifier.FINAL)) == 0) {
                buffer.append(hyphen).append(field.getName()).append(TOSTRING_COLON);
                toStringField(field.get(object));
                hyphen = TOSTRING_HYPHEN;
            }
        } catch (Exception e) {
            // TODO: Zaimplementowa logowanie wyjtku
            e.printStackTrace();
        }
    }
}

From source file:org.codehaus.griffon.commons.GriffonClassUtils.java

/**
 * Work out if the specified object has a public field with the name supplied.
 *
 * @param obj//from w w  w . j a v a  2  s . c o  m
 * @param name
 * @return True if a public field with the name exists
 */
public static boolean isPublicField(Object obj, String name) {
    Class clazz = obj.getClass();
    Field f = null;
    try {
        f = clazz.getDeclaredField(name);
        return Modifier.isPublic(f.getModifiers());
    } catch (NoSuchFieldException e) {
        return false;
    }
}

From source file:grails.util.GrailsClassUtils.java

/**
 * Work out if the specified object has a public field with the name supplied.
 *
 * @param obj//  w  w w.  ja  v a 2  s. c o  m
 * @param name
 * @return true if a public field with the name exists
 */
public static boolean isPublicField(Object obj, String name) {
    Class<?> clazz = obj.getClass();
    try {
        Field f = clazz.getDeclaredField(name);
        return Modifier.isPublic(f.getModifiers());
    } catch (NoSuchFieldException e) {
        return false;
    }
}

From source file:com.opengamma.financial.analytics.curve.CurveNodeIdMapper.java

/**
 * Gets all fields used by the Fudge builder.
 * @return The fields//  w  w w  .  j a va  2 s  .  c o  m
 */
protected static List<String> getCurveIdMapperNames() {
    final List<String> list = new ArrayList<>();
    for (final Field field : CurveNodeIdMapperBuilder.class.getDeclaredFields()) {
        if (Modifier.isStatic(field.getModifiers()) && field.isSynthetic() == false) {
            field.setAccessible(true);
            try {
                list.add((String) field.get(null));
            } catch (final Exception ex) {
                // Ignore
            }
        }
    }
    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
    return ImmutableList.copyOf(list);
}

From source file:org.apache.openjpa.enhance.Reflection.java

/**
 * Return the value of the given field in the given object.
 *///from  www  .  j ava 2s.c o m
public static int getInt(Object target, Field field) {
    if (target == null || field == null)
        return 0;
    makeAccessible(field, field.getModifiers());
    try {
        return field.getInt(target);
    } catch (Throwable t) {
        throw wrapReflectionException(t, _loc.get("get-field", target, field));
    }
}