Here you can find the source of setFieldValue(Object o, Class> baseClass, Class
public static <T> void setFieldValue(Object o, Class<?> baseClass, Class<T> fieldType, T value)
//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); } }