Here you can find the source of setFieldValue(Class clazz, Object target, String fieldName, Object value)
Parameter | Description |
---|---|
clazz | Class to scan |
target | Instance |
fieldName | Name of field |
public static void setFieldValue(Class clazz, Object target, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException
//package com.java2s; //License from project: Open Source License import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.*; public class Main { /**/*from w ww.j ava 2 s .co m*/ * Set class field value, returns success status * @param clazz Class to scan * @param target Instance * @param fieldName Name of field */ public static void setFieldValue(Class clazz, Object target, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException { HashMap<String, Object> result = new HashMap<>(); Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(target, value); } }