mockit.internal.injection.BeanExporter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for automated developer testing.
It contains mocking/faking APIs and a code coverage tool, supporting both JUnit and TestNG.
The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested
in isolation from selected dependencies.
/*
* Copyright (c) 2006 JMockit developers
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.internal.injection;
import javax.annotation.*;
import mockit.internal.injection.field.*;
import mockit.internal.injection.full.*;
public final class BeanExporter
{
@Nonnull private final InjectionState injectionState;
BeanExporter(@Nonnull InjectionState injectionState) { this.injectionState = injectionState; }
@Nullable
public Object getBean(@Nonnull String name) {
InjectionPoint injectionPoint = new InjectionPoint(Object.class, name, true);
Object bean = injectionState.getInstantiatedDependency(null, injectionPoint);
return bean;
}
@Nullable
public T getBean(@Nonnull Class beanType) {
TestedClass testedClass = new TestedClass(beanType, beanType);
String beanName = getBeanNameFromType(beanType);
injectionState.setTypeOfInjectionPoint(beanType);
InjectionProvider injectable = injectionState.findInjectableByTypeAndName(beanName, testedClass);
if (injectable != null) {
@SuppressWarnings("unchecked") T bean = (T) injectionState.getValueToInject(injectable);
return bean;
}
FullInjection injection = new FullInjection(injectionState, beanType, beanName);
Injector injector = new FieldInjection(injectionState, injection);
@SuppressWarnings("unchecked") T bean = (T) injection.createOrReuseInstance(testedClass, injector, null, beanName);
return bean;
}
@Nonnull
private static String getBeanNameFromType(@Nonnull Class> beanType) {
String name = beanType.getSimpleName();
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
}
}