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.luna.common.utils.ReflectUtils.java

public static Object getFieldValue(Object target, String fieldName, boolean isForce)
        throws NoSuchFieldException, IllegalAccessException {
    Field field = target.getClass().getDeclaredField(fieldName);
    field.setAccessible(isForce);//from   w  ww.  j  a v a2  s  .c o  m

    return field.get(target);
}

From source file:de.erdesignerng.visual.Java3DUtils.java

private static void addLibraryPath(String pathToAdd) throws Exception {
    Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
    usrPathsField.setAccessible(true);//from   ww w.j  a va  2  s . c  om

    //get array of paths
    String[] paths = (String[]) usrPathsField.get(null);

    //check if the path to add is already present
    for (String path : paths) {
        if (path.equals(pathToAdd)) {
            return;
        }
    }

    //add the new path
    String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
    newPaths[newPaths.length - 1] = pathToAdd;
    usrPathsField.set(null, newPaths);
}

From source file:shiver.me.timbers.spring.security.WrappedUsernamePasswordAuthenticationFilterTest.java

private static Object extractFiledValue(Class type, Object object, String fieldName)
        throws IllegalAccessException {
    if (Object.class.equals(type)) {
        return null;
    }//from  w  ww  . j  a va 2s .c  o  m

    try {
        final Field field = type.getDeclaredField(fieldName);
        field.setAccessible(true);
        return field.get(object);
    } catch (NoSuchFieldException e) {
        return extractFiledValue(type.getSuperclass(), object, fieldName);
    }
}

From source file:org.wso2.carbon.ganalytics.publisher.GoogleAnalyticsDataPublisher.java

private static String getFieldValue(Field f, Object o) {
    try {//w  w w.j  ava  2  s . c o m
        Object value = f.get(o);
        if (value instanceof String) {
            return (String) value;
        } else {
            if (value == null) {
                return null;
            } else {
                return String.valueOf(value);
            }
        }
    } catch (IllegalAccessException e) {
        log.error("Error while obtaining field value. " + e.getMessage());
    }

    return null;
}

From source file:me.j360.dubbo.modules.util.reflect.ReflectionUtil.java

/**
 * ?Field, ?, ??getter.//  w  ww.ja va  2  s  . c  o m
 */
public static <T> T getFieldValue(final Object obj, final Field field) {
    try {
        return (T) field.get(obj);
    } catch (Exception e) {
        throw convertReflectionExceptionToUnchecked(e);
    }
}

From source file:Main.java

/**
 * Returns the value in the current thread's copy of this
 * thread-local variable.  If the variable has no value for the
 * current thread, it is first initialized to the value returned
 * by an invocation of the {@link #initialValue} method.
 *
 * @return the current thread's value of this thread-local
 * @throws NoSuchMethodException //from  www.ja va 2 s  .c  o m
 * @throws SecurityException 
 * @throws InvocationTargetException 
 * @throws IllegalAccessException 
 * @throws IllegalArgumentException 
 * @throws NoSuchFieldException 
 */
@SuppressWarnings("unchecked")
public static <T extends Object> T get(Thread thread, ThreadLocal<T> threadLocal)
        throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException, NoSuchFieldException {
    Object map = getMap(thread); // ThreadLocalMap
    if (map != null) {
        // calling getEntry returns a ThreadLocal.Entry instance, which is a
        // mapping from a ThreadLocal to an Entry, and an Entry is an 
        // extension of WeakReference.
        // ThreadLocalMap.Entry e = map.getEntry(this);
        Method getEntryMethod = map.getClass().getDeclaredMethod("getEntry", new Class[] { ThreadLocal.class });
        getEntryMethod.setAccessible(true);
        Object entry = getEntryMethod.invoke(map, new Object[] { threadLocal }); // ThreadLocalMap.Entry

        if (entry != null) {
            Field valueField = entry.getClass().getDeclaredField("value");
            valueField.setAccessible(true);
            return (T) valueField.get(entry);
        }
    }
    return setInitialValue(thread, threadLocal);
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c = null;/*from  ww w .  ja v a  2s  .c  o  m*/
    Object obj = null;
    Field field = null;
    int x = 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());
        return context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        e1.printStackTrace();
        return 75;
    }
}

From source file:com.snapdeal.archive.util.ArchivalUtil.java

/**
 * Returns a map where key is string and value is object. Caution should be made to use this method iff the property is String 
 * @param objects/*from  w ww  .  j a  v a  2 s  .  c  o m*/
 * @param propertyName
 * @return
 */
public static <K, T> Map<K, T> getPropertyObjectMap(List<T> objects, String propertyName,
        Class<K> propertyClass) {
    Map<K, T> propertyObjectMap = new HashMap<>();
    for (T t : objects) {
        Object value = null;
        try {
            Field field = t.getClass().getDeclaredField(propertyName);
            field.setAccessible(true);
            value = field.get(t);
            // value = PropertyUtils.getProperty(t, propertyName);
            K k = (K) propertyClass.cast(value);
            propertyObjectMap.put(k, t);
        } catch (IllegalAccessException | NoSuchFieldException | SecurityException e) {
            e.printStackTrace();
        }
    }
    return propertyObjectMap;
}

From source file:Main.java

@TargetApi(4)
public static boolean isTabletDevice(android.content.Context activityContext) {
    if (!androidAPIover(4)) //VERSION
        return false; //If there is a tablet running 1.5.....GOD HELP YOU

    try {/*ww w  .j  av  a  2  s.  c  o  m*/

        // Verifies if the Generalized Size of the device is XLARGE to be
        // considered a Tablet
        Configuration config = activityContext.getResources().getConfiguration();
        Log.i("screenlayout",
                Integer.parseInt(
                        Configuration.class.getDeclaredField("SCREENLAYOUT_SIZE_LARGE").get(config).toString())
                        + "!!!");
        boolean xlarge =
                //activityContext.getResources().getConfiguration().screenLayout &
                Boolean.parseBoolean(config.getClass().getField("screenLayout").get(config).toString())
                        & getStaticInt(config, "SCREENLAYOUT_SIZE_MASK") >= //Changed this from == to >= because my tablet was returning 8 instead of 4.
                        Integer.parseInt(Configuration.class.getDeclaredField("SCREENLAYOUT_SIZE_LARGE")
                                .get(config).toString());
        getStaticInt(config, "SCREENLAYOUT_SIZE_MASK");

        // If XLarge, checks if the Generalized Density is at least MDPI (160dpi)
        if (xlarge) {
            android.util.DisplayMetrics metrics = new android.util.DisplayMetrics();
            Activity activity = (Activity) activityContext;
            activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

            //This next block lets us get constants that are not available in lower APIs.
            // If they aren't available, it's safe to assume that the device is not a tablet.
            // If you have a tablet or TV running Android 1.5, what the fuck is wrong with you.
            int xhigh = -1, tv = -1;
            try {
                Field f = android.util.DisplayMetrics.class.getDeclaredField("DENSITY_XHIGH");
                xhigh = (Integer) f.get(null);
                f = android.util.DisplayMetrics.class.getDeclaredField("DENSITY_TV");
                xhigh = (Integer) f.get(null);
            } catch (Exception e) {
            }

            // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160, DENSITY_TV=213, DENSITY_XHIGH=320
            int densityDpi = Integer
                    .parseInt(metrics.getClass().getDeclaredField("densityDpi").get(metrics).toString());
            if (densityDpi == 240 || //DENSITY_HIGH
                    densityDpi == 160 || //DENSITY_MEDIUM
                    //densityDpi == android.util.DisplayMetrics.DENSITY_DEFAULT ||
                    //densityDpi == android.util.DisplayMetrics.DENSITY_HIGH ||
                    //densityDpi == android.util.DisplayMetrics.DENSITY_MEDIUM ||
                    densityDpi == tv || densityDpi == xhigh) {
                return true;
            }
        }
    } catch (Exception e) {
    }
    return false;
}

From source file:api_proto3.TestElf.java

public static HikariPool getPool(HikariDataSource ds) {
    try {/*from ww w  .  j  a  v a 2s  . com*/
        Field field = ds.getClass().getDeclaredField("pool");
        field.setAccessible(true);
        return (HikariPool) field.get(ds);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}