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:Main.java

/** 
 * Returns the KeyEvent-code (only for VK_a..Z).
 * If no key-event could be found, {@link Integer#MIN_VALUE} is returned. 
 *//* w ww .j  a  v a 2s.c  om*/
public static int getKeyEvent(Character ch) {

    int ke = Integer.MIN_VALUE;
    try {
        Field f = KeyEvent.class.getField("VK_" + Character.toUpperCase(ch));
        f.setAccessible(true);
        ke = (Integer) f.get(null);
    } catch (Exception e) {
    }
    return ke;
}

From source file:Main.java

public static <T> T getObjInList(Class<T> clazz, Collection<T> all, String valueStr, String propName)
        throws Exception {
    if (all.isEmpty() || valueStr == null) {
        return null;
    }/*  w ww .j  a va2s .c  om*/
    T obj = null;
    Iterator<T> iter = all.iterator();
    for (; iter.hasNext();) {
        T temp = iter.next();
        Object match = null;
        if (propName == null) {
            match = temp.toString();
        } else {
            Field field = clazz.getField(propName);
            field.setAccessible(true);
            match = field.get(temp);
        }
        if (valueStr.equals(match)) {
            obj = temp;
            break;
        }
    }
    return obj;
}

From source file:Main.java

public static <K, V> Map<K, V> collectionToMap(Collection<V> collection, String key) {
    Map<K, V> map = null;/*w  ww. ja v  a2s.c  om*/
    Iterator<V> iterator = collection.iterator();
    try {
        if (key == null)
            throw new Exception("no key filed");
        else {
            map = new HashMap<K, V>();
            while (iterator.hasNext()) {
                V v = iterator.next();
                Class<? extends Object> c = v.getClass();
                Field field = c.getField(key);
                field.setAccessible(true);
                map.put((K) field.get(v), v);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return map;
}

From source file:Main.java

/**
 * Copy fields from parent object to child object.
 *
 * @param parent parent object//w  w  w  .  jav  a  2  s  . c o m
 * @param child child object
 * @param <T> child class
 * @return filled child object
 */
public static <T> T shallowCopy(Object parent, T child) {
    try {
        List<Field> fields = new ArrayList<>();
        Class clazz = parent.getClass();
        do {
            fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
        } while (!(clazz = clazz.getSuperclass()).equals(Object.class));

        for (Field field : fields) {
            field.setAccessible(true);
            field.set(child, field.get(parent));
        }

        return child;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Gets Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD using reflection.
 * The field is since API Level 15.//from  w w  w  .  j a  v a  2s. co m
 */
static boolean isAccessibilitySpeakPasswordEnabled() {
    if (isAccessibilitySpeakPasswordEnabled.isPresent()) {
        return isAccessibilitySpeakPasswordEnabled.get();
    }
    if (Build.VERSION.SDK_INT < 15) {
        return false;
    }
    try {
        Field field = Settings.Secure.class.getField("ACCESSIBILITY_SPEAK_PASSWORD");
        return String.class.cast(field.get(null)).equals("1");
    } catch (NoSuchFieldException e) {
        // Do nothing.
    } catch (IllegalArgumentException e) {
        // Do nothing.
    } catch (IllegalAccessException e) {
        // Do nothing.
    }
    return false;
}

From source file:Main.java

private static void recreateGB(Activity activity) throws IllegalArgumentException, IllegalAccessException,
        NoSuchMethodException, InvocationTargetException {
    Field Activity$mToken = scanField(Activity.class, "mToken");
    IBinder mToken = (IBinder) Activity$mToken.get(activity);
    Field Activity$mMainThread = scanField(Activity.class, "mMainThread");
    Object mMainThread = Activity$mMainThread.get(activity);
    Field ActivityThread$mAppThread = scanField(mMainThread.getClass(), "mAppThread");
    Object mAppThread = ActivityThread$mAppThread.get(mMainThread);
    // @formatter:off
    Method method = mAppThread.getClass().getMethod("scheduleRelaunchActivity", IBinder.class, List.class,
            List.class, int.class, boolean.class, Configuration.class);
    // @formatter:on
    method.invoke(mAppThread, mToken, null, null, 0, false, null);
}

From source file:Main.java

/**
 * Gets the value of a static field./*from  ww  w  . j  a v a  2  s.  c om*/
 *
 * @param clazz from which to get the field value
 * @param name the name of the field
 * @return the value of the field.
 * @throws PrivilegedActionException
 */
static <T> T getStaticFieldValue(final Class<?> clazz, final String name) throws PrivilegedActionException {
    final PrivilegedExceptionAction<T> action = new PrivilegedExceptionAction<T>() {
        @SuppressWarnings("unchecked")
        public T run() throws Exception {
            Field field = clazz.getDeclaredField(name);
            field.setAccessible(true);
            return (T) field.get(null);
        }
    };

    return AccessController.doPrivileged(action);
}

From source file:Main.java

private static int getImageViewFieldValue(Object object, String fieldName) {
    int value = 0;
    try {//from   w w  w  . j av  a  2 s  .  c  om
        Field field = ImageView.class.getDeclaredField(fieldName);
        field.setAccessible(true);
        int fieldValue = (Integer) field.get(object);
        if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) {
            value = fieldValue;
        }
    } catch (Exception e) {
    }
    return value;
}

From source file:Main.java

public static Object getReflection(Object itemToGetObject, String objectName) {
    try {//from w  w w. j av a2  s  . c  om
        Class<?> clazz = itemToGetObject.getClass();
        Field field;
        field = clazz.getDeclaredField(objectName);
        field.setAccessible(true);
        return field.get(itemToGetObject);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Object getFieldValue(final Object receiver, final Object defaultValue, final Field field) {
    if (field == null) {
        return defaultValue;
    }//from   ww w  .  j  a  v a  2 s .c om
    try {
        return field.get(receiver);
    } catch (final IllegalAccessException | IllegalArgumentException e) {
        Log.e(TAG, "Exception in getFieldValue", e);
    }
    return defaultValue;
}