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

private final static Field getField(String name) {
    Field field = null;
    try {/*w  w w  .  j  av  a  2  s  . c o  m*/
        field = sCurrentClass.getDeclaredField(name);
        field.setAccessible(true);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        //         e.printStackTrace();
    }

    return field;
}

From source file:Main.java

private static void setField(Object paramObject1, Class<?> paramClass, String paramString, Object paramObject2)
        throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
    Field localField = paramClass.getDeclaredField(paramString);
    localField.setAccessible(true);
    localField.set(paramObject1, paramObject2);
}

From source file:Main.java

public static LinkedHashMap<String, String> convertBeans(Object bean) {
    if (bean == null)
        return null;
    try {/* ww w. ja  v  a2 s  .  c  o  m*/
        LinkedHashMap<String, String> returnMap = new LinkedHashMap<String, String>();

        Class<? extends Object> clazz = bean.getClass();
        List<Field> fleids = new ArrayList<Field>();
        for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
            fleids.addAll(Arrays.asList(c.getDeclaredFields()));
        }

        for (Field field : fleids) {
            String value = "";
            field.setAccessible(true);
            try {
                Object result = field.get(bean);
                if (result == null)
                    continue;
                value = result.toString();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            //            MLogUtil.e("field.getName() "+field.getName());
            //            MLogUtil.e("value "+value);
            returnMap.put(field.getName(), value);
            field.setAccessible(false);
        }
        return returnMap;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.angstoverseer.util.ReflectionUtil.java

public static void setField(String fieldName, Object target, Object value) {
    final Field field = ReflectionUtils.findField(target.getClass(), fieldName);
    field.setAccessible(true);
    ReflectionUtils.setField(field, target, value);
}

From source file:com.aw.support.reflection.AttributeAccessor.java

/**
 * @param target/*w  w w . j a  va2 s . c o m*/
 * @return
 */
public static void set(Object target, Field field, Object value) {
    field.setAccessible(true);
    try {
        field.set(target, value);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Error setting the value of the attribute:<" + field.getName()
                + "> of object:<" + target + "> check: Attribute Type:<" + field.getType() + "> value Type: <"
                + value.getClass() + ">");
    }
}

From source file:Main.java

public static Map<String, String> convertBean(Object bean) {
    if (bean == null)
        return null;
    try {//from   w w w .j  a va2s  .com
        Class<? extends Object> clazz = bean.getClass();
        Map<String, String> returnMap = new HashMap<String, String>();
        List<Field> fleids = new ArrayList<Field>();
        for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
            fleids.addAll(Arrays.asList(c.getDeclaredFields()));
        }

        for (Field field : fleids) {
            String value = "";
            field.setAccessible(true);
            try {
                Object result = field.get(bean);
                if (result == null)
                    continue;
                value = result.toString();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            //            MLogUtil.e("field.getName() "+field.getName());
            //            MLogUtil.e("value "+value);
            returnMap.put(field.getName(), value);
            field.setAccessible(false);
        }
        return returnMap;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

@Deprecated
public static void quitAppliation(Activity activity) {
    try {//from www  .  j ava  2s  . com
        Class<?> clazz_Activity = Class.forName("android.app.Activity");
        Field field_mMainThread = clazz_Activity.getDeclaredField("mMainThread");
        field_mMainThread.setAccessible(true);
        Object mMainThread = field_mMainThread.get(activity);

        Class<?> clazz_ActivityThread = Class.forName("android.app.ActivityThread");
        Field field_mAppThread = clazz_ActivityThread.getDeclaredField("mAppThread");
        field_mAppThread.setAccessible(true);
        Object mAppThread = field_mAppThread.get(mMainThread);

        Class<?> clazz_ActivityClientRecord = Class.forName("android.app.ActivityThread$ApplicationThread");
        Method method_scheduleExit = clazz_ActivityClientRecord.getDeclaredMethod("scheduleExit");
        method_scheduleExit.invoke(mAppThread);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}

From source file:de.cbb.mplayer.mapping.MappingUtil.java

public static Object getAccessableControl(String fieldname, Object presenter) throws Exception {
    Field cbField = presenter.getClass().getDeclaredField(fieldname);
    cbField.setAccessible(true);
    Object control = cbField.get(presenter);
    return control;
}

From source file:Main.java

public static void setField(Object value, Field field, Object target) {
    boolean accessible = field.isAccessible();
    field.setAccessible(true);
    Object result = null;//from   ww  w.ja va 2  s . c  om
    try {
        field.set(target, value);
    } catch (IllegalAccessException e) {
        // ignore
    }
    field.setAccessible(accessible);
}

From source file:io.fns.calculator.filter.CORSFilter.java

private static Object getInstanceField(Object instance, String fieldName) throws Throwable {
    Field field = instance.getClass().getSuperclass().getDeclaredField(fieldName);
    field.setAccessible(true);
    return field.get(instance);
}