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:com.ms.commons.test.common.ReflectUtil.java

public static boolean isValueEqualsBean(Object bean, String field, Object value) {
    Class<?> clazz = bean.getClass();
    try {//w ww.  j av a2 s .  c o m
        Field f = getDeclaredField(clazz, NamingUtil.dbNameToJavaName(field));
        f.setAccessible(true);
        try {
            Object beanValue = f.get(bean);
            Object fieldValue = TypeConvertUtil.convert(f.getType(), value);
            return CompareUtil.isObjectEquals(beanValue, fieldValue);
        } catch (IllegalArgumentException e) {
            throw new UnknowException(e);
        } catch (IllegalAccessException e) {
            throw new UnknowException(e);
        }
    } catch (SecurityException e) {
        throw new UnknowException(e);
    } catch (NoSuchFieldException e) {
        throw new JavaFieldNotFoundException(clazz, field);
    }
}

From source file:de.cosmocode.palava.salesforce.sync.NullFieldCollector.java

private static void collect(Set<String> nullFields, Object object) {
    LOG.trace("Collecting null fields on {}", object);
    final Field[] fields = object.getClass().getDeclaredFields();
    for (Field field : fields) {
        if (Modifier.isStatic(field.getModifiers()))
            continue;
        if ("fieldsToNull".equals(field.getName()))
            continue;
        try {//from w  ww  .  ja  v  a2  s .c om
            final boolean accessible = field.isAccessible();
            field.setAccessible(true);
            final Object value = field.get(object);
            field.setAccessible(accessible);
            if (value == null) {
                continue;
            } else if (value instanceof JAXBElement<?>) {
                final JAXBElement<?> jaxb = JAXBElement.class.cast(value);
                if (jaxb.getValue() == null) {
                    nullFields.add(nameOf(field));
                }
            }
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(e);
        }
    }
    LOG.trace("Null fields on {}: {}", object, nullFields);
}

From source file:com.junly.common.util.ReflectUtils.java

/**
 * <p class="detail">//from  ww  w  .  j ava 2 s  . co  m
 * ?
 * </p>
 * @author wan.Dong
 * @date 20161112 
 * @param obj   
 * @param name   ??
 * @return
 */
public static Object getProperty(Object obj, String name) {
    if (obj != null) {
        Class<?> clazz = obj.getClass();
        while (clazz != null) {
            Field field = null;
            try {
                field = clazz.getDeclaredField(name);
            } catch (Exception e) {
                clazz = clazz.getSuperclass();
                continue;
            }
            try {
                field.setAccessible(true);
                return field.get(obj);
            } catch (Exception e) {
                return null;
            } finally {
                field.setAccessible(false);
            }
        }
    }
    return null;
}

From source file:com.playersun.jbf.common.utils.reflex.Reflections.java

public static Map<String, Object> bean2Map(Object... objects) {
    Map<String, Object> map = new HashMap<String, Object>();

    for (Object object : objects) {
        if (object == null)
            continue;

        if (object instanceof Map) {
            map.putAll((Map) object);
        } else {// w  w  w. j  av a  2s . co m
            Validate.notNull(object, "object can't be null");
            for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass
                    .getSuperclass()) {
                try {
                    for (Field field : superClass.getDeclaredFields()) {
                        makeAccessible(field);
                        if (field.get(object) != null) {
                            map.put(field.getName(), field.get(object));
                        }
                    }
                } catch (Exception e) {//NOSONAR
                    // Field??,?
                    continue;// new add
                }
            }
        }
    }

    return map;
}

From source file:gov.va.vinci.leo.tools.LeoUtils.java

/**
 * Create the set of ConfigurationParameter objects from the Param class of an object.
 *
 * @param c Param class//from   w  ww  .jav a  2  s  .co m
 * @return Set of ConfigurationParameter objects
 */
public static Set<ConfigurationParameter> getStaticConfigurationParameters(Class c) {
    Set<ConfigurationParameter> list = new HashSet<ConfigurationParameter>();
    Field[] fields = c.getFields();
    for (Field field : fields) {
        try {
            if (field.getType().equals(ConfigurationParameter.class)
                    && Modifier.isStatic(field.getModifiers())) {
                list.add((ConfigurationParameter) field.get(null));
            }
        } catch (IllegalAccessException e) {
            // Handle exception here
        }
    }
    return list;
}

From source file:kilim.Fiber.java

static private void stateToString(StringBuilder sb, State s) {
    if (s == PAUSE_STATE) {
        sb.append("PAUSE\n");
        return;/*  www .  j a v  a2  s . c o m*/
    }
    Field[] fs = s.getClass().getFields();
    for (int i = 0; i < fs.length; i++) {
        Field f = fs[i];
        sb.append(f.getName()).append(" = ");
        Object v;
        try {
            v = f.get(s);
        } catch (IllegalAccessException iae) {
            v = "?";
        }
        sb.append(' ').append(v).append(' ');
    }
    sb.append('\n');
}

From source file:com.github.hibatis.ReflectionUtils.java

/**
 * ?, private/protected, ??getter./*  w  ww . j  a va  2  s  .  c  o  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 field [" + 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.yahoo.egads.data.JsonEncoder.java

public static void // modifies json_out
        toJson(Object object, JSONStringer json_out) throws Exception {
    json_out.object();//from  w  w  w  . ja v  a  2 s. com
    // for each inherited class...
    for (Class c = object.getClass(); c != Object.class; c = c.getSuperclass()) {
        // for each member variable... 
        Field[] fields = c.getDeclaredFields();
        for (Field f : fields) {
            // if variable is static/private... skip it
            if (Modifier.isStatic(f.getModifiers())) {
                continue;
            }
            if (Modifier.isPrivate(f.getModifiers())) {
                continue;
            }
            Object value = f.get(object);

            // if variable is a complex type... recurse on sub-objects
            if (value instanceof JsonAble) {
                json_out.key(f.getName());
                ((JsonAble) value).toJson(json_out);
                // if variable is an array... recurse on sub-objects
            } else if (value instanceof ArrayList) {
                json_out.key(f.getName());
                json_out.array();
                for (Object e : (ArrayList) value) {
                    toJson(e, json_out);
                }
                json_out.endArray();
                // if variable is a simple type... convert to json
            } else {
                json_out.key(f.getName()).value(value);
            }
        }
    }
    json_out.endObject();
}

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

/**
 * @param className ??//from   w w w  . j a v a2  s.c  o m
 * @param fieldName ?17
 * @return 171717
 */
public static Object getStaticProperty(String className, String fieldName)

{
    Object property = null;
    try {
        Class ownerClass = Class.forName(className);
        Field field = ownerClass.getField(fieldName);
        property = field.get(ownerClass);
    } catch (Exception ex) {
        log.error(ex);
    }
    return property;
}

From source file:com.koda.integ.hbase.util.CacheableSerializer.java

@SuppressWarnings("unchecked")
public static void setHFileDeserializer() {
    Field field = getProtectedField(HFileBlock.class, "blockDeserializer");

    if (field == null) {
        LOG.error("Could not get access to HFileBlock.blockDeserializer");
        return;/*from www . j  a  v  a  2  s . com*/
    }

    try {
        CacheableDeserializer<Cacheable> serde = (CacheableDeserializer<Cacheable>) field.get(null);
        if (serde != null) {
            deserializer.set(serde);
        } else {
            LOG.warn("HFileBlock.blockDeserializer is null");
        }
    } catch (Exception e) {
        LOG.warn("unable to read HFileBlock.blockDeserializer");
    }

}