Java Reflection Field Set setField(Field field, Object target, Object value)

Here you can find the source of setField(Field field, Object target, Object value)

Description

set Field

License

Open Source License

Declaration

public static void setField(Field field, Object target, Object value) 

Method Source Code


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

import java.lang.reflect.Field;

import java.lang.reflect.Modifier;

public class Main {
    public static void setField(Field field, Object target, Object value) {
        try {/*from  ww  w  .  j av a  2s  . c  o m*/
            makeAccessible(field);
            field.set(target, value);
        } catch (IllegalAccessException var4) {
            throw new IllegalStateException(
                    "Unexpected reflection exception - " + var4.getClass().getName() + ": " + var4.getMessage());
        }
    }

    public static void makeAccessible(Field field) {
        if ((!Modifier.isPublic(field.getModifiers())
                || !Modifier.isPublic(field.getDeclaringClass().getModifiers())
                || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {
            field.setAccessible(true);
        }
    }
}

Related

  1. setField(Field field, @Nullable Object instance, Object thing)
  2. setField(Field field, Object classWithField, Object value)
  3. setField(Field field, Object instance, Object value)
  4. setField(Field field, Object obj, String value)
  5. setField(Field field, Object objToSet, Object value)
  6. setField(Field field, Object target, Object value)
  7. setField(Field field, Object target, Object value)
  8. setField(Field field, Object target, Object value)
  9. setField(Field field, Object target, Object value)