@Override public void run() { try { graph.inject(target, Inject.class); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e); } } });
@Test public void should_throw_ProviderConflictException_on_graph_preparation_by_duplicate_registration() throws Component.MultiParentException, ProviderConflictException, ProvideException { MvcComponent component = new MvcComponent(""); component.register(new Object() { @Provides public UiThreadRunner uiThreadRunner() { return mock(UiThreadRunner.class); } }); boolean caught = false; try { graph.prepareInternalGraph(new Graph(), component); } catch (MvcGraphException e) { if (e.getCause() instanceof ProviderConflictException) { caught = true; } } Assert.assertTrue(caught); }
@Test public void should_throw_IllegalRootComponentException_on_graph_preparation_with_attached_component() throws Component.MultiParentException, ProviderConflictException { MvcComponent parent = new MvcComponent(""); MvcComponent child = new MvcComponent(""); parent.attach(child); boolean caught = false; try { graph.prepareInternalGraph(new Graph(), child); } catch (MvcGraphException e) { if (e.getCause() instanceof Graph.IllegalRootComponentException) { caught = true; } } Assert.assertTrue(caught); }
@Override public void run() { try { graph.release(target, Inject.class); } catch (ProviderMissingException e) { throw new MvcGraphException(e.getMessage(), e); } } });
@Override public void run() { try { graph.use(type, qualifier, Inject.class, consumer); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e); } } });
@Override public void run() { try { graph.setRootComponent(component); } catch (Graph.IllegalRootComponentException e) { throw new MvcGraphException(e.getMessage(), e); } } });
@Override public void run() { try { graph.use(type, Inject.class, consumer); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e); } } });
/** * Reference an injectable object and retain it. Use * {@link #dereference(Object, Class, Annotation)} to dereference it when it's not used * any more. * @param type the type of the object * @param qualifier the qualifier * @return */ public <T> T reference(Class<T> type, Annotation qualifier) throws ProviderMissingException, ProvideException, CircularDependenciesException { if (!uiThreadRunner.isOnUiThread()) { throw new MvcGraphException("Cannot reference an instance from Non-UiThread"); } return graph.reference(type, qualifier, Inject.class); }
throw new MvcGraphException("Failed to register base Mvc dependencies", e); } catch (ProviderConflictException e) { throw new MvcGraphException("Failed to register base Mvc dependencies", e);
Graph prepareInternalGraph(Graph graph, MvcComponent rootComponent) { try { graph.setRootComponent(rootComponent); graph.getRootComponent().register(new Object() { @Provides public UiThreadRunner uiThreadRunner() { return uiThreadRunner; } }); } catch (Graph.IllegalRootComponentException e) { throw new MvcGraphException(e.getMessage(), e); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e); } graph.registerDisposeListener(new Provider.DisposeListener() { @Override public <T> void onDisposed(Provider<T> provider, T instance) { if (instance != null && instance instanceof Bean) { //When the cached instance is still there free and dispose it. Bean bean = (Bean) instance; bean.onDestroy(); logger.trace("---Bean destroyed - '{}'.", provider.type().getSimpleName()); } } }); return graph; }
/** * Same as {@link #use(Class, Annotation, Consumer)} except using un-qualified injectable type. * @param type The type of the injectable instance * @param consumer Consume to use the instance */ public <T> void use(final Class<T> type, final Consumer<T> consumer) { if (uiThreadRunner.isOnUiThread()) { try { graph.use(type, Inject.class, consumer); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e); } } else { uiThreadRunner.post(new Runnable() { @Override public void run() { try { graph.use(type, Inject.class, consumer); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e); } } }); } }
/** * Add {@link Component} to the graph. * * @param component The root {@link Component} of this graph. */ public void setRootComponent(final MvcComponent component) { if (uiThreadRunner.isOnUiThread()) { try { graph.setRootComponent(component); } catch (Graph.IllegalRootComponentException e) { throw new MvcGraphException(e.getMessage(), e); } } else { uiThreadRunner.post(new Runnable() { @Override public void run() { try { graph.setRootComponent(component); } catch (Graph.IllegalRootComponentException e) { throw new MvcGraphException(e.getMessage(), e); } } }); } }
/** * Inject all fields annotated by {@link Inject}. References of controllers will be * incremented. * * @param target The target object whose fields annotated by {@link Inject} will be injected. */ public void inject(final Object target) { if (uiThreadRunner.isOnUiThread()) { try { graph.inject(target, Inject.class); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e); } } else { uiThreadRunner.post(new Runnable() { @Override public void run() { try { graph.inject(target, Inject.class); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e); } } }); } }
/** * Release cached instances held by fields of target object. References of instances of the * instances will be decremented. Once the reference count of a contract type reaches 0, it will * be removed from the instances. * * @param target of which the object fields will be released. */ public void release(final Object target) { if (uiThreadRunner.isOnUiThread()) { try { graph.release(target, Inject.class); } catch (ProviderMissingException e) { throw new MvcGraphException(e.getMessage(), e); } } else { uiThreadRunner.post(new Runnable() { @Override public void run() { try { graph.release(target, Inject.class); } catch (ProviderMissingException e) { throw new MvcGraphException(e.getMessage(), e); } } }); } }
instance = Mvc.graph().reference(type, qualifier); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e);
graph.use(type, qualifier, Inject.class, consumer); } catch (PokeException e) { throw new MvcGraphException(e.getMessage(), e);
@Test public void should_post_wrapped_MvcGraphException_when_run_async_task() { Controller controller = new Controller() { @Override public Class modelType() { return null; } }; graph.inject(controller); boolean exceptionCaught = false; final MvcGraphException exp = new MvcGraphException(""); try { controller.runTask(new Task() { @Override public Object execute(Monitor monitor) throws Exception { throw exp; } }); } catch (Exception e) { exceptionCaught = true; Assert.assertTrue(e.getCause() == exp); } Assert.assertTrue(exceptionCaught); }