Java Reflection Field Set setFieldObject(Class clazz, String field, Object object, Object newObj)

Here you can find the source of setFieldObject(Class clazz, String field, Object object, Object newObj)

Description

set Field Object

License

Open Source License

Declaration

public static void setFieldObject(Class<?> clazz, String field, Object object, Object newObj) 

Method Source Code

//package com.java2s;
/**/*from   w  w w .ja  va2s . c o  m*/
 * 
 * This software is part of the MaterialAPI
 * 
 * This api allows plugin developers to create on a easy way custom
 * items with a custom id and recipes depending on them.
 * 
 * MaterialAPI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * any later version.
 *  
 * MaterialAPI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MaterialAPI. If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import java.lang.reflect.Field;

public class Main {
    public static void setFieldObject(Class<?> clazz, String field, Object object, Object newObj) {
        try {
            Field f = clazz.getDeclaredField(field);
            f.setAccessible(true);
            f.set(object, newObj);
        } catch (Exception ex) {
        }
    }
}

Related

  1. setFieldContent(final Object obj, final String name, final Object value)
  2. setFieldEditable(Class clazz, String fieldName)
  3. setFieldForAnnotation(Object target, Class annotation, Object value)
  4. setFieldHelper(Object o, String name, Object v)
  5. setFieldIfExists(final Object instance, final String fieldName, final Object value)
  6. setFieldObject(Class target, Object targetObject, String fieldName, Object object)
  7. setFieldObjectValue(Class targetClass, Object target, String fieldName, Object value)
  8. setFieldPrimitive(Object target, Field field, Object value)
  9. setFields(Field field, Object value, Object config)