从提供的类中获取一个Field对象,该对象将对指定方法名称的数据访问进行后端访问
import java.lang.reflect.Field;/** 时 代 J a v a 提 供 **/ import java.lang.reflect.Modifier; import java.security.AccessController; import sun.reflect.FieldAccessor; import sun.reflect.ReflectionFactory; public class Main{ /** * Get a Field object which backends data access to the given method name, * from the supplied class * @param m The name of the method * @param c The class to get the method from * @return a Field with an accessor that backends to the requested Method * @throws NoSuchMethodException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static final Field getMethodField(String m, Class c) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException { // First, get ANY field at all to use as a base Field f = c.getDeclaredFields()[0]; // Now set up the internal structures to 'look like' the given method // but chop up the name so that it is prettier for output if (m.startsWith("get") || m.startsWith("set")) m = m.substring(3, 4).toLowerCase() + m.substring(4); set("name", f, m); set("modifiers", f, Modifier.PUBLIC); // Finally, override the default FieldAccesors with our own FieldAccessor fa = new MethodPseudoAccessor(c, m);/* 来自 时 代 Java - nowjava.com*/ set("fieldAccessor", f, fa); set("overrideFieldAccessor", f, fa); return f; } /** * Find the requested Field declared in the target object's class, and set * its value (irrespective of the field's modifiers) * @param fi The field name to seek * @param target The target object * @param v The value to set the field to * @throws java.lang.IllegalArgumentException * @throws java.lang.IllegalAccessException */ public static final void set(String fi, Object target, Object v) throws IllegalArgumentException, IllegalAccessException { set(target.getClass(), fi, target, v); } /** * Find the requested Field declared in the given class, and set its value * (irrespective of the field's modifiers) * @param c The Class to look in * @param fi The field name to seek * @param target The target object * @param v The value to set the field to * @throws java.lang.IllegalArgumentException * @throws java.lang.IllegalAccessException */ public static final void set(Class c, String fi, Object target, Object v) throws IllegalArgumentException, IllegalAccessException { for (Field f0 : c.getDeclaredFields()) if (f0.getName().equals(fi)) set(f0, target, v); } /** * Set the given field on target to value, irrespective of its modifiers * @param f The Field to set * @param target The object to set it on * @param v The value to set the field to * @throws java.lang.IllegalArgumentException * @throws java.lang.IllegalAccessException */ public static final void set(Field f, Object target, Object v)