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 printObject(Object object) {
    Class<?> class1 = object.getClass();
    Field[] fields = class1.getDeclaredFields();
    for (Field field : fields) {
        try {//www . j av a 2 s  . com
            field.setAccessible(true);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void injectLayoutInflater(LayoutInflater layoutInflater, Object src, Class clz, String name) {
    try {//  w w w  . j  ava  2 s.  co m
        Field field = clz.getDeclaredField(name);
        field.setAccessible(true);
        field.set(src, layoutInflater);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static Field fieldGetOrg(Object object, String name) throws Exception {
    Field field = object.getClass().getDeclaredField(name);
    field.setAccessible(true);
    return field;
}

From source file:Main.java

private static Object getField(Object paramObject, Class<?> paramClass, String paramString)
        throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
    Field localField = paramClass.getDeclaredField(paramString);
    localField.setAccessible(true);
    return localField.get(paramObject);
}

From source file:Main.java

public static void setFieldValue(Class<?> fieldClass, String fieldName, Object instance, Object value) {
    try {/*  w  ww .  j a va 2 s .  c o  m*/
        Field e = fieldClass.getDeclaredField(fieldName);
        e.setAccessible(true);
        e.set(instance, value);
    } catch (Exception var5) {
        var5.printStackTrace();
    }

}

From source file:Main.java

public static Object getFieldValue(Class<?> fieldClass, String fieldName, Object instance) {
    try {// ww  w  . j a  v  a2 s .  c  o m
        Field e = fieldClass.getDeclaredField(fieldName);
        e.setAccessible(true);
        return e.get(instance);
    } catch (Exception var4) {
        var4.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static void setField(Object obj, Class<?> cls, String field, Object value) {
    try {/*from   ww  w.  j a  v  a2s  .  com*/
        Field localField = cls.getDeclaredField(field);
        localField.setAccessible(true);
        localField.set(obj, value);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

static Field getField(Class clazz, String fieldName) {
    try {/*from  ww w  . ja v a  2 s  .c  om*/
        final Field f = clazz.getDeclaredField(fieldName);
        f.setAccessible(true);
        return f;
    } catch (NoSuchFieldException ignored) {
    }
    return null;
}

From source file:Main.java

public static <T> T getMember(Object object, String member, Class<T> klass)
        throws NoSuchFieldException, IllegalAccessException {
    Field field = getField(object.getClass(), member);
    field.setAccessible(true);
    return (T) field.get(object);
}

From source file:Main.java

private static Field prepareField(Class c, String fieldName) throws NoSuchFieldException {
    while (c != null) {
        try {//from   w ww .j  a v  a 2  s .c  o m
            Field f = c.getDeclaredField(fieldName);
            f.setAccessible(true);
            return f;
        } catch (Exception e) {
        } finally {
            c = c.getSuperclass();
        }
    }
    throw new NoSuchFieldException();
}