Revision: 1413
Author: sberlin
Date: Sat Nov 20 21:12:10 2010
Log: Edited wiki page Multibindings through web user interface.
http://code.google.com/p/google-guice/source/detail?r=1413

Modified:
 /wiki/Multibindings.wiki

=======================================
--- /wiki/Multibindings.wiki    Sun Sep  6 09:58:11 2009
+++ /wiki/Multibindings.wiki    Sat Nov 20 21:12:10 2010
@@ -1,4 +1,4 @@
-#summary Overview of Multibinder and MapBinder extensions
+#summary Overview of Multibinder and !MapBinder extensions
 =Multibindings=
[http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/google/inject/multibindings/Multibinder.html Multibinder] and [http://google-guice.googlecode.com/svn/trunk/latest-javadoc/com/google/inject/multibindings/MapBinder.html MapBinder] are intended for plugin-type architectures, where you've got several modules contributing Servlets, Actions, Filters, Components or even just names.

@@ -102,3 +102,64 @@

 ==Limitations==
When you use !PrivateModules with multibindings, all of the elements must be bound in the same environment. You cannot create collections whose elements span private modules. Otherwise injector creation will fail.
+
+==Inspecting Multibindings or !MapBindings _(new in Guice 3.0)_==
+Sometimes you need to inspect the elements that make up a Multibinder or !MapBinder. For example, you may need a test that strips all elements of a !MapBinder out of a series of modules. You can visit a binding with a [http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/multibindings/MultibindingsTargetVisitor.html MultibindingTargetVisitor] to get details about Multibindings or !MapBindings. After you have an instance of a [http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/multibindings/MapBinderBinding.html MapBinderBinding] or a [http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/multibindings/MultibinderBinding.html MultibinderBinding] you can learn more.
+
+{{{
+   // Find the MapBinderBinding and use it to remove elements within it.
+   Module stripMapBindings(Key<?> mapKey, Module... modules) {
+     MapBinderBinding<?> mapBinder = findMapBinder(mapKey, modules);
+ List<Element> allElements = Lists.newArrayList(Elements.getElements(modules));
+     if (mapBinder != null) {
+       List<Element> mapElements = getMapElements(mapBinder, modules);
+       allElements.removeAll(mapElements);
+     }
+     return Elements.getModule(allElements);
+  }
+
+  // Look through all Elements in the module and, if the key matches,
+ // then use our custom MultibindingsTargetVisitor to get the MapBinderBinding
+  // for the matching binding.
+  MapBinderBinding<?> findMapBinder(Key<?> mapKey, Module... modules) {
+    for(Element element : Elements.getElements(modules)) {
+      MapBinderBinding<?> binding =
+ element.acceptVisitor(new DefaultElementVisitor<MapBinderBinding<?>>() {
+            MapBinderBinding<?> visit(Binding<?> binding) {
+              if(binding.getKey().equals(mapKey)) {
+                return binding.acceptTargetVisitor(new Visitor());
+              }
+              return null;
+            }
+          });
+      if (binding != null) {
+        return binding;
+      }
+    }
+    return null;
+  }
+
+  // Get all elements in the module that are within the MapBinderBinding.
+ List<Element> getMapElements(MapBinderBinding<?> binding, Module... modules) {
+    List<Element> elements = Lists.newArrayList();
+    for(Element element : Elements.getElements(modules)) {
+      if(binding.containsElement(elements)) {
+        elements.add(element);
+      }
+    }
+    return elements;
+  }
+
+  // A visitor that just returns the MapBinderBinding for the binding.
+  class Visitor
+      extends DefaultBindingTargetVisitor<Object, MapBinderBinding<?>>
+      implements MultibindingsTargetVisitor<Object, MapBinderBinding<?>> {
+    MapBinderBinding<?> visit(MapBinderBinding<?> mapBinder) {
+      return mapBinder;
+    }
+
+    MapBinderBinding<?> visit(MultibinderBinding<?> multibinder) {
+      return null;
+    }
+  }
+}}}

--
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice-dev?hl=en.

Reply via email to