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 Object getField(Object obj, Class<?> cl, String field)
        throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    Field localField = cl.getDeclaredField(field);
    localField.setAccessible(true);
    return localField.get(obj);
}

From source file:Main.java

public static void objectCopy(Object from, Object to) throws Exception {

    if (from.getClass() != to.getClass()) {
        throw new IllegalArgumentException("[objectCopy]The left and right must be same class");
    }//from  w  w  w .  jav a2 s .  co m
    Class<?> clz = from.getClass();
    Field[] fs = clz.getDeclaredFields();
    for (int i = 0; i < fs.length; i++) {
        Field field = fs[i];

        field.setAccessible(true);

        Object value = field.get(from);
        field.set(to, value);
    }
}

From source file:Main.java

public static Object getStaticFieldValue(Class<?> clazz, String fieldName) throws Exception {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);
    return field.get(fieldName);
}

From source file:Main.java

public static void copyBeanWithOutNull(Object from, Object to) {
    Class<?> beanClass = from.getClass();
    Field[] fields = beanClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        field.setAccessible(true);
        try {//  w w w.  j  a v a  2 s.c o  m
            Object value = field.get(from);
            if (value != null) {
                field.set(to, value);
            }
        } catch (Exception e) {
        }
    }

}

From source file:Main.java

public static Field fieldGetOrg(Object object, Class<?> clazz, String name) throws Exception {
    Field field = clazz.getDeclaredField(name);
    field.setAccessible(true);
    return field;
}

From source file:Main.java

public static void copyBeanWithOutNull(Object from, Object to) {
    Class<?> beanClass = from.getClass();
    Field[] fields = beanClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        field.setAccessible(true);
        try {/* w  w  w . j  av a2s .  c  o m*/
            Object value = field.get(from);
            if (value != null) {
                field.set(to, value);
            }
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static void setField(Class<?> clazz, String fieldName, Object object, Object value)
        throws NoSuchFieldException, IllegalAccessException {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);
    field.set(object, value);/*from   w w  w. j  a v a  2s  . c o  m*/
}

From source file:Main.java

public static Object getField(Class<?> clazz, String fieldName, Object object)
        throws NoSuchFieldException, IllegalAccessException {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);
    return field.get(object);
}

From source file:Main.java

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

From source file:Main.java

public static Object fieldGet(Object object, Class<?> clazz, String fieldName) throws Exception {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);
    return field.get(object);
}