Java Reflection Field Set setField(Class c, Object inst, String name, Object value)

Here you can find the source of setField(Class c, Object inst, String name, Object value)

Description

changes a field in a class by using reflection

License

Open Source License

Parameter

Parameter Description
c the class (can be null for objects)
inst the instance, null for static context
name the name of the field to edit
value the new value of the field

Declaration

public static void setField(Class<?> c, Object inst, String name, Object value) 

Method Source Code

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

import java.lang.reflect.Array;
import java.lang.reflect.Field;

public class Main {
    /**//from  w ww. j a  v a2s.  c  o  m
     * changes a field in a class by using reflection
     * @param c the class (can be null for objects)
     * @param inst the instance, null for static context
     * @param name the name of the field to edit
     * @param value the new value of the field
     */
    public static void setField(Class<?> c, Object inst, String name, Object value) {
        if (c == null)
            c = inst.getClass();
        try {
            Field field = null;
            while (field == null) {
                try {
                    field = c.getDeclaredField(name);
                } catch (NoSuchFieldException e) {
                    c = c.getSuperclass();
                    if (c == null)
                        throw new NoSuchFieldException(name);
                }
            }
            field.setAccessible(true);
            field.set(inst, value);
        } catch (Exception e) {
        }
    }

    /**
     * sets the specified element in the array to a new value
     * @param arr the array to edit
     * @param item the new value
     * @param pos the index, can be >= arr.length or < 0
     * @return an array, will be a new instance if pos < 0
     */
    public static Object set(Object arr, Object item, int pos) {
        int len = Array.getLength(arr);
        if (pos < 0)
            arr = subArray(arr, pos, len);
        Array.set(arr, pos, item);
        return arr;
    }

    /**
     * constructs a new array with length max - min (like String.substring) use with min = 0 and max = arr.length to make a "copy"
     * @param arr the array to resize
     * @param min beginning index, can be < 0
     * @param max the end index, can be > arr.length
     * @return the new array
     */
    public static Object subArray(Object arr, int min, int max) {
        if (max <= min)
            throw new IllegalArgumentException("invalid bounds");
        Object array = Array.newInstance(arr.getClass().getComponentType(), max - min);
        int len = Array.getLength(arr);
        int srcmin = min <= 0 ? 0 : -min;
        int dstmin = min >= 0 ? 0 : -min;
        int dstlen = max - srcmin;
        if (dstlen > len - srcmin)
            dstlen = len;
        System.arraycopy(arr, srcmin, array, dstmin, dstlen);
        return array;
    }
}

Related

  1. setField(Class clazz, String name, Object value)
  2. setField(Class cls, Object obj, String fieldName, Object value)
  3. setField(Class type, String name, Object instance, Object value)
  4. setField(Class klazz, Object recipient, String fieldName, Object newValue)
  5. setField(Class clazz, String fieldName, T instance, Object value)
  6. setField(Class cl, String name, Object obj, Object value)
  7. setField(Class clazz, Object instance, String field, Object value)
  8. setField(Class clazz, Object obj, String fieldName, Object value)
  9. setField(Class ownerClass, Object owner, String fieldName, Object newValue)