コード例 #1
0
 public void testNewInstance() {
   ConstructorAccess<SomeClass> access = ConstructorAccess.get(SomeClass.class);
   SomeClass someObject = new SomeClass();
   assertEquals(someObject, access.newInstance());
   assertEquals(someObject, access.newInstance());
   assertEquals(someObject, access.newInstance());
 }
コード例 #2
0
 public void testPackagePrivateNewInstance() {
   ConstructorAccess<PackagePrivateClass> access =
       ConstructorAccess.get(PackagePrivateClass.class);
   PackagePrivateClass someObject = new PackagePrivateClass();
   assertEquals(someObject, access.newInstance());
   assertEquals(someObject, access.newInstance());
   assertEquals(someObject, access.newInstance());
 }
コード例 #3
0
 /**
  * Get the constructor access of this class.
  *
  * @param <T> the class type
  * @param clazz the class
  * @return the constructor access
  */
 public static final <T> ConstructorAccess<T> getConstructorAccess(Class<T> clazz) {
   @SuppressWarnings("unchecked")
   ConstructorAccess<T> ca = (ConstructorAccess<T>) CSTR_ACCESS_MAP.get(clazz);
   if (ca == null) {
     ca = ConstructorAccess.get(clazz);
     CSTR_ACCESS_MAP.putIfAbsent(clazz, ca);
   }
   return ca;
 }
コード例 #4
0
 public void testHasPublicConstructor() {
   try {
     ConstructorAccess<HasPublicConstructor> access =
         ConstructorAccess.get(HasPublicConstructor.class);
     HasPublicConstructor newInstance = access.newInstance();
     assertEquals("cow", newInstance.getMoo());
   } catch (Throwable t) {
     System.out.println("Unexpected exception happened: " + t);
     assertTrue(false);
   }
 }
コード例 #5
0
 public void testHasPrivateConstructor() {
   try {
     ConstructorAccess.get(HasPrivateConstructor.class);
     assertTrue(false);
   } catch (RuntimeException re) {
     System.out.println("Expected exception happened: " + re);
   } catch (Throwable t) {
     System.out.println("Unexpected exception happened: " + t);
     assertTrue(false);
   }
 }