Java Reflection Field Value Set setFieldValue(Object obj, String fieldName, String fieldValue)

Here you can find the source of setFieldValue(Object obj, String fieldName, String fieldValue)

Description

set Field Value

License

Apache License

Declaration

public static void setFieldValue(Object obj, String fieldName, String fieldValue)
        throws IllegalArgumentException, IllegalAccessException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;

public class Main {

    public static void setFieldValue(Object obj, String fieldName, String fieldValue)
            throws IllegalArgumentException, IllegalAccessException {
        Field field = getField(obj.getClass(), fieldName);
        if (field != null) {
            field.setAccessible(true);/*from  w  w  w.j a  v  a  2  s.  co m*/
            field.set(obj, fieldValue);
        }
    }

    public static <T> Field getField(Class<T> clazz, String fieldName) {
        try {
            Field field = clazz.getDeclaredField(fieldName);
            if (field == null) {
                field = clazz.getSuperclass().getDeclaredField(fieldName);
            }
            return field;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. setFieldValue(Object obj, String fieldName, Object value)
  2. setFieldValue(Object obj, String fieldname, Object value)
  3. setFieldValue(Object obj, String fieldName, Object value)
  4. setFieldValue(Object obj, String fieldName, Object value)
  5. setFieldValue(Object obj, String fieldName, Object value)
  6. setFieldValue(Object obj, String name, Object value)
  7. setFieldValue(Object object, Field field, Object value)
  8. setFieldValue(Object object, String fieldName, Object fieldValue)
  9. setFieldValue(Object object, String fieldName, Object fieldValue)