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.dangdang.ddframe.job.spring.util.AopTargetUtils.java

private static Object getProxyTargetObjectForCglibAndSpring4(final Object proxy) {
    Field h;
    try {/*from  w  w  w  .ja  v a  2s. c o  m*/
        h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0");
        h.setAccessible(true);
        Object dynamicAdvisedInterceptor = h.get(proxy);
        Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
        advised.setAccessible(true);
        return ((AdvisedSupport) advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget();
        // CHECKSTYLE:OFF
    } catch (final Exception ex) {
        // CHECKSTYLE:ON
        throw new JobException(ex);
    }
}

From source file:edu.teco.smartlambda.container.docker.HttpHijackingWorkaround.java

/**
 * Recursively traverse a hierarchy of fields in classes, obtain their value and continue the traversing on the optained object
 *
 * @param fieldContent     current object to operate on
 * @param classFieldTupels the class/field hierarchy
 *
 * @return the content of the leaf in the traversed hierarchy path
 *//*from ww  w . j  a  v a  2 s. c o m*/
@SneakyThrows // since the expressions are constant the exceptions cannot occur (except when the library is changed, but then a crash
// is favourable)
private static Object getInternalField(final Object fieldContent, final List<String[]> classFieldTupels) {
    Object curr = fieldContent;
    for (final String[] classFieldTuple : classFieldTupels) {
        //noinspection ConstantConditions
        final Field field = Class.forName(classFieldTuple[0]).getDeclaredField(classFieldTuple[1]);
        field.setAccessible(true);
        curr = field.get(curr);
    }
    return curr;
}

From source file:cz.cuni.mff.d3s.spl.example.newton.app.Main.java

private static void inspectClass(Object obj) {
    if (!INSPECT) {
        return;/*w  ww. j ava 2 s  .c  om*/
    }

    System.out.printf("Inspecting %s:\n", obj);
    Class<?> klass = obj.getClass();
    System.out.printf("  Class: %s\n", klass.getName());
    for (Field f : klass.getDeclaredFields()) {
        Object value;
        boolean accessible = f.isAccessible();
        try {
            f.setAccessible(true);
            value = f.get(obj);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            value = String.format("<failed to read: %s>", e.getMessage());
        }
        f.setAccessible(accessible);
        System.out.printf("  Field %s: %s\n", f.getName(), value);
    }
    for (Method m : klass.getDeclaredMethods()) {
        System.out.printf("  Method %s\n", m.getName());
    }
    System.out.printf("-------\n");
    System.out.flush();
}

From source file:Main.java

public static void dumphreadLocals() {
    try {/*www  . ja v a  2s .c  om*/
        // Get a reference to the thread locals table of the current thread
        Thread thread = Thread.currentThread();
        Field threadLocalsField = Thread.class.getDeclaredField("threadLocals");
        threadLocalsField.setAccessible(true);
        Object threadLocalTable = threadLocalsField.get(thread);

        // Get a reference to the array holding the thread local variables
        // inside the
        // ThreadLocalMap of the current thread
        @SuppressWarnings("rawtypes")
        Class threadLocalMapClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
        Field tableField = threadLocalMapClass.getDeclaredField("table");
        tableField.setAccessible(true);
        Object[] table = (Object[]) tableField.get(threadLocalTable);

        @SuppressWarnings("rawtypes")
        Class threadLocalMapEntryClass = Class.forName("java.lang.ThreadLocal$ThreadLocalMap$Entry");
        Field entryValueField = threadLocalMapEntryClass.getDeclaredField("value");
        entryValueField.setAccessible(true);
        // The key to the ThreadLocalMap is a WeakReference object. The
        // referent field of this object
        // is a reference to the actual ThreadLocal variable
        Field referentField = Reference.class.getDeclaredField("referent");
        referentField.setAccessible(true);

        for (Object entry : table) {
            // Each entry in the table array of ThreadLocalMap is an Entry
            // object
            // representing the thread local reference and its value
            if (entry != null) {

                Object tlcValue = entryValueField.get(entry);

                ThreadLocal threadLocal = (ThreadLocal) referentField.get(entry);

                // System.out.println("thread local value "+tlcValue);
                // if(threadLocal)
                printObject(threadLocal, tlcValue);
            }
        }
        System.out.println("__________________________");
    } catch (Exception e) {
        // We will tolerate an exception here and just log it
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c;/*from ww w  .  j  av  a  2  s.  c om*/
    Object obj;
    Field field;
    int x, statusBarHeight = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        statusBarHeight = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return statusBarHeight;
}

From source file:Main.java

private static int getStatusBarHeight(Activity activity) {
    Class<?> c = null;//from w  ww.  j a v a 2  s  . c o  m
    Object obj = null;
    Field field = null;
    int x = 0, sbar = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        sbar = activity.getResources().getDimensionPixelSize(x);
        return sbar;
    } catch (Exception e1) {
        //       loge("get status bar height fail");
        e1.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static Service createService(Context context, ServiceInfo serviceInfo) throws Exception {
    IBinder token = new Binder();

    Class<?> createServiceDataClass = Class.forName("android.app.ActivityThread$CreateServiceData");
    Constructor<?> constructor = createServiceDataClass.getDeclaredConstructor();
    constructor.setAccessible(true);// w w w . j a  v  a 2  s.c  om
    Object createServiceData = constructor.newInstance();

    Field tokenField = createServiceDataClass.getDeclaredField("token");
    tokenField.setAccessible(true);
    tokenField.set(createServiceData, token);

    serviceInfo.applicationInfo.packageName = context.getPackageName();
    Field infoField = createServiceDataClass.getDeclaredField("info");
    infoField.setAccessible(true);
    infoField.set(createServiceData, serviceInfo);

    Class<?> compatibilityClass = Class.forName("android.content.res.CompatibilityInfo");
    Field defaultCompatibilityField = compatibilityClass.getDeclaredField("DEFAULT_COMPATIBILITY_INFO");
    defaultCompatibilityField.setAccessible(true);
    Object defaultCompatibility = defaultCompatibilityField.get(null);
    defaultCompatibilityField.set(createServiceData, defaultCompatibility);

    Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
    Method currentActivityThreadMethod = activityThreadClass.getDeclaredMethod("currentActivityThread");
    Object currentActivityThread = currentActivityThreadMethod.invoke(null);

    Method handleCreateServiceMethod = activityThreadClass.getDeclaredMethod("handleCreateService",
            createServiceDataClass);
    handleCreateServiceMethod.setAccessible(true);
    handleCreateServiceMethod.invoke(currentActivityThread, createServiceData);

    Field mServicesField = activityThreadClass.getDeclaredField("mServices");
    mServicesField.setAccessible(true);
    Map mServices = (Map) mServicesField.get(currentActivityThread);
    Service service = (Service) mServices.get(token);
    mServices.remove(token);

    return service;
}

From source file:jp.terasoluna.fw.beans.jxpath.JXPATH152PatchActivator.java

/**
 * ??/*  w ww .j av a 2 s  .  co m*/
 */
private static void activate() {
    try {
        // ?????Map?
        Field byClassField = JXPathIntrospector.class.getDeclaredField("byClass");
        byClassField.setAccessible(true);
        Map<?, ?> byClass = (Map<?, ?>) byClassField.get(null);

        Field byInterfaceField = JXPathIntrospector.class.getDeclaredField("byInterface");
        byInterfaceField.setAccessible(true);
        Map<?, ?> byInterface = (Map<?, ?>) byInterfaceField.get(null);

        // Map??
        byClassField.set(null, new HashMapForJXPathIntrospector<Object, Object>(byClass));
        byInterfaceField.set(null, new HashMapForJXPathIntrospector<Object, Object>(byInterface));

        log.info("JXPATH-152 Patch activation succeeded.");
    } catch (Exception e) {
        // ?????
        // ???????commons-JXPath?????
        log.fatal("JXPATH-152 Patch activation failed.", e);
    }
}

From source file:hudson.util.ReflectionUtils.java

public static Object getPublicProperty(Object o, String p)
        throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
    PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(o, p);
    if (pd == null) {
        // field?
        try {/*from   www  . j  av a 2  s.c  om*/
            Field f = o.getClass().getField(p);
            return f.get(o);
        } catch (NoSuchFieldException e) {
            throw new IllegalArgumentException("No such property " + p + " on " + o.getClass());
        }
    } else {
        return PropertyUtils.getProperty(o, p);
    }
}

From source file:net.servicefixture.converter.ObjectConverter.java

/**
 * Converts types that are supported by <code>ConvertUtils</code>.
 * // w  w w  .j a  v a  2 s.c  o  m
 * @param data
 * @param destType
 * @return
 */
public static Object specialConvert(String data, Class destType) {
    try {
        //If it is enumeration class.
        if (ReflectionUtils.isEnumarationPatternClass(destType)) {
            try {
                Field field = destType.getField(data);
                return field.get(null);
            } catch (NoSuchFieldException e) {
                //No such field, meaning the variable name and the data
                //doesn't match, call fromString method.
                return destType.getMethod("fromString", new Class[] { String.class }).invoke(null,
                        new Object[] { data });
            }
        }
        //JDK5.0 enum type
        if (destType.isEnum()) {
            return destType.getMethod("valueOf", String.class).invoke(null, data);
        }
    } catch (Exception ignoreIt) {
        ignoreIt.printStackTrace();
    }

    throw new ServiceFixtureException("Unable to convert data:" + data + " to class:" + destType.getName());
}