use of org.eclipse.xtext.testing.InjectWith in project xtext-core by eclipse.
the class InjectionExtension method getOrCreateInjectorProvider.
/**
* Returns the {@link IInjectorProvider injector provider} for the given context. Tries to find an {@link InjectWith}
* annotation on the required test classes along the {@link ExtensionContext#getParent() hierarchy} of contexts.
*
* If the injector provider can be found, it will be reflectively instantiated and cached. Only one instance of any
* given injector provider will be created.
*
* @since 2.24
*/
protected static IInjectorProvider getOrCreateInjectorProvider(ExtensionContext context) {
Optional<InjectWith> injectWithOpt = AnnotationSupport.findAnnotation(context.getRequiredTestClass(), InjectWith.class);
if (injectWithOpt.isPresent()) {
InjectWith injectWith = injectWithOpt.get();
Class<? extends IInjectorProvider> klass = injectWith.value();
IInjectorProvider injectorProvider = injectorProviderClassCache.get(klass);
if (injectorProvider == null) {
try {
injectorProvider = klass.getDeclaredConstructor().newInstance();
injectorProviderClassCache.put(klass, injectorProvider);
} catch (Exception e) {
throwUncheckedException(e);
}
}
return injectorProvider;
} else {
Optional<ExtensionContext> parentContext = context.getParent().filter(p -> p.getTestClass().isPresent());
if (parentContext.isPresent()) {
return getOrCreateInjectorProvider(parentContext.get());
}
}
return null;
}
use of org.eclipse.xtext.testing.InjectWith in project xtext-core by eclipse.
the class InjectorProviders method getOrCreateInjectorProvider.
public static IInjectorProvider getOrCreateInjectorProvider(TestClass testClass) {
InjectWith injectWith = testClass.getJavaClass().getAnnotation(InjectWith.class);
if (injectWith != null) {
Class<? extends IInjectorProvider> klass = injectWith.value();
IInjectorProvider injectorProvider = injectorProviderClassCache.get(klass);
if (injectorProvider == null) {
try {
injectorProvider = klass.getDeclaredConstructor().newInstance();
injectorProviderClassCache.put(klass, injectorProvider);
} catch (Exception e) {
throwUncheckedException(e);
}
}
return injectorProvider;
}
return null;
}
Aggregations