Java Reflection Field Value Set setFieldValue(String name, String obfuscatedName, Class clazz, Object object, Object value)

Here you can find the source of setFieldValue(String name, String obfuscatedName, Class clazz, Object object, Object value)

Description

set Field Value

License

Open Source License

Declaration

public static void setFieldValue(String name, String obfuscatedName, Class<?> clazz, Object object,
            Object value) 

Method Source Code


//package com.java2s;
import java.lang.reflect.Field;

public class Main {
    public static void setFieldValue(String name, String obfuscatedName, Class<?> clazz, Object object,
            Object value) {// www.  j  av  a 2  s. c  om
        Field field = getField(name, obfuscatedName, clazz);
        try {
            field.setAccessible(true);
            field.set(object, value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Field getField(String name, String obfuscatedName, Class<?> clazz) {
        try {
            return clazz.getDeclaredField(name);
        } catch (NoSuchFieldException e1) {
            if (obfuscatedName == null) {
                throw new RuntimeException("No obfuscated field name provided.");
            }
            try {
                return clazz.getDeclaredField(obfuscatedName);
            } catch (NoSuchFieldException e2) {
            }
        }
        throw new RuntimeException("Cannot find field: " + name);
    }
}

Related

  1. setFieldValue(Object target, String fieldName, String fieldValue)
  2. setFieldValue(Object target, String fname, Class ftype, Object fvalue)
  3. setFieldValue(String fieldName, int fieldValue, Object board)
  4. setFieldValue(String inField, Object inObject, Object inValue)
  5. setFieldValue(String name, Object instance, int value, Class cl)
  6. setFieldvalue(T instance, E value, Field field)
  7. setFieldValue_internal(Object bean, Field field, Object value)
  8. setFieldValueForObject(Object object, String fieldName, Object value)
  9. setFieldValues(Object bean, Map valMap)