Java Reflection Field Value Set setFieldValue(Object o, Class baseClass, Class fieldType, T value)

Here you can find the source of setFieldValue(Object o, Class baseClass, Class fieldType, T value)

Description

set Field Value

License

Open Source License

Declaration

public static <T> void setFieldValue(Object o, Class<?> baseClass, Class<T> fieldType, T value) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * This file is part of Minebot./*from   w  ww  . j  a  va 2s.  com*/
 *
 * Minebot 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
 * (at your option) any later version.
 *
 * Minebot 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 Minebot.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class Main {
    public static <T> void setFieldValue(Object o, Class<?> baseClass, Class<T> fieldType, T value) {
        if (o == null) {
            throw new NullPointerException();
        }
        if (!baseClass.isAssignableFrom(o.getClass())) {
            throw new IllegalArgumentException(
                    "Got a " + o.getClass().getName() + " but expected a " + baseClass.getName());
        }
        for (Field f : baseClass.getDeclaredFields()) {
            if (typeEquals(f.getType(), fieldType) && !Modifier.isStatic(f.getModifiers())) {
                f.setAccessible(true);
                try {
                    f.set(o, value);
                    return;
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                    throw new IllegalArgumentException(e);
                }
            }
        }

        throw new IllegalArgumentException("No field of type " + fieldType + " in " + baseClass);
    }

    public static boolean typeEquals(Class<?> a, Class<?> b) {
        return a.equals(b);
    }
}

Related

  1. setFieldValue(Object bean, String field, Object value)
  2. setFieldValue(Object host, Field f, Object value)
  3. setFieldValue(Object input, Object value, String fieldName)
  4. setFieldValue(Object instance, Field field, Object value)
  5. setFieldValue(Object instanceContainingField, String fieldName, Object fieldValue)
  6. SetFieldValue(Object o, String field, Object object)
  7. setFieldValue(Object obj, Field f, Object value)
  8. setFieldValue(Object obj, Field field, Object value)
  9. setFieldValue(Object obj, Field field, Object value)