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

/**
 * Attempt to find a {@link Field field} on the supplied {@link Class} with the
 * supplied <code>name</code>. Searches all superclasses up to {@link Object}.
 *
 * @param clazz the class to introspect/*  ww w.j  a  va2  s  . c o  m*/
 * @param name  the name of the field
 * @return the corresponding Field object, or <code>null</code> if not found
 */
public static Field findField(Class<?> clazz, String name) {
    Class<?> searchType = clazz;
    while (!Object.class.equals(searchType) && searchType != null) {
        Field[] fields = searchType.getDeclaredFields();
        for (Field field : fields) {
            if (name.equals(field.getName())) {
                field.setAccessible(true);
                return field;
            }
        }
        searchType = searchType.getSuperclass();
    }
    return null;
}

From source file:Main.java

/**
 * /*from  w  w  w  . j av a2s  .  c  o  m*/
 * @param node
 * @param classname
 * @return
 */
public static Object decode(Element node, String classname) {
    try {
        Class classObject = Class.forName(classname);
        Object object = classObject.newInstance();
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node child = attributes.item(i);
            String nodeName = child.getNodeName();
            Field field = classObject.getField(nodeName);
            field.setAccessible(true);
            field.set(object, child.getNodeValue());
        }
        return object;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (InstantiationException e) {
        e.printStackTrace();
        return null;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return null;
    } catch (SecurityException e) {
        e.printStackTrace();
        return null;
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:com.job.portal.utils.BeanUtils.java

public static Map<String, Object> convertToMap(Object bean) {
    Map<String, Object> m = new HashMap<String, Object>();
    try {//from  w w  w. j  a va2  s  . c  om
        for (Field field : bean.getClass().getDeclaredFields()) {
            field.setAccessible(true); // You might want to set modifier to public first.
            Object value = field.get(bean);
            if (value != null) {
                m.put(field.getName(), value);
            }
        }
    } catch (Exception e) {
        LogOut.log.error("In " + new Object() {
        }.getClass().getEnclosingClass().getName() + "." + new Object() {
        }.getClass().getEnclosingMethod().getName() + " " + e);
    }
    return m;
}

From source file:com.job.portal.utils.BeanUtils.java

public static JSONObject convertToJSON(Object bean) {
    JSONObject obj = new JSONObject();
    try {//from w  w w. j a v a  2s .  c  om
        for (Field field : bean.getClass().getDeclaredFields()) {
            field.setAccessible(true); // You might want to set modifier to public first.
            Object value = field.get(bean);
            if (value != null) {
                obj.put(field.getName(), value);
            }
        }
    } catch (Exception e) {
        LogOut.log.error("In " + new Object() {
        }.getClass().getEnclosingClass().getName() + "." + new Object() {
        }.getClass().getEnclosingMethod().getName() + " " + e);
    }
    return obj;
}

From source file:com.eclecticlogic.pedal.dm.internal.MetamodelUtil.java

/**
 * @param attribute JPA metamodel attribute.
 * @param entity Entity to set the value on.
 * @param value Value to set.//w w w  .  ja va2s  .  co m
 */
public static <E extends Serializable, T extends Serializable> void set(Attribute<? super E, T> attribute,
        E entity, T value) {
    Member member = attribute.getJavaMember();
    if (member instanceof Field) {
        Field field = (Field) member;
        field.setAccessible(true);
        try {
            field.set(entity, value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else if (member instanceof Method) {
        PropertyDescriptor pd = BeanUtils.findPropertyForMethod((Method) member);
        if (pd.getWriteMethod() != null) {
            pd.getWriteMethod().setAccessible(true);
            try {
                pd.getWriteMethod().invoke(entity, value);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            throw new RuntimeException(
                    "No setter for " + attribute.getName() + " in " + entity.getClass().getName());
        }
    } else {
        throw new RuntimeException("Failed to set " + attribute.getName() + " of type "
                + member.getClass().getName() + " in entity " + entity.getClass().getName());
    }
}

From source file:Main.java

public static Field getField(Class<?> clazz, String fieldName) {
    for (Field field : clazz.getDeclaredFields()) {
        if (field.getName().equals(fieldName)) {
            field.setAccessible(true);
            return field;
        }/*w w  w.j a  va 2  s.  c o m*/
    }
    return null;
}

From source file:Main.java

public static void setField(Field field, Object target, Object value) {
    if (!Modifier.isPublic(field.getModifiers())) {
        field.setAccessible(true);
    }/*from   www .ja  v  a  2s .c  o m*/
    try {
        field.set(target, value);
    } catch (IllegalAccessException iae) {
        throw new IllegalArgumentException("Could not set field " + field, iae);
    }
}

From source file:Main.java

public static Object getField(Field field, Object target) {
    if (!Modifier.isPublic(field.getModifiers())) {
        field.setAccessible(true);
    }/*w  w w.  j  a v a  2s . c o m*/
    try {
        return field.get(target);
    } catch (IllegalAccessException iae) {
        throw new IllegalArgumentException("Could not get field " + field, iae);
    }
}

From source file:Main.java

private static Object getDeclaredField(Object obj, String name)
        throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    Field f = obj.getClass().getDeclaredField(name);
    f.setAccessible(true);
    Object out = f.get(obj);/*from w  w  w.j  av a  2  s  .c om*/
    //System.out.println(obj.getClass().getName() + "." + name + " = "+ out);
    return out;
}

From source file:Main.java

public static <T> T getVar(@NonNull Object obj, @NonNull String name) throws Exception {
    for (Class cls = obj.getClass(); //
            cls != null && cls != Object.class; //
            cls = cls.getSuperclass()) {
        for (Field f : cls.getDeclaredFields()) {
            if (f.getName().equals(name)) {
                f.setAccessible(true);
                //noinspection unchecked
                return (T) f.get(obj);
            }//from  w w w .ja  va 2s. c o  m
        }
    }
    throw new RuntimeException("no var matching " + name);
}