Ejemplo n.º 1
0
  @Test
  public void findField() {
    Field field =
        ReflectionUtils.findField(
            TestObjectSubclassWithPublicField.class, "publicField", String.class);
    assertNotNull(field);
    assertEquals("publicField", field.getName());
    assertEquals(String.class, field.getType());
    assertTrue("Field should be public.", Modifier.isPublic(field.getModifiers()));

    field = ReflectionUtils.findField(TestObjectSubclassWithNewField.class, "prot", String.class);
    assertNotNull(field);
    assertEquals("prot", field.getName());
    assertEquals(String.class, field.getType());
    assertTrue("Field should be protected.", Modifier.isProtected(field.getModifiers()));

    field = ReflectionUtils.findField(TestObjectSubclassWithNewField.class, "name", String.class);
    assertNotNull(field);
    assertEquals("name", field.getName());
    assertEquals(String.class, field.getType());
    assertTrue("Field should be private.", Modifier.isPrivate(field.getModifiers()));
  }
Ejemplo n.º 2
0
  @Test
  public void setField() {
    TestObjectSubclassWithNewField testBean = new TestObjectSubclassWithNewField();
    Field field =
        ReflectionUtils.findField(TestObjectSubclassWithNewField.class, "name", String.class);

    ReflectionUtils.makeAccessible(field);

    ReflectionUtils.setField(field, testBean, "FooBar");
    assertNotNull(testBean.getName());
    assertEquals("FooBar", testBean.getName());

    ReflectionUtils.setField(field, testBean, null);
    assertNull(testBean.getName());
  }