@SuppressWarnings("unchecked")
 private void doTestCache() {
   for (int i = 0; i < toConstruct.length; i++) {
     Class cl = toConstruct[i];
     Object x = ReflectionUtils.newInstance(cl, null);
     Object y = ReflectionUtils.newInstance(cl, null);
     assertEquals(cl, x.getClass());
     assertEquals(cl, y.getClass());
   }
 }
  @Test
  public void isCglibRenamedMethod() throws SecurityException, NoSuchMethodException {
    @SuppressWarnings("unused")
    class C {
      public void CGLIB$m1$123() {}

      public void CGLIB$m1$0() {}

      public void CGLIB$$0() {}

      public void CGLIB$m1$() {}

      public void CGLIB$m1() {}

      public void m1() {}

      public void m1$() {}

      public void m1$1() {}
    }
    assertTrue(ReflectionUtils.isCglibRenamedMethod(C.class.getMethod("CGLIB$m1$123")));
    assertTrue(ReflectionUtils.isCglibRenamedMethod(C.class.getMethod("CGLIB$m1$0")));
    assertFalse(ReflectionUtils.isCglibRenamedMethod(C.class.getMethod("CGLIB$$0")));
    assertFalse(ReflectionUtils.isCglibRenamedMethod(C.class.getMethod("CGLIB$m1$")));
    assertFalse(ReflectionUtils.isCglibRenamedMethod(C.class.getMethod("CGLIB$m1")));
    assertFalse(ReflectionUtils.isCglibRenamedMethod(C.class.getMethod("m1")));
    assertFalse(ReflectionUtils.isCglibRenamedMethod(C.class.getMethod("m1$")));
    assertFalse(ReflectionUtils.isCglibRenamedMethod(C.class.getMethod("m1$1")));
  }
  @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());
  }
 @Test
 public void getUniqueDeclaredMethods_withCovariantReturnType() throws Exception {
   class Parent {
     @SuppressWarnings("unused")
     public Number m1() {
       return new Integer(42);
     }
   }
   class Leaf extends Parent {
     @Override
     public Integer m1() {
       return new Integer(42);
     }
   }
   int m1MethodCount = 0;
   Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(Leaf.class);
   for (Method method : methods) {
     if (method.getName().equals("m1")) {
       m1MethodCount++;
     }
   }
   assertThat(m1MethodCount, is(1));
   assertTrue(ObjectUtils.containsElement(methods, Leaf.class.getMethod("m1")));
   assertFalse(ObjectUtils.containsElement(methods, Parent.class.getMethod("m1")));
 }
  @Test
  public void invokeMethod() throws Exception {
    String rob = "Rob Harrop";

    TestObject bean = new TestObject();
    bean.setName(rob);

    Method getName = TestObject.class.getMethod("getName", (Class[]) null);
    Method setName = TestObject.class.getMethod("setName", String.class);

    Object name = ReflectionUtils.invokeMethod(getName, bean);
    assertEquals("Incorrect name returned", rob, name);

    String juergen = "Juergen Hoeller";
    ReflectionUtils.invokeMethod(setName, bean, juergen);
    assertEquals("Incorrect name set", juergen, bean.getName());
  }
 @Test
 public void testCache() throws Exception {
   assertEquals(0, cacheSize());
   doTestCache();
   assertEquals(toConstruct.length, cacheSize());
   ReflectionUtils.clearCache();
   assertEquals(0, cacheSize());
 }
 @Test
 public void testCantCreate() {
   try {
     ReflectionUtils.newInstance(NoDefaultCtor.class, null);
     fail("invalid call should fail");
   } catch (RuntimeException rte) {
     assertEquals(NoSuchMethodException.class, rte.getCause().getClass());
   }
 }
  private void testValidCopy(TestObject src, TestObject dest) {
    src.setName("freddie");
    src.setAge(15);
    src.setSpouse(new TestObject());
    assertFalse(src.getAge() == dest.getAge());

    ReflectionUtils.shallowCopyFieldState(src, dest);
    assertEquals(src.getAge(), dest.getAge());
    assertEquals(src.getSpouse(), dest.getSpouse());
  }
 @Test
 public void duplicatesFound() {
   ListSavingMethodCallback mc = new ListSavingMethodCallback();
   ReflectionUtils.doWithMethods(TestObjectSubclass.class, mc);
   int absquatulateCount = 0;
   for (String name : mc.getMethodNames()) {
     if (name.equals("absquatulate")) {
       ++absquatulateCount;
     }
   }
   assertEquals("Found 2 absquatulates", 2, absquatulateCount);
 }
  @Test
  public void declaresException() throws Exception {
    Method remoteExMethod = A.class.getDeclaredMethod("foo", Integer.class);
    assertTrue(ReflectionUtils.declaresException(remoteExMethod, RemoteException.class));
    assertTrue(ReflectionUtils.declaresException(remoteExMethod, ConnectException.class));
    assertFalse(ReflectionUtils.declaresException(remoteExMethod, NoSuchMethodException.class));
    assertFalse(ReflectionUtils.declaresException(remoteExMethod, Exception.class));

    Method illegalExMethod = B.class.getDeclaredMethod("bar", String.class);
    assertTrue(ReflectionUtils.declaresException(illegalExMethod, IllegalArgumentException.class));
    assertTrue(ReflectionUtils.declaresException(illegalExMethod, NumberFormatException.class));
    assertFalse(ReflectionUtils.declaresException(illegalExMethod, IllegalStateException.class));
    assertFalse(ReflectionUtils.declaresException(illegalExMethod, Exception.class));
  }
  @Test
  public void testGetDeclaredFieldsIncludingInherited() {
    Parent child =
        new Parent() {
          private int childField;

          @SuppressWarnings("unused")
          public int getChildField() {
            return childField;
          }
        };

    List<Field> fields = ReflectionUtils.getDeclaredFieldsIncludingInherited(child.getClass());
    boolean containsParentField = false;
    boolean containsChildField = false;
    for (Field field : fields) {
      if (field.getName().equals("parentField")) {
        containsParentField = true;
      } else if (field.getName().equals("childField")) {
        containsChildField = true;
      }
    }

    List<Method> methods = ReflectionUtils.getDeclaredMethodsIncludingInherited(child.getClass());
    boolean containsParentMethod = false;
    boolean containsChildMethod = false;
    for (Method method : methods) {
      if (method.getName().equals("getParentField")) {
        containsParentMethod = true;
      } else if (method.getName().equals("getChildField")) {
        containsChildMethod = true;
      }
    }

    assertTrue("Missing parent field", containsParentField);
    assertTrue("Missing child field", containsChildField);
    assertTrue("Missing parent method", containsParentMethod);
    assertTrue("Missing child method", containsChildMethod);
  }
  @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()));
  }
 @SuppressWarnings("unchecked")
 @Test
 public void testCacheDoesntLeak() throws Exception {
   int iterations = 9999; // very fast, but a bit less reliable - bigger numbers force GC
   for (int i = 0; i < iterations; i++) {
     URLClassLoader loader = new URLClassLoader(new URL[0], getClass().getClassLoader());
     Class cl =
         Class.forName("org.apache.hadoop.util.TestReflectionUtils$LoadedInChild", false, loader);
     Object o = ReflectionUtils.newInstance(cl, null);
     assertEquals(cl, o.getClass());
   }
   System.gc();
   assertTrue(cacheSize() + " too big", cacheSize() < iterations);
 }
 @Test
 public void getUniqueDeclaredMethods() throws Exception {
   class Foo {
     @Override
     public String toString() {
       return super.toString();
     }
   }
   int toStringMethodCount = 0;
   for (Method method : ReflectionUtils.getUniqueDeclaredMethods(Foo.class)) {
     if (method.getName().equals("toString")) {
       toStringMethodCount++;
     }
   }
   assertThat(toStringMethodCount, is(1));
 }
 @Test
 public void doWithProtectedMethods() {
   ListSavingMethodCallback mc = new ListSavingMethodCallback();
   ReflectionUtils.doWithMethods(
       TestObject.class,
       mc,
       new ReflectionUtils.MethodFilter() {
         @Override
         public boolean matches(Method m) {
           return Modifier.isProtected(m.getModifiers());
         }
       });
   assertFalse(mc.getMethodNames().isEmpty());
   assertTrue("Must find protected method on Object", mc.getMethodNames().contains("clone"));
   assertTrue("Must find protected method on Object", mc.getMethodNames().contains("finalize"));
   assertFalse("Public, not protected", mc.getMethodNames().contains("hashCode"));
   assertFalse("Public, not protected", mc.getMethodNames().contains("absquatulate"));
 }
  @Test
  public void getUniqueDeclaredMethods_isFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);

    @SuppressWarnings("unused")
    class C {
      void m00() {}

      void m01() {}

      void m02() {}

      void m03() {}

      void m04() {}

      void m05() {}

      void m06() {}

      void m07() {}

      void m08() {}

      void m09() {}

      void m10() {}

      void m11() {}

      void m12() {}

      void m13() {}

      void m14() {}

      void m15() {}

      void m16() {}

      void m17() {}

      void m18() {}

      void m19() {}

      void m20() {}

      void m21() {}

      void m22() {}

      void m23() {}

      void m24() {}

      void m25() {}

      void m26() {}

      void m27() {}

      void m28() {}

      void m29() {}

      void m30() {}

      void m31() {}

      void m32() {}

      void m33() {}

      void m34() {}

      void m35() {}

      void m36() {}

      void m37() {}

      void m38() {}

      void m39() {}

      void m40() {}

      void m41() {}

      void m42() {}

      void m43() {}

      void m44() {}

      void m45() {}

      void m46() {}

      void m47() {}

      void m48() {}

      void m49() {}

      void m50() {}

      void m51() {}

      void m52() {}

      void m53() {}

      void m54() {}

      void m55() {}

      void m56() {}

      void m57() {}

      void m58() {}

      void m59() {}

      void m60() {}

      void m61() {}

      void m62() {}

      void m63() {}

      void m64() {}

      void m65() {}

      void m66() {}

      void m67() {}

      void m68() {}

      void m69() {}

      void m70() {}

      void m71() {}

      void m72() {}

      void m73() {}

      void m74() {}

      void m75() {}

      void m76() {}

      void m77() {}

      void m78() {}

      void m79() {}

      void m80() {}

      void m81() {}

      void m82() {}

      void m83() {}

      void m84() {}

      void m85() {}

      void m86() {}

      void m87() {}

      void m88() {}

      void m89() {}

      void m90() {}

      void m91() {}

      void m92() {}

      void m93() {}

      void m94() {}

      void m95() {}

      void m96() {}

      void m97() {}

      void m98() {}

      void m99() {}
    }

    StopWatch sw = new StopWatch();
    sw.start();
    Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(C.class);
    sw.stop();
    long totalMs = sw.getTotalTimeMillis();
    assertThat(methods.length, Matchers.greaterThan(100));
    assertThat(totalMs, Matchers.lessThan(10L));
  }
 @Test(expected = IllegalArgumentException.class)
 public void rejectsNullDest() {
   TestObject src = new TestObject();
   String dest = null;
   ReflectionUtils.shallowCopyFieldState(src, dest);
 }
 private int cacheSize() throws Exception {
   return ReflectionUtils.getCacheSize();
 }
 @Test
 public void findMethod() throws Exception {
   assertNotNull(ReflectionUtils.findMethod(B.class, "bar", String.class));
   assertNotNull(ReflectionUtils.findMethod(B.class, "foo", Integer.class));
   assertNotNull(ReflectionUtils.findMethod(B.class, "getClass"));
 }
 @Before
 public void setUp() {
   ReflectionUtils.clearCache();
 }
 @Ignore("[SPR-8644] findMethod() does not currently support var-args")
 @Test
 public void findMethodWithVarArgs() throws Exception {
   assertNotNull(ReflectionUtils.findMethod(B.class, "add", int.class, int.class, int.class));
 }
 @Test(expected = IllegalArgumentException.class)
 public void copySrcToDestinationOfIncorrectClass() {
   TestObject src = new TestObject();
   String dest = new String();
   ReflectionUtils.shallowCopyFieldState(src, dest);
 }