/** * Gets an accessible {@link Field} by name respecting scope. Superclasses/interfaces will be considered. * * @param cls * the {@link Class} to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @return the Field object * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty */ public static Field getField(final Class<?> cls, final String fieldName) { final Field field = getField(cls, fieldName, false); MemberUtils.setAccessibleWorkaround(field); return field; }
private void reset() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { // 从GreysClassLoader中加载Spy final Class<?> spyClassFromGreysClassLoader = loadSpyClassFromGreysClassLoader( ShutdownCommand.class.getClassLoader(), "com.github.ompc.greys.agent.Spy" ); if (null != spyClassFromGreysClassLoader) { // 重置整个greys final Method agentResetMethod = (Method) getField(spyClassFromGreysClassLoader, "AGENT_RESET_METHOD").get(null); agentResetMethod.invoke(null); } }
/** * Reads the named {@code static} {@link Field}. Superclasses will be considered. * * @param cls * the {@link Class} to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only * match {@code public} fields. * @return the Field object * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty, is not {@code static}, or could * not be found * @throws IllegalAccessException * if the field is not made accessible */ public static Object readStaticField(final Class<?> cls, final String fieldName, final boolean forceAccess) throws IllegalAccessException { final Field field = getField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate field '%s' on %s", fieldName, cls); // already forced access above, don't repeat it here: return readStaticField(field, false); }
/** * Reads the named {@link Field}. Superclasses will be considered. * * @param target * the object to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only * match {@code public} fields. * @return the field value * @throws IllegalArgumentException * if {@code target} is {@code null}, or the field name is blank or empty or could not be found * @throws IllegalAccessException * if the named field is not made accessible */ public static Object readField(final Object target, final String fieldName, final boolean forceAccess) throws IllegalAccessException { Validate.isTrue(target != null, "target object must not be null"); final Class<?> cls = target.getClass(); final Field field = getField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate field %s on %s", fieldName, cls); // already forced access above, don't repeat it here: return readField(field, target, false); }
@Test(expected = IllegalArgumentException.class) public void testGetFieldIllegalArgumentException3() { FieldUtils.getField(PublicChild.class, ""); }
@Test(expected = IllegalArgumentException.class) public void testAmbig() { FieldUtils.getField(Ambig.class, "VALUE"); }
@Test(expected = IllegalArgumentException.class) public void testGetFieldForceAccessIllegalArgumentException1() { FieldUtils.getField(null, "none", true); }
@Test(expected = IllegalArgumentException.class) public void testGetFieldForceAccessIllegalArgumentException2() { FieldUtils.getField(PublicChild.class, null, true); }
@Test(expected = IllegalArgumentException.class) public void testGetFieldForceAccessIllegalArgumentException4() { FieldUtils.getField(PublicChild.class, " ", true); }
@Test(expected = IllegalArgumentException.class) public void testGetFieldIllegalArgumentException1() { FieldUtils.getField(null, "none"); }
@Test(expected = IllegalArgumentException.class) public void testGetFieldIllegalArgumentException2() { FieldUtils.getField(PublicChild.class, null); }
@Test(expected = IllegalArgumentException.class) public void testGetFieldIllegalArgumentException4() { FieldUtils.getField(PublicChild.class, " "); }
@Test(expected = IllegalArgumentException.class) public void testGetFieldForceAccessIllegalArgumentException3() { FieldUtils.getField(PublicChild.class, "", true); }
@Test public void testGetField() { assertEquals(Foo.class, FieldUtils.getField(PublicChild.class, "VALUE").getDeclaringClass()); assertEquals(parentClass, FieldUtils.getField(PublicChild.class, "s").getDeclaringClass()); assertNull(FieldUtils.getField(PublicChild.class, "b")); assertNull(FieldUtils.getField(PublicChild.class, "i")); assertNull(FieldUtils.getField(PublicChild.class, "d")); assertEquals(Foo.class, FieldUtils.getField(PubliclyShadowedChild.class, "VALUE").getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "s").getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "b").getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "i").getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "d").getDeclaringClass()); assertEquals(Foo.class, FieldUtils.getField(PrivatelyShadowedChild.class, "VALUE").getDeclaringClass()); assertEquals(parentClass, FieldUtils.getField(PrivatelyShadowedChild.class, "s").getDeclaringClass()); assertNull(FieldUtils.getField(PrivatelyShadowedChild.class, "b")); assertNull(FieldUtils.getField(PrivatelyShadowedChild.class, "i")); assertNull(FieldUtils.getField(PrivatelyShadowedChild.class, "d")); }
@Test public void testGetFieldForceAccess() { assertEquals(PublicChild.class, FieldUtils.getField(PublicChild.class, "VALUE", true).getDeclaringClass()); assertEquals(parentClass, FieldUtils.getField(PublicChild.class, "s", true).getDeclaringClass()); assertEquals(parentClass, FieldUtils.getField(PublicChild.class, "b", true).getDeclaringClass()); assertEquals(parentClass, FieldUtils.getField(PublicChild.class, "i", true).getDeclaringClass()); assertEquals(parentClass, FieldUtils.getField(PublicChild.class, "d", true).getDeclaringClass()); assertEquals(Foo.class, FieldUtils.getField(PubliclyShadowedChild.class, "VALUE", true).getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "s", true).getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "b", true).getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "i", true).getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "d", true).getDeclaringClass()); assertEquals(Foo.class, FieldUtils.getField(PrivatelyShadowedChild.class, "VALUE", true).getDeclaringClass()); assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "s", true).getDeclaringClass()); assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "b", true).getDeclaringClass()); assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "i", true).getDeclaringClass()); assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "d", true).getDeclaringClass()); }
@Test(expected = IllegalArgumentException.class) public void testReadStaticFieldIllegalArgumentException2() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(Foo.class, "VALUE"))); final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s"); assumeNotNull(nonStaticField); FieldUtils.readStaticField(nonStaticField); }
@Test(expected = IllegalArgumentException.class) public void testReadStaticFieldForceAccessIllegalArgumentException2() throws Exception { final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s", true); assumeNotNull(nonStaticField); FieldUtils.readStaticField(nonStaticField); }
@Test public void testReadStaticField() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(Foo.class, "VALUE"))); }
public static void resetClock(final Class<?> clazz) throws IllegalAccessException { System.clearProperty(ClockFactory.PROPERTY_NAME); final Field field = FieldUtils.getField(clazz, "CLOCK", true); FieldUtils.removeFinalModifier(field, true); FieldUtils.writeStaticField(field, ClockFactory.getClock(), false); }