Java Reflection Field Set setField(Object instance, String name, Object obj)

Here you can find the source of setField(Object instance, String name, Object obj)

Description

set Field

License

Apache License

Declaration

public static void setField(Object instance, String name, Object obj) 

Method Source Code


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

import java.lang.reflect.Field;

public class Main {
    public static void setField(Object instance, String name, Object obj) {

        try {//from w  ww.  j av a  2 s. co m
            Field f = instance.getClass().getDeclaredField(name);
            f.setAccessible(true);
            f.set(instance, obj);
        } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. setField(final Object obj, final String variableName, final Object variableValue)
  2. setField(final Object object, final String name, final Object value)
  3. setField(final Object parent, final String name, final Object obj)
  4. setField(java.lang.Object toObj, String tcFieldName, java.lang.Object toNewValue)
  5. setField(Object instance, String fieldName, Object value)
  6. setField(Object instance, String sField, Object value)
  7. setField(Object item, Field f, Object value)
  8. setField(Object o, String fieldName, Object inject)
  9. setField(Object o, String fieldName, Object value)