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 void fieldSet(Object object, Class<?> clazz, String fieldName, Object value) throws Exception {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);
    field.set(object, value);/*from w  ww  .j  a va2s .c  o m*/
}

From source file:Main.java

public static Object getField(String fieldName, Class clazz, Object object) {
    try {/*  w  w  w .  j  a  v  a  2  s  .c  o m*/
        Field f = clazz.getDeclaredField(fieldName);
        f.setAccessible(true);
        return f.get(object);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static void setField(Class cls, String name, Object obj, Object value) {
    try {//from w  w w .ja  va2s  .  com
        Field nameField = cls.getDeclaredField(name);
        nameField.setAccessible(true);
        nameField.set(obj, value);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void writeValue(Object src, String fieldName, Object value) throws Exception {
    Field field = findField(src, fieldName);
    field.setAccessible(true);
    field.set(src, value);//from  ww w . j  a va 2  s  .co  m
}

From source file:Main.java

public static Field getPrivateField(Object target, String fieldName) throws NoSuchFieldException {
    Class targetClass = target.getClass();
    Field field = targetClass.getDeclaredField(fieldName);
    field.setAccessible(true);
    return field;
}

From source file:Main.java

public static String getMobileInfo() {
    StringBuffer sb = new StringBuffer();
    try {// ww  w  . ja  v a2s.c  o m

        Field[] fields = Build.class.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String name = field.getName();
            String value = field.get(null).toString();
            sb.append(name + "=" + value);
            sb.append("\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:Main.java

public static <T> JSONArray beansToJsonArray(T... beans)
        throws IllegalArgumentException, IllegalAccessException, JSONException {
    JSONArray jArr = new JSONArray();
    for (T bean : beans) {
        JSONObject jObj = new JSONObject();
        Field[] fields = bean.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String name = field.getName();
            Object value = field.get(bean);

            jObj.put(name, value);/*ww w  .jav a  2  s  . c o m*/
        }
        jArr.put(jObj);
    }
    return jArr;
}

From source file:Main.java

private static Field findField(Object instance, String name) throws NoSuchFieldException {
    for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
        try {/*from w  w  w  .  ja  v  a  2s .c o m*/
            Field f = clazz.getDeclaredField(name);
            f.setAccessible(true);
            return f;
        } catch (NoSuchFieldException e) {
        }
    }
    throw new NoSuchFieldException("Unable to find field " + name + " in " + instance.getClass());
}

From source file:Main.java

public static Object getStaticFieldValue(@SuppressWarnings("rawtypes") final Class clazz, final String name)
        throws Exception {
    Field field = clazz.getDeclaredField(name);
    field.setAccessible(true);
    return field.get(null);
}

From source file:Main.java

public static <T> T reflectField(Object src, String fieldName, Class<T> clazz) throws Exception {
    Field field = findField(src, fieldName);
    field.setAccessible(true);
    return (T) field.get(src);
}