use of roboguice.config.DefaultRoboModule in project roboguice by roboguice.
the class RoboGuice method overrideApplicationInjector.
/**
* Shortcut to obtain an injector during tests. It will load all modules declared in manifest, add a {@code DefaultRoboModule},
* and override all bindings defined in test modules. We use default stage by default.
*
* RoboGuice.overrideApplicationInjector( app, new TestModule() );
*
* @see com.google.inject.util.Modules#override(com.google.inject.Module...)
* @see roboguice.RoboGuice#getOrCreateBaseApplicationInjector(android.app.Application, com.google.inject.Stage, com.google.inject.Module...)
* @see roboguice.RoboGuice#newDefaultRoboModule(android.app.Application)
* @see roboguice.RoboGuice#DEFAULT_STAGE
*
* If using this method with test cases, be sure to call {@link roboguice.RoboGuice.Util#reset()} in your test teardown methods
* to avoid polluting our other tests with your custom injector. Don't do this in your real application though.
*/
public static Injector overrideApplicationInjector(final Application application, Module... overrideModules) {
synchronized (RoboGuice.class) {
final List<Module> baseModules = extractModulesFromManifest(application);
final Injector rtrn = Guice.createInjector(DEFAULT_STAGE, Modules.override(baseModules).with(overrideModules));
injectors.put(application, rtrn);
return rtrn;
}
}
use of roboguice.config.DefaultRoboModule in project roboguice by roboguice.
the class RoboGuice method extractModulesFromManifest.
private static List<Module> extractModulesFromManifest(Application application) {
try {
final ArrayList<Module> modules = new ArrayList<Module>();
final ApplicationInfo ai = application.getPackageManager().getApplicationInfo(application.getPackageName(), PackageManager.GET_META_DATA);
final Bundle bundle = ai.metaData;
final String roboguiceModules = bundle != null ? bundle.getString("roboguice.modules") : null;
final DefaultRoboModule defaultRoboModule = newDefaultRoboModule(application);
final String[] moduleNames = roboguiceModules != null ? roboguiceModules.split("[\\s,]") : new String[] {};
modules.add(defaultRoboModule);
for (String name : moduleNames) {
if (Strings.notEmpty(name)) {
final Class<? extends Module> clazz = Class.forName(name).asSubclass(Module.class);
try {
modules.add(clazz.getDeclaredConstructor(Application.class).newInstance(application));
} catch (NoSuchMethodException ignored) {
modules.add(clazz.newInstance());
}
}
}
return modules;
} catch (Exception e) {
throw new RuntimeException("Unable to instantiate your Module. Check your roboguice.modules metadata in your AndroidManifest.xml", e);
}
}
Aggregations