Search in sources :

Example 6 with DIRuntimeException

use of org.apache.cayenne.di.DIRuntimeException in project cayenne by apache.

the class FieldInjectingProvider method injectMember.

private void injectMember(Object object, Field field, String bindingName) {
    Object value = value(field, bindingName);
    field.setAccessible(true);
    try {
        field.set(object, value);
    } catch (Exception e) {
        String message = String.format("Error injecting into field %s.%s of type %s", field.getDeclaringClass().getName(), field.getName(), field.getType().getName());
        throw new DIRuntimeException(message, e);
    }
}
Also used : DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException)

Example 7 with DIRuntimeException

use of org.apache.cayenne.di.DIRuntimeException in project cayenne by apache.

the class DefaultInjectorCircularInjectionTest method testConstructorInjection_WithFieldInjectionDeps.

@Test
public void testConstructorInjection_WithFieldInjectionDeps() {
    Module module = binder -> {
        binder.bind(MockInterface1.class).to(MockImplementation1_DepOn2Constructor.class);
        binder.bind(MockInterface2.class).to(MockImplementation2_I3Dependency.class);
        binder.bind(MockInterface3.class).to(MockImplementation3.class);
    };
    DefaultInjector injector = new DefaultInjector(module);
    try {
        injector.getInstance(MockInterface1.class);
    } catch (DIRuntimeException e) {
        fail("Circular dependency is detected incorrectly: " + e.getMessage());
    }
}
Also used : MockImplementation2_Constructor(org.apache.cayenne.di.mock.MockImplementation2_Constructor) MockImplementation1_DepOn2(org.apache.cayenne.di.mock.MockImplementation1_DepOn2) Module(org.apache.cayenne.di.Module) MockImplementation2(org.apache.cayenne.di.mock.MockImplementation2) Test(org.junit.Test) MockImplementation3(org.apache.cayenne.di.mock.MockImplementation3) MockImplementation2_I3Dependency(org.apache.cayenne.di.mock.MockImplementation2_I3Dependency) MockImplementation1_DepOn2Constructor(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Constructor) MockInterface2(org.apache.cayenne.di.mock.MockInterface2) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) Assert.fail(org.junit.Assert.fail) MockImplementation1_DepOn2Provider(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Provider) MockInterface3(org.apache.cayenne.di.mock.MockInterface3) Assert.assertEquals(org.junit.Assert.assertEquals) MockImplementation1_DepOn2Constructor(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Constructor) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) Module(org.apache.cayenne.di.Module) MockImplementation3(org.apache.cayenne.di.mock.MockImplementation3) MockImplementation2_I3Dependency(org.apache.cayenne.di.mock.MockImplementation2_I3Dependency) Test(org.junit.Test)

Example 8 with DIRuntimeException

use of org.apache.cayenne.di.DIRuntimeException in project cayenne by apache.

the class DefaultInjectorCircularInjectionTest method testConstructorInjection_CircularDependency.

@Test
public void testConstructorInjection_CircularDependency() {
    Module module = binder -> {
        binder.bind(MockInterface1.class).to(MockImplementation1_DepOn2Constructor.class);
        binder.bind(MockInterface2.class).to(MockImplementation2_Constructor.class);
    };
    DefaultInjector injector = new DefaultInjector(module);
    try {
        injector.getInstance(MockInterface1.class);
        fail("Circular dependency is not detected.");
    } catch (DIRuntimeException e) {
    // expected
    } catch (StackOverflowError e) {
        fail("Circular dependency is not detected, causing stack overflow");
    }
}
Also used : MockImplementation2_Constructor(org.apache.cayenne.di.mock.MockImplementation2_Constructor) MockImplementation1_DepOn2(org.apache.cayenne.di.mock.MockImplementation1_DepOn2) Module(org.apache.cayenne.di.Module) MockImplementation2(org.apache.cayenne.di.mock.MockImplementation2) Test(org.junit.Test) MockImplementation3(org.apache.cayenne.di.mock.MockImplementation3) MockImplementation2_I3Dependency(org.apache.cayenne.di.mock.MockImplementation2_I3Dependency) MockImplementation1_DepOn2Constructor(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Constructor) MockInterface2(org.apache.cayenne.di.mock.MockInterface2) MockInterface1(org.apache.cayenne.di.mock.MockInterface1) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) Assert.fail(org.junit.Assert.fail) MockImplementation1_DepOn2Provider(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Provider) MockInterface3(org.apache.cayenne.di.mock.MockInterface3) Assert.assertEquals(org.junit.Assert.assertEquals) MockImplementation1_DepOn2Constructor(org.apache.cayenne.di.mock.MockImplementation1_DepOn2Constructor) MockImplementation2_Constructor(org.apache.cayenne.di.mock.MockImplementation2_Constructor) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) Module(org.apache.cayenne.di.Module) Test(org.junit.Test)

Aggregations

DIRuntimeException (org.apache.cayenne.di.DIRuntimeException)8 Module (org.apache.cayenne.di.Module)3 MockImplementation1_DepOn2 (org.apache.cayenne.di.mock.MockImplementation1_DepOn2)3 MockImplementation1_DepOn2Constructor (org.apache.cayenne.di.mock.MockImplementation1_DepOn2Constructor)3 MockImplementation1_DepOn2Provider (org.apache.cayenne.di.mock.MockImplementation1_DepOn2Provider)3 MockImplementation2 (org.apache.cayenne.di.mock.MockImplementation2)3 MockImplementation2_Constructor (org.apache.cayenne.di.mock.MockImplementation2_Constructor)3 MockImplementation2_I3Dependency (org.apache.cayenne.di.mock.MockImplementation2_I3Dependency)3 MockImplementation3 (org.apache.cayenne.di.mock.MockImplementation3)3 MockInterface1 (org.apache.cayenne.di.mock.MockInterface1)3 MockInterface2 (org.apache.cayenne.di.mock.MockInterface2)3 MockInterface3 (org.apache.cayenne.di.mock.MockInterface3)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.fail (org.junit.Assert.fail)3 Test (org.junit.Test)3 Annotation (java.lang.annotation.Annotation)1 Constructor (java.lang.reflect.Constructor)1 Type (java.lang.reflect.Type)1 URI (java.net.URI)1 CacheException (javax.cache.CacheException)1