Java Reflection Field Value Set setFieldValue(Class clazz, E instance, Object value, String... names)

Here you can find the source of setFieldValue(Class clazz, E instance, Object value, String... names)

Description

set Field Value

License

Open Source License

Declaration

public static <T, E> void setFieldValue(Class<? extends E> clazz,
            E instance, Object value, String... names) 

Method Source Code

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

import java.lang.reflect.Field;

public class Main {
    public static <T, E> void setFieldValue(Class<? extends E> clazz,
            E instance, Object value, String... names) {
        try {/*from   w  w w . ja v  a2  s . co m*/
            Field field = getField(clazz, names);
            if (field != null)
                field.set(instance, value);
        } catch (Exception e) {
        }
    }

    public static Field getField(Class<?> clazz, String... names) {
        try {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                for (String fieldName : names) {
                    if (field.getName().equalsIgnoreCase(fieldName)) {
                        field.setAccessible(true);
                        return field;
                    }
                }
            }
        } catch (Exception e) {
            throw e;
        }
        return null;
    }
}

Related

  1. setFieldValue(Class c, Object instance, String name, Object value)
  2. setFieldValue(Class clazz, Object instance, String fieldName, T value)
  3. setFieldValue(Class clazz, Object target, String fieldName, Object value)
  4. setFieldValue(Class clazz, Object entity, String fieldName, V value)
  5. setFieldValue(Class clazz, String fieldName, Object value)
  6. setFieldValue(Class clazz, String fieldName, Object value)
  7. setFieldValue(Class expectedType, Object target, String fieldName, T value)