Search in sources :

Example 1 with DIRuntimeException

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

the class JCacheManagerProvider method get.

@Override
public CacheManager get() throws DIRuntimeException {
    CachingProvider provider;
    try {
        provider = Caching.getCachingProvider();
    } catch (CacheException e) {
        throw new RuntimeException("'cayenne-jcache' doesn't bundle any JCache providers. " + "You must place a JCache 1.0 provider on classpath explicitly.", e);
    }
    CacheManager manager;
    URI jcacheConfig = getConfig();
    if (jcacheConfig == null) {
        manager = provider.getCacheManager();
    } else {
        manager = provider.getCacheManager(jcacheConfig, null);
    }
    return manager;
}
Also used : DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) CacheException(javax.cache.CacheException) CacheManager(javax.cache.CacheManager) URI(java.net.URI) CachingProvider(javax.cache.spi.CachingProvider)

Example 2 with DIRuntimeException

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

the class DefaultAdhocObjectFactory method newInstance.

@SuppressWarnings("unchecked")
@Override
public <T> T newInstance(Class<? super T> superType, String className) {
    if (superType == null) {
        throw new NullPointerException("Null superType");
    }
    if (className == null) {
        throw new NullPointerException("Null className");
    }
    Class<T> type = (Class<T>) getJavaClass(className);
    if (!superType.isAssignableFrom(type)) {
        throw new DIRuntimeException("Class %s is not assignable to %s", className, superType.getName());
    }
    T instance;
    try {
        Provider<T> provider0 = new ConstructorInjectingProvider<T>(type, (DefaultInjector) injector);
        Provider<T> provider1 = new FieldInjectingProvider<T>(provider0, (DefaultInjector) injector);
        instance = provider1.get();
    } catch (Exception e) {
        throw new DIRuntimeException("Error creating instance of class %s of type %s", e, className, superType.getName());
    }
    return instance;
}
Also used : DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException)

Example 3 with DIRuntimeException

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

the class DefaultInjectorCircularInjectionTest method testFieldInjection_CircularDependency.

@Test
public void testFieldInjection_CircularDependency() {
    Module module = binder -> {
        binder.bind(MockInterface1.class).to(MockImplementation1_DepOn2.class);
        binder.bind(MockInterface2.class).to(MockImplementation2.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) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) Module(org.apache.cayenne.di.Module) MockImplementation1_DepOn2(org.apache.cayenne.di.mock.MockImplementation1_DepOn2) MockImplementation2(org.apache.cayenne.di.mock.MockImplementation2) Test(org.junit.Test)

Example 4 with DIRuntimeException

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

the class ConstructorInjectingProvider method get.

@Override
public T get() {
    Class<?>[] constructorParameters = constructor.getParameterTypes();
    Type[] genericTypes = constructor.getGenericParameterTypes();
    Object[] args = new Object[constructorParameters.length];
    InjectionStack stack = injector.getInjectionStack();
    for (int i = 0; i < constructorParameters.length; i++) {
        args[i] = value(constructorParameters[i], genericTypes[i], bindingNames[i], stack);
    }
    try {
        return constructor.newInstance(args);
    } catch (Exception e) {
        throw new DIRuntimeException("Error instantiating class '%s'", e, constructor.getDeclaringClass().getName());
    }
}
Also used : Type(java.lang.reflect.Type) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException)

Example 5 with DIRuntimeException

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

the class ConstructorInjectingProvider method initConstructor.

@SuppressWarnings("unchecked")
private void initConstructor(Class<? extends T> implementation) {
    Constructor<?>[] constructors = implementation.getDeclaredConstructors();
    Constructor<?> lastMatch = null;
    int lastSize = -1;
    // if multiple matches are found
    for (Constructor<?> constructor : constructors) {
        int size = constructor.getParameterTypes().length;
        if (size <= lastSize) {
            continue;
        }
        if (size == 0) {
            lastSize = 0;
            lastMatch = constructor;
            continue;
        }
        boolean injectable = true;
        for (Annotation[] annotations : constructor.getParameterAnnotations()) {
            boolean parameterInjectable = false;
            for (Annotation annotation : annotations) {
                if (annotation.annotationType().equals(Inject.class)) {
                    parameterInjectable = true;
                    break;
                }
            }
            if (!parameterInjectable) {
                injectable = false;
                break;
            }
        }
        if (injectable) {
            lastSize = size;
            lastMatch = constructor;
        }
    }
    if (lastMatch == null) {
        throw new DIRuntimeException("No applicable constructor is found for constructor injection in class '%s'", implementation.getName());
    }
    // the cast is lame, but Class.getDeclaredConstructors() is not using
    // generics in Java 5 and using <?> in Java 6, creating compilation problems.
    this.constructor = (Constructor<? extends T>) lastMatch;
    Annotation[][] annotations = lastMatch.getParameterAnnotations();
    this.bindingNames = new String[annotations.length];
    for (int i = 0; i < annotations.length; i++) {
        Annotation[] parameterAnnotations = annotations[i];
        for (int j = 0; j < parameterAnnotations.length; j++) {
            Annotation annotation = parameterAnnotations[j];
            if (annotation.annotationType().equals(Inject.class)) {
                Inject inject = (Inject) annotation;
                bindingNames[i] = inject.value();
                break;
            }
        }
    }
}
Also used : Inject(org.apache.cayenne.di.Inject) Constructor(java.lang.reflect.Constructor) DIRuntimeException(org.apache.cayenne.di.DIRuntimeException) Annotation(java.lang.annotation.Annotation)

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