ReConstructor { public static <R> R constructFrom( @NonNull Class<R> assignableClass, @NonNull Context context, @NonNull AttributeSet attrs, int defStyleAttr, @NonNull String targetName ) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException { Class<? extends R> generatorClass; ReConstructor<R> factory = new ReConstructor<>(assignableClass); generatorClass = factory.loadClassIn(targetName, context.getClassLoader()); return factory.constructWithReflectionFrom(generatorClass, context, attrs, defStyleAttr); } protected  ReConstructor(Class<T> assignableClass); static R constructFrom(
            @NonNull Class<R> assignableClass,
            @NonNull Context context,
            @NonNull AttributeSet attrs,
            int defStyleAttr,
            @NonNull String targetName
    );  }
@Test(expected = ClassNotFoundException.class) public void reconstructClassWithUnknownName() throws Exception { constructFrom( ReflectiveInterface.class, mockContext, mockAttrs, 0, "invalid class name" ); }
@Test(expected = IllegalAccessException.class) public void reconstructClassWithInaccessibleName() throws Exception { constructFrom( ReflectiveInterface.class, mockContext, mockAttrs, 0, "com.fuzz.indicator.reflect.subpackage.PackagePrivateClass" ); }
@Test(expected = ClassCastException.class) public void reconstructClassWithoutInterfaceWithoutPublicConstructor() throws Exception { constructFrom( ReflectiveInterface.class, mockContext, mockAttrs, 0, PrivateConstructorWithoutInterfaceClass.class.getName() ); }
@Test(expected = InstantiationException.class) public void reconstructAbstractClassWithInterface() throws Exception { constructFrom( ReflectiveInterface.class, mockContext, mockAttrs, 0, PrivateConstructorWithInterfaceAbstractClass.class.getName() ); }
@Test(expected = InvocationTargetException.class) public void reconstructClassWithRightInterfaceWithExceptionalPublicConstructor() throws Exception { constructFrom( ReflectiveInterface.class, mockContext, mockAttrs, 0, PublicConstructorThatExceptsClass.class.getName() ); }
@Test public void reconstructClassWithRightInterfaceWithOnlyPrivateConstructor() throws Exception { ReflectiveInterface constructed = constructFrom( ReflectiveInterface.class, mockContext, mockAttrs, 0, PrivateConstructorClass.class.getName() ); assertNull("ReConstructor shouldn't instantiate a class with only a private constructor.", constructed); }
@Test public void reconstructClassWithRightInterfaceWithOnlyPrivateConstructors() throws Exception { ReflectiveInterface constructed = constructFrom( ReflectiveInterface.class, mockContext, mockAttrs, 0, PrivateConstructorsClass.class.getName() ); assertNull("ReConstructor shouldn't instantiate a class with only private constructors.", constructed); }
@Test public void reconstructClassWithRightInterfaceWithoutUsablePublicConstructor() throws Exception { ReflectiveInterface constructed = constructFrom( ReflectiveInterface.class, mockContext, mockAttrs, 0, PublicUnusableConstructorClass.class.getName() ); assertNull("ReConstructor shouldn't instantiate a class with a public constructor unless it can call that constructor.", constructed); }
@Test public void reconstructClassWithRightInterfaceWithPublicConstructor() throws Exception { ReflectiveInterface constructed = constructFrom( ReflectiveInterface.class, mockContext, mockAttrs, 0, PublicConstructorClass.class.getName() ); assertNotNull("ReConstructor should instantiate a class with a valid public constructor.", constructed); assertThat(constructed, instanceOf(PublicConstructorClass.class)); assertNull("ReConstructor should not call any private constructors", ((PublicConstructorClass) constructed).context); }
@Test public void reconstructClassWithRightInterfaceWithCorrectParameters() throws Exception { ReflectiveInterface constructed = constructFrom( ReflectiveInterface.class, mockContext, mockAttrs, 0, PublicSavedParametersClass.class.getName() ); assertNotNull(constructed); assertThat(constructed, instanceOf(PublicSavedParametersClass.class)); assertEquals(mockContext, ((PublicSavedParametersClass)constructed).context); assertEquals(mockAttrs, ((PublicSavedParametersClass)constructed).attrs); }