java - How do I subtract a binding using a Guice module override? -


According to my test, if you have something like this:

  module modA = New abstract module () {public zero configuration () (tie (aklas) .to (AIMPL class); Do the bind (C.class). Exhibit (ACImpl.class); Do the E.class. Expedition (EImpl.class); }} Module modB = new abstract module () {public zero configured () {tie (aklas) .to (c. Class); Dump (D.class). Exhibit (DImpl.class); }} Guice.createInjector (Modules.overrides (modA, modB)); // gives the binding for A, C, E and D which is A-> Override for C is I  

But what if you want to remove the bond for ebb? Without breaking the bind into a separate module for E, I can not seem to find a way to do this. Is there any way?

You can do this by using Elements.getElements (modA, modB) To get the element list of items, represent your binding. Repeat through the list and remove the binding, whose keys you want to remove then create a module from the filtered elements using Elements.getModule () . By placing it all together:

  Reduce the public module binding (module module, key & lt ;? & gt; toSubtract) {list & lt; Element & gt; Element = elements Module; (Iterator> Lt; Element & gt; i = elements.iterator (); i.hasNext ();) {element element = i.next (); Remove Boolean = element.acceptVisitor (new DefaultElementVisitor & lt; boolean & gt; () {@Override Public & lt; t & gt; Boolean Tour (Binding & lt; T & gt; Binding) {return binding.getKey ( ) Equal to (toSubtract);} @Override Public Boolean VisitOther (Element Other) {Return False;}}); If (remove) {i.remove (); }} Layers Elements.getModule (element); }  

Comments