Commits

Anonymous committed 9aeaa67

Added ability to discover registered factory names for a given type

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1287e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits cdddefa

Comments (0)

Files changed (2)

File src/java/com/opensymphony/xwork2/inject/Container.java

 
 package com.opensymphony.xwork2.inject;
 
+import java.util.Set;
+
 /**
  * Injects dependencies into constructors, methods and fields annotated with
  * {@link Inject}. Immutable.
    * DEFAULT_NAME)}.
    */
   <T> T getInstance(Class<T> type);
+  
+  /**
+   * Gets a set of all registered names for the given type
+   * @param type The instance type
+   * @return A set of registered names
+   */
+  Set<String> getInstanceNames(Class<?> type);
 
   /**
    * Sets the scope strategy for the current thread.

File src/java/com/opensymphony/xwork2/inject/ContainerImpl.java

 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
 
 /**
  * Default {@link Container} implementation.
 class ContainerImpl implements Container {
 
   final Map<Key<?>, InternalFactory<?>> factories;
+  final Map<Class<?>,Set<String>> factoryNamesByType;
 
   ContainerImpl(Map<Key<?>, InternalFactory<?>> factories) {
     this.factories = factories;
+    Map<Class<?>,Set<String>> map = new HashMap<Class<?>,Set<String>>();
+    for (Key<?> key : factories.keySet()) {
+      Set<String> names = map.get(key.getType());
+      if (names == null) {
+        names = new HashSet<String>();
+        map.put(key.getType(), names);
+      }
+      names.add(key.getName());
+    }
+    
+    for (Entry<Class<?>,Set<String>> entry : map.entrySet()) {
+      entry.setValue(Collections.unmodifiableSet(entry.getValue()));
+    }
+    
+    this.factoryNamesByType = Collections.unmodifiableMap(map);
   }
 
   @SuppressWarnings("unchecked")
       }
     });
   }
+  
+  public Set<String> getInstanceNames(final Class<?> type) {
+    return factoryNamesByType.get(type);
+  }
 
   ThreadLocal<InternalContext[]> localContext =
       new ThreadLocal<InternalContext[]>() {