Java Reflection Field Set setField(Object target, String name, Object value)

Here you can find the source of setField(Object target, String name, Object value)

Description

Set the field identified by name to the given value.

License

Open Source License

Parameter

Parameter Description
target a parameter
name a parameter
value a parameter

Return

true on success, false if field wasn't found.

Declaration

public static boolean setField(Object target, String name, Object value) 

Method Source Code

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

import java.lang.reflect.Field;

public class Main {
    /**//  www .  j  a  v a  2  s  . co  m
     * Set the field identified by name to the given value.
     * @param target
     * @param name
     * @param value
     * @return true on success, false if field wasn't found.
     */
    public static boolean setField(Object target, String name, Object value) {
        try {
            Field field = target.getClass().getDeclaredField(name);
            field.setAccessible(true);
            field.set(target, value);
            return true;
        } catch (NoSuchFieldException e) {
            return false;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. setField(Object target, Field field, Object value)
  2. setField(Object target, Field field, Object value)
  3. setField(Object target, String fieldName, Class fieldType, T value)
  4. setField(Object target, String fieldname, Object value)
  5. setField(Object target, String name, Object value)
  6. setField(Object target, String name, Object value)
  7. setField(Object target, String name, Object value)
  8. setField(Object targetObject, String fieldName, Object value, boolean failIfError)
  9. setField(String field, Object object, Object value)