Here you can find the source of setField(Class> clazz, Object instance, String field, Object value)
public static void setField(Class<?> clazz, Object instance, String field, Object value) throws NoSuchFieldException, IllegalAccessException
//package com.java2s; /*//from w w w . j a v a 2 s. co m * BungeeTabListPlus - a BungeeCord plugin to customize the tablist * * Copyright (C) 2014 - 2015 Florian Stober * * This program 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. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ import java.lang.reflect.Field; public class Main { public static void setField(Class<?> clazz, Object instance, String field, Object value) throws NoSuchFieldException, IllegalAccessException { Field f = clazz.getDeclaredField(field); f.setAccessible(true); f.set(instance, value); } public static void setField(Class<?> clazz, Object instance, String field, Object value, int tries) throws NoSuchFieldException, IllegalAccessException { while (--tries > 0) { try { setField(clazz, instance, field, value); return; } catch (NoSuchFieldException | IllegalAccessException ignored) { } } setField(clazz, instance, field, value); } }