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()); }
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()); }
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); } }
/** * 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; }
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); } }