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;
}
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;
}
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");
}
}
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());
}
}
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;
}
}
}
}
Aggregations