Java Reflection Field Set setField(Object obj, Object value, String fieldName)

Here you can find the source of setField(Object obj, Object value, String fieldName)

Description

Sets the value of a field

License

Open Source License

Parameter

Parameter Description
obj The object to set the field in
value The value to set the field to
fieldName The field name

Declaration

public static void setField(Object obj, Object value, String fieldName) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Field;

public class Main {
    /**//from  w  w  w . j  a  v  a2  s.c o m
     * Sets the value of a field
     * @param obj The object to set the field in
     * @param value The value to set the field to
     * @param fieldName The field name
     */
    public static void setField(Object obj, Object value, String fieldName) {
        try {
            getField(obj.getClass(), fieldName).set(obj, value);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * Gets the value of a field
     * 
     * @param obj The object to get the field from
     * @param fieldName The field name to get
     * @return null if there is no field or an exception was thrown
     */
    public static Object getField(Object obj, String fieldName) {
        try {
            return getField(obj.getClass(), fieldName).get(obj);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Retrieves a field
     * 
     * @param cl The class to retrieve the field from
     * @param fieldName The name of the field
     * @return null if there is no field or an exception was thrown
     */
    public static Field getField(Class<?> cl, String fieldName) {
        try {
            Field field = cl.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field;
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Class<?> getClass(String name) {
        try {
            return Class.forName(name);
        } catch (ClassNotFoundException e) {
            return null;
        }
    }
}

Related

  1. setField(Object o, String fieldName, Object value)
  2. setField(Object o, String fieldName, Object value)
  3. setField(Object o, String name, Object value)
  4. setField(Object obj, Class fieldOwner, String fieldName, Object value)
  5. setField(Object obj, Field field, Object value)
  6. setField(Object obj, Object value, String fieldName)
  7. setField(Object obj, String fieldName, Object fieldValue)
  8. setField(Object obj, String fieldName, Object val)
  9. setField(Object obj, String fieldname, Object value)