Search in sources :

Example 1 with DefaultRoboModule

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;
    }
}
Also used : ContextScopedRoboInjector(roboguice.inject.ContextScopedRoboInjector) RoboInjector(roboguice.inject.RoboInjector) Injector(com.google.inject.Injector) Module(com.google.inject.Module) DefaultRoboModule(roboguice.config.DefaultRoboModule)

Example 2 with DefaultRoboModule

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);
    }
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) ApplicationInfo(android.content.pm.ApplicationInfo) DefaultRoboModule(roboguice.config.DefaultRoboModule) Module(com.google.inject.Module) DefaultRoboModule(roboguice.config.DefaultRoboModule) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException)

Aggregations

Module (com.google.inject.Module)2 DefaultRoboModule (roboguice.config.DefaultRoboModule)2 ApplicationInfo (android.content.pm.ApplicationInfo)1 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1 Bundle (android.os.Bundle)1 Injector (com.google.inject.Injector)1 ArrayList (java.util.ArrayList)1 ContextScopedRoboInjector (roboguice.inject.ContextScopedRoboInjector)1 RoboInjector (roboguice.inject.RoboInjector)1