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.npower.dm.util.BeanHelper.java

/**
 * private/protected/*from  w  w w  .  j a  va2s.  co m*/
 */
static public Object getPrivateProperty(Object object, String propertyName)
        throws IllegalAccessException, NoSuchFieldException {
    assert object != null;
    assert StringUtils.isNotEmpty(propertyName);
    Field field = object.getClass().getDeclaredField(propertyName);
    field.setAccessible(true);
    return field.get(object);
}

From source file:Main.java

/**
 * Stores the classes 'static final' field values as a map.
 * //ww  w  .ja  va2s.com
 * @param clazz
 *            The class containing static field values.
 * @return A map keyed by static field name to value.
 */
public static Map<String, Object> constantsAsMap(Class<?> clazz) {
    try {
        final Map<String, Object> constants = new HashMap<String, Object>();
        final int staticFinalMods = Modifier.STATIC | Modifier.FINAL;
        for (Field field : clazz.getFields()) {
            if (staticFinalMods == (field.getModifiers() & staticFinalMods)) {
                // this is a constant!
                constants.put(field.getName(), field.get(null));
            }
        }

        return constants;
    } catch (Exception e) {
        // wrap in general error
        throw new IllegalStateException("Unable to initialize class constants for: " + clazz);
    }
}

From source file:com.datatorrent.stram.util.WebServicesClientTest.java

private static CredentialsProvider getCredentialsProvider()
        throws NoSuchFieldException, IllegalAccessException {
    Field field = WebServicesClient.class.getDeclaredField(CREDENTIALS_PROVIDER_FIELD);
    field.setAccessible(true);//from   w w w. j  av a  2s. c o m
    CredentialsProvider credentials = (CredentialsProvider) field.get(null);
    field.setAccessible(false);
    return credentials;
}

From source file:org.onebusaway.io.client.test.UriAssert.java

protected static URI getUriFromRequest(RequestBase actualRequest)
        throws NoSuchFieldException, IllegalAccessException {
    Field uriField = RequestBase.class.getDeclaredField("mUri");
    uriField.setAccessible(true);//from w  w  w  . j  av a2  s  .c om
    URI actualUri = (URI) uriField.get(actualRequest);
    return actualUri;
}

From source file:com.hortonworks.minicluster.util.ReflectionUtils.java

/**
 *
 * @param instance//from   w w  w .ja  va 2 s. com
 * @param fieldName
 * @return
 */
public static Object getFieldValue(Object instance, String fieldName) {
    Field f = getFieldAndMakeAccessible(instance.getClass(), fieldName);
    try {
        if (f != null) {
            return f.get(instance);
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
    return null;
}

From source file:com.funtl.framework.smoke.core.modules.act.service.creator.RuntimeActivityCreatorSupport.java

private static void copyFields(Object source, Object target, String... fieldNames) {
    Assert.assertNotNull(source);/*from  w ww  .j a v  a 2 s  . co  m*/
    Assert.assertNotNull(target);
    Assert.assertSame(source.getClass(), target.getClass());

    for (String fieldName : fieldNames) {
        try {
            Field field = FieldUtils.getField(source.getClass(), fieldName, true);
            field.setAccessible(true);
            field.set(target, field.get(source));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.taobao.itest.spring.context.SpringContextManager.java

private static boolean hasBeanInjected(Field field, Object object) {
    field.setAccessible(true);/*from  ww w. j a  v  a2s.  co  m*/
    try {
        return field.get(object) != null;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:grails.plugin.cache.util.ClassUtils.java

/**
 * This method will try to retrieve the value of the named property from the
 * object using a corresponding getter method.  If no getter method is found
 * then this method will look for the corresponding field and return its value.
 *
 * @param object object to inspect//from   w w  w.jav  a 2  s. com
 * @param propertyOrFieldName the name of the field or property to retrieve
 * @return the value of the field or property, null if neither is found
 */
public static Object getPropertyOrFieldValue(Object object, String propertyOrFieldName) {
    final String getterName = GrailsNameUtils.getGetterName(propertyOrFieldName);
    final Class<? extends Object> objectClass = object.getClass();
    try {
        final Method method = objectClass.getMethod(getterName, new Class[0]);
        if (method != null) {
            ReflectionUtils.makeAccessible(method);
            return method.invoke(object, new Object[0]);
        }
    } catch (Exception e) {
    }
    try {
        final Field field = ReflectionUtils.findField(objectClass, propertyOrFieldName);
        if (field != null) {
            ReflectionUtils.makeAccessible(field);
            return field.get(object);
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:com.jkoolcloud.tnt4j.streams.parsers.ActivityJavaObjectParser.java

private static Object getFieldValue(String[] path, Object dataObj, int i) {
    if (ArrayUtils.isEmpty(path) || dataObj == null) {
        return null;
    }/*from  w  w w .  j av a  2  s.c  o m*/

    try {
        Field f = dataObj.getClass().getDeclaredField(path[i]);
        Object obj = f.get(dataObj);

        if (i < path.length - 1 && !f.getType().isPrimitive()) {
            obj = getFieldValue(path, obj, ++i);
        }

        return obj;
    } catch (Exception exc) {
        LOGGER.log(OpLevel.WARNING,
                StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME,
                        "ActivityJavaObjectParser.could.not.get.declared.field"),
                path[i], dataObj.getClass().getSimpleName(), toString(dataObj));
        return null;
    }
}

From source file:org.schedulesdirect.api.utils.HttpUtils.java

static public Header[] scrapeHeaders(Request r) {
    try {//from   ww  w.  ja  va  2s.c o  m
        Field f = r.getClass().getDeclaredField("request");
        f.setAccessible(true);
        HttpRequestBase req = (HttpRequestBase) f.get(r);
        return req.getAllHeaders();
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        LOG.error("Error accessing request headers!", e);
        return new Header[0];
    }
}