Example usage for java.lang.reflect Field setAccessible

List of usage examples for java.lang.reflect Field setAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Field setAccessible.

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:Main.java

public static Integer getPID(Process process) {
    Integer pid = null;//from   w  w w .  j  a  v  a2  s  . co m
    if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
        /* get the PID on unix/linux systems */
        try {
            Field f = process.getClass().getDeclaredField("pid");
            f.setAccessible(true);
            pid = f.getInt(process);
        } catch (Throwable e) {
        }
    }
    return pid;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E getProperty(Object object, String fieldName) {
    Class<?> clazz = object.getClass();
    while (clazz != null) {
        try {/*  w ww  .  j  a v  a2  s.  c om*/
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            return (E) field.get(object);
        } catch (NoSuchFieldException e) {
            clazz = clazz.getSuperclass();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
    return null;
}

From source file:Main.java

private static int getImageViewFieldValue(Object object, String fieldName) {
    int value = 0;
    try {/*from  w  w  w  . j a  va2 s  .  c o  m*/
        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

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

}

From source file:Main.java

public static <K, V> Map<K, V> collectionToMap(Collection<V> collection, String key) {
    Map<K, V> map = null;//from   w  ww .ja  v  a  2s .c  o m
    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

public static Object getProperty(Object o, String field) {
    try {/*from  ww w.  jav a 2 s.c om*/
        Field f = o.getClass().getDeclaredField(field);
        f.setAccessible(true);
        String name = f.getName();
        name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase(Locale.US));
        Method m = o.getClass().getMethod("get" + name);
        // return f.get(o);
        return (Object) m.invoke(o);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Map<String, String> beanToMap(Object object) {
    Map<String, String> params = new HashMap<>();
    if (null == object) {
        return params;
    }/*from  w  w w  .j av  a2 s  . c om*/
    try {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object fieldObj = field.get(object);
            if (fieldObj != null && fieldObj.getClass() == String.class) {
                String fieldValue = (String) fieldObj;
                if (!TextUtils.isEmpty(fieldValue)) {
                    if (!TextUtils.isEmpty(fieldValue)) {
                        fieldValue = fieldValue.replaceAll("\"", "%22");
                    }
                    params.put(fieldName, fieldValue);
                }
            }
        }
        return params;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return params;
}

From source file:Main.java

public static Object get(Field f, Object obj) throws IllegalArgumentException, IllegalAccessException {
    f.setAccessible(true);
    return f.get(obj);
}

From source file:Main.java

public static Object getProperty(Object classObject, String paramString) {
    try {//  ww w.j a  v a  2  s  . com
        Field localField = classObject.getClass().getDeclaredField(paramString);
        localField.setAccessible(true);
        return localField.get(classObject);
    } catch (Exception var3) {
        return null;
    }
}

From source file:Main.java

/**
 * Returns a {@code Class} object that identifies the
 * declared class for the field represented by the given {@code String name} parameter inside
 * the invoked {@code Class<?> clazz} parameter.
 *
 * @param clazz the {@code Class} object whose declared fields to be
 *       checked for a certain field.//  w ww . j  ava 2s.  co m
 * @param name the field name as {@code String} to be
 *       compared with {@link Field#getName()}
 * @return the {@code Class} object representing the type of given field name.
 *
 * @see {@link Class#getDeclaredFields()}
 * @see {@link Field#getType()}
 */
public static Class<?> getFieldClass(Class<?> clazz, String name) {
    if (clazz == null || name == null || name.isEmpty()) {
        return null;
    }

    Class<?> propertyClass = null;

    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        if (field.getName().equalsIgnoreCase(name)) {
            propertyClass = field.getType();
            break;
        }
    }

    return propertyClass;
}