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.classloader.IntlTestURLClassPath.java

private static URLStreamHandlerFactory getURLStreamHandlerFactory() {
    try {//from w ww .j a  v a 2 s  .c  o m
        Field factoryField = Launcher.class.getDeclaredField("factory");
        factoryField.setAccessible(true);
        return (URLStreamHandlerFactory) factoryField.get(null);
    } catch (Exception e) {
        throw ExceptionUtil.wrapToRuntimeException(e);
    }
}

From source file:com.zfsoft.util.reflect.ReflectionUtils.java

/**
 * ?, private/protected, ??getter./*w w  w. j  a va 2 s .  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 field [" + fieldName + "] on target [" + obj + "]");
    }

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

From source file:com.abssh.util.ReflectionUtils.java

/**
 * ?, private/protected, ??getter./*ww w.j a v a 2 s  .c om*/
 */
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.ms.commons.test.classloader.IntlTestURLClassPath.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private static URL[] getParamArrayOfURL(URLClassPath urlClassPath) {
    try {/*from   w w  w  . j a va2 s  .c om*/
        Field pathField = urlClassPath.getClass().getDeclaredField("path");
        pathField.setAccessible(true);
        List<URL> path = (List) pathField.get(urlClassPath);

        path = dealWithMaevnTest(path);
        path = filterMavenSurefireBooter(path);
        path = adjustJarIndex(path);

        getCallCopyAdditationURLPath();

        List<URL> translatedPath = new ArrayList<URL>(path.size());
        for (URL url : path) {
            translatedPath.add(translateURL(url));
        }

        System.err.println("Final classpath url(s):" + translatedPath);

        return translatedPath.toArray(new URL[0]);
    } catch (Exception e) {
        throw ExceptionUtil.wrapToRuntimeException(e);
    }
}

From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java

public static Object getField(Class<?> clazz, Object obj, String fieldName)
        throws NoSuchFieldException, IllegalArgumentException {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);// w w  w. j a  v  a  2s .  co  m

    try {
        return field.get(obj);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java

public static Object getField(Object obj, String fieldName)
        throws NoSuchFieldException, IllegalArgumentException {
    Class<?> clazz = obj.getClass();

    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);/* w  w w  .  j  a  va 2 s . c om*/

    try {
        return field.get(obj);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.blackducksoftware.integration.hub.detect.workflow.report.util.ObjectPrinter.java

public static void populateField(final Field field, final String prefix, final Object guy,
        Map<String, String> fieldMap) {
    if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
        return; // don't print static fields.
    }/*from ww  w .  ja va2  s. c o  m*/
    final String name = field.getName();
    String value = "unknown";
    Object obj = null;
    try {
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        obj = field.get(guy);
    } catch (final Exception e) {
        e.printStackTrace();
    }
    boolean shouldPrintObjectsFields = false;
    if (obj == null) {
        value = "null";
    } else {
        value = obj.toString();
        shouldPrintObjectsFields = shouldRecursivelyPrintType(obj.getClass());
    }
    if (!shouldPrintObjectsFields) {
        if (StringUtils.isBlank(prefix)) {
            fieldMap.put(name, value);
        } else {
            fieldMap.put(prefix + "." + name, value);
        }
    } else {
        String nestedPrefix = name;
        if (StringUtils.isNotBlank(prefix)) {
            nestedPrefix = prefix + "." + nestedPrefix;
        }
        populateObject(nestedPrefix, obj, fieldMap);
    }

}

From source file:com.project.framework.util.ReflectionUtils.java

/**
 * ?, private/protected, ??getter./* ww  w.  j  a  v a 2 s. co  m*/
 */
public static Object getFieldValue(final Object object, final String fieldName) {

    Field field = getDeclaredField(object, fieldName);

    Assert.notNull(field, "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.sunchenbin.store.feilong.core.lang.reflect.FieldUtil.java

/**
 *  (?),key fieldName,value ./* ww w .  j av  a 2 s  .  com*/
 *
 * @param obj
 *            the obj
 * @param excludeFieldNames
 *            ?field names,?nullOrEmpty ?
 * @return the field value map
 * @see #getAllFieldList(Object, String[])
 * @see org.apache.commons.lang3.reflect.MemberUtils#setAccessibleWorkaround(AccessibleObject)
 */
public static Map<String, Object> getAllFieldNameAndValueMap(Object obj, String[] excludeFieldNames) {
    List<Field> fieldList = getAllFieldList(obj, excludeFieldNames);

    if (Validator.isNullOrEmpty(fieldList)) {
        return Collections.emptyMap();
    }
    Map<String, Object> map = new TreeMap<String, Object>();
    for (Field field : fieldList) {
        //XXX see org.apache.commons.lang3.reflect.MemberUtils.setAccessibleWorkaround(AccessibleObject)
        field.setAccessible(true);
        try {
            map.put(field.getName(), field.get(obj));
        } catch (Exception e) {
            LOGGER.error(e.getClass().getName(), e);
            throw new ReflectException(e);
        }
    }
    return map;
}

From source file:com.lukakama.serviio.watchservice.watcher.WatcherRunnable.java

private static Object getFieldValue(Object obj, String fieldName) {
    try {// ww  w. j a v a2 s .co m
        Object iWantThis;

        Field f = obj.getClass().getDeclaredField(fieldName); //NoSuchFieldException
        if (!f.isAccessible()) {
            f.setAccessible(true);
            iWantThis = f.get(obj);
            f.setAccessible(false);
        } else {
            iWantThis = f.get(obj);
        }
        return iWantThis;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}