Example usage for java.lang.reflect Field get

List of usage examples for java.lang.reflect Field get

Introduction

In this page you can find the example usage for java.lang.reflect Field get.

Prototype

@CallerSensitive
@ForceInline 
public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Returns the value of the field represented by this Field , on the specified object.

Usage

From source file:edu.illinois.ncsa.springdata.SpringData.java

/**
 * Check each field in object to see if it is already seen. This will modify
 * collections, lists and arrays as well as the fields in an object.
 * /*from ww w  .  jav  a  2 s .  co  m*/
 * @param obj
 *            the object to be checked for duplicates.
 * @param seen
 *            objects that have already been checked.
 * @return the object with all duplicates removed
 */
private static <T> T removeDuplicate(T obj, Map<String, Object> seen) {
    if (obj == null) {
        return null;
    }
    if (obj instanceof AbstractBean) {
        String id = ((AbstractBean) obj).getId();
        if (seen.containsKey(id)) {
            return (T) seen.get(id);
        }
        seen.put(id, obj);

        // check all subfields
        for (Field field : obj.getClass().getDeclaredFields()) {
            if ((field.getModifiers() & Modifier.FINAL) == 0) {
                try {
                    field.setAccessible(true);
                    field.set(obj, removeDuplicate(field.get(obj), seen));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    } else if (obj instanceof Collection<?>) {
        Collection col = (Collection) obj;
        ArrayList arr = new ArrayList(col);
        col.clear();
        for (int i = 0; i < arr.size(); i++) {
            Object x = removeDuplicate(arr.get(i), seen);
            col.add(x);
        }
    } else if (obj instanceof Map<?, ?>) {
        Map map = (Map) obj;
        ArrayList<Entry> arr = new ArrayList(map.entrySet());
        map.clear();
        for (int i = 0; i < arr.size(); i++) {
            Object k = removeDuplicate(arr.get(i).getKey(), seen);
            Object v = removeDuplicate(arr.get(i).getValue(), seen);
            map.put(k, v);
        }
    } else if (obj.getClass().isArray()) {
        Object[] arr = (Object[]) obj;
        for (int i = 0; i < arr.length; i++) {
            arr[i] = removeDuplicate(arr[i], seen);
        }
    }

    return obj;
}

From source file:net.umpay.mailbill.util.reflect.ReflectionUtils.java

/**
 * ?, private/protected, ??getter.//from   w  w w.  jav a  2 s.co  m
 */
public static Object getFieldValue(final Object object, final String fieldName) {
    Field field = getDeclaredField(object, fieldName);

    if (field == null) {
        throw new IllegalArgumentException(
                "Could not find field [" + fieldName + "] on target [" + object + "]");
    }

    makeAccessible(field);

    Object result = null;
    try {
        result = field.get(object);
    } catch (IllegalAccessException e) {
        logger.error("??{}", e);
    }
    return result;
}

From source file:com.rapid.develop.core.utils.ReflectionUtils.java

/**
 * ?, private/protected, ??getter./* w w w  . j a v a2s.co  m*/
 */
public static Object getFieldValue(final Object obj, final String fieldName) {
    Field field = getAccessibleField(obj, fieldName);

    if (field == null) {
        throw new IllegalArgumentException("Could not find fields [" + fieldName + "] on target [" + obj + "]");
    }

    Object result = null;
    try {
        result = field.get(obj);
    } catch (IllegalAccessException e) {
        logger.error("??{}", e.getMessage());
    }
    return result;
}

From source file:com.smf.platform.util.ReflectionUtils.java

/**
 * ?, private/protected, ??getter.// w  w  w.  j a v  a 2s  .  c o  m
 */
public static Object getFieldValue(final Object object, final String fieldName) {
    Field field = getDeclaredField(object, fieldName);

    if (field == null) {
        throw new IllegalArgumentException(
                "Could not find field [" + fieldName + "] on target [" + object + "]");
    }

    makeAccessible(field);

    Object result = null;
    try {
        result = field.get(object);
    } catch (IllegalAccessException e) {
        //         logger.error("??{}", e.getMessage());
    }
    return result;
}

From source file:com.austin.base.commons.util.ReflectUtil.java

/**
 * @param object/*from   www .j  a  v a  2s.  co  m*/
 * @param propertyName
 * @return
 * @throws NoSuchFieldException
 */
public static Object forceGetProperty(Object object, String propertyName) throws NoSuchFieldException {
    Assert.notNull(object);
    Assert.hasText(propertyName);

    Field field = getDeclaredField(object, propertyName);

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

    Object result = null;
    try {
        result = field.get(object);
    } catch (IllegalAccessException e) {
        log.info("error wont' happen");
    }
    field.setAccessible(accessible);
    return result;
}

From source file:com.vanstone.common.util.ReflectionUtils.java

/**
 * ?, private/protected, ??getter./*w w w  . j  a v a 2 s .  c  o m*/
 */
public static Object getFieldValue(final Object object, final String fieldName) {
    Field field = getDeclaredField(object, fieldName);

    if (field == null) {
        throw new IllegalArgumentException(
                "Could not find field [" + fieldName + "] on target [" + object + "]");
    }

    makeAccessible(field);

    Object result = null;
    try {
        result = field.get(object);
    } catch (IllegalAccessException e) {
        LOG.error("??{}" + e.getMessage());
    }
    return result;
}

From source file:de.codesourcery.luaparser.LuaToJSON.java

private static int getOffset(JSONTokener tokener)
        throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {

    for (Field f : JSONTokener.class.getDeclaredFields()) {
        if (f.getName().equals("index")) {
            f.setAccessible(true);//from   w  ww .j  a  va2 s.co m
            return ((Number) f.get(tokener)).intValue();
        }
    }
    throw new RuntimeException("Internal error,failed to find field 'index' in JSONTokener");
}

From source file:de.micromata.genome.util.runtime.ClassUtils.java

/**
 * Set all String field to empty on current class.
 *
 * @param object the object//from w w  w.j  av  a2s  .  co m
 * @param exceptClass the except class
 * @throws IllegalArgumentException the illegal argument exception
 * @throws IllegalAccessException the illegal access exception
 */
// CHECKSTYLE.OFF com.puppycrawl.tools.checkstyle.checks.metrics.CyclomaticComplexityCheck Trivial code.
public static void fillDefaultEmptyFieldsIfEmpty(Object object, Class<?>... exceptClass) // NOSONAR "Methods should not be too complex" trivial
        throws IllegalArgumentException, IllegalAccessException {
    Class<?> currentClazz = object.getClass();
    List exClazzes = Arrays.asList(exceptClass);
    while (currentClazz.getSuperclass() != null) {
        if (exClazzes.contains(currentClazz) == false) {
            Field[] fields = currentClazz.getDeclaredFields();
            for (Field field : fields) {
                if (String.class.equals(field.getType())) {
                    field.setAccessible(true);
                    if (field.get(object) == null) {
                        field.set(object, "");
                    }
                }
                if (Integer.class.equals(field.getType())) {
                    field.setAccessible(true);
                    if (field.get(object) == null) {
                        field.set(object, 0);
                    }
                }
                if (BigDecimal.class.equals(field.getType())) {
                    field.setAccessible(true);
                    if (field.get(object) == null) {
                        field.set(object, BigDecimal.ZERO);
                    }
                }
                if (Date.class.equals(field.getType())) {
                    field.setAccessible(true);
                    if (field.get(object) == null) {
                        field.set(object, new Date());
                    }
                }
            }
        }
        currentClazz = currentClazz.getSuperclass();
    }
}

From source file:gov.nih.nci.caarray.dao.SampleDaoImpl.java

private static String getDiscriminator(Class<? extends AbstractBioMaterial> bmClass) {
    String errorMsg = "Not a valid biomaterial class (no DISCRIMINATOR field): ";
    try {/*from  w  ww  .j av  a 2s . com*/
        Field discField = bmClass.getDeclaredField("DISCRIMINATOR");
        return (String) discField.get(null);
    } catch (SecurityException e) {
        throw new IllegalArgumentException(errorMsg + bmClass, e);
    } catch (NoSuchFieldException e) {
        throw new IllegalArgumentException(errorMsg + bmClass, e);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(errorMsg + bmClass, e);
    } catch (IllegalAccessException e) {
        throw new IllegalArgumentException(errorMsg + bmClass, e);
    }
}