Here you can find the source of setField(Field field, Object instance, Object value)
Parameter | Description |
---|---|
field | - The field to set |
instance | - The instance of the field to set |
value | - The value to set the field |
public static boolean setField(Field field, Object instance, Object value)
//package com.java2s; //License from project: LGPL import java.lang.reflect.Field; public class Main { /**/*from w w w.j a va2s .com*/ * Sets the specified field to the specified value in the instance. * * @param field - The field to set * @param instance - The instance of the field to set * @param value - The value to set the field * @return True if setting the field was successful. */ public static boolean setField(Field field, Object instance, Object value) { if (field == null) { System.err.println("Null field"); return false; } try { field.set(instance, value); } catch (Exception e) { System.err.println(field.getType() + " not assignable from " + value.getClass()); return false; } return true; } /** * Finds the first class that matches one of the names specified * * @param classes - A list of all class names * @return The first class found, or null */ public static Class<?> getClass(String... classes) { if (classes.length == 0) { System.err.println("No classes specified."); return null; } for (String name : classes) { try { return Class.forName(name); } catch (Throwable t) { } } return null; } }