RE: [picocontainer-dev] comments on injection

"Putrycz, Erik" <[email protected]>
Newsgroups gmane.comp.java.picocontainer.devel
Message-ID <[email protected]>
Here is another expanded testcase and more doc.

 

Erik.

________________________________

From: Paul Hammant [mailto:[email protected]] 
Sent: June 1, 2007 16:20
To: dev-qxt/[email protected]
Subject: Re: [picocontainer-dev] comments on injection

 

Expand that a little more with testcode will ya Erik?

 

-ph

 

On Jun 1, 2007, at 11:21 AM, Putrycz, Erik wrote:





My other (main) use case is injection...

When I have

@Inject

private MyClass _myClass;

 

If MyClass is not registered then it gets registered automatically in
Pico. With this feature, I hardly have any "initialization" code in my
projects. I just start my project by creating a container and everything
else gets wired automatically. I'm not sure how this could work out with
"getInstance".

 

Erik.

________________________________

From: Paul Hammant [mailto:[email protected]] 
Sent: June 1, 2007 13:27
To: dev-qxt/[email protected]
Subject: Re: [picocontainer-dev] comments on injection

 

What other usecase is there Erik ?

 

For the one you;ve given :-

 

+          @Test

+          public void testDynamicRegistration() {

+                      MutablePicoContainer pico = new
DefaultPicoContainer();

+                      ComponentLocator dynamicReg = new
DynamicRegistrationLocator();

+                      pico.addLocator(dynamicReg);

+                      LinkedList<String> list =
pico.getComponent(LinkedList.class);

+                      assertNotNull(list);

+          }

 

 

I might prefer ..

 

+          @Test

+          public void testDynamicRegistration() {

+                      MutablePicoContainer pico = new
DefaultPicoContainer();

+                      LinkedList<String> list =
pico.addComponent(LinkedList.class).getInstance();  // getInstance() is
new and implicitly on the last component added ~  v easy to implement

+                      assertNotNull(list);

+          }

 

I know you have a 'bigger' use of your way up your sleeve :-)

 

- Paul

 

On Jun 1, 2007, at 9:46 AM, Putrycz, Erik wrote:

 

<Pico2-locator.patch>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email
Pico2-locator.patch (application/octet-stream, 21 KB)
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/pom.xml
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/pom.xml	(revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/pom.xml	(working copy)
@@ -26,6 +26,7 @@
             <plugin>
                 <groupId>com.thoughtworks.paranamer</groupId>
                 <artifactId>paranamer-maven-plugin</artifactId>
+                
                 <configuration>
                     <sourceDirectory>src/test</sourceDirectory>
                     <outputDirectory>target/test-classes</outputDirectory>
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/ComponentLocator.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/ComponentLocator.java	(revision 0)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/ComponentLocator.java	(revision 0)
@@ -0,0 +1,21 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ *****************************************************************************/
+
+package org.picocontainer;
+
+/**
+ * This enables to locate and handle non registered components
+ * @author putrycze
+ *
+ */
+public interface ComponentLocator {
+	
+	public abstract boolean locateComponentType(Class cl, MutablePicoContainer container);
+
+}
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/containers/AbstractDelegatingMutablePicoContainer.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/containers/AbstractDelegatingMutablePicoContainer.java	(revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/containers/AbstractDelegatingMutablePicoContainer.java	(working copy)
@@ -11,6 +11,7 @@
 
 import org.picocontainer.ComponentAdapter;
 import org.picocontainer.ComponentCharacteristic;
+import org.picocontainer.ComponentLocator;
 import org.picocontainer.MutablePicoContainer;
 import org.picocontainer.Parameter;
 import org.picocontainer.PicoContainer;
@@ -28,7 +29,7 @@
  */
 public abstract class AbstractDelegatingMutablePicoContainer implements MutablePicoContainer, Serializable {
 
-    private MutablePicoContainer delegate;
+	private MutablePicoContainer delegate;
 
     public AbstractDelegatingMutablePicoContainer(MutablePicoContainer delegate) {
         this.delegate = delegate;
@@ -137,4 +138,9 @@
     public MutablePicoContainer as(ComponentCharacteristic... characteristics) {
         return delegate.as(characteristics);
     }
+    
+    public void addLocator(ComponentLocator locator) {
+    	delegate.addLocator(locator);
+	}
+    
 }
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/defaults/DefaultPicoContainer.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/defaults/DefaultPicoContainer.java	(revision 3460)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/defaults/DefaultPicoContainer.java	(working copy)
@@ -12,6 +12,7 @@
 import org.picocontainer.ComponentAdapter;
 import org.picocontainer.ComponentCharacteristic;
 import org.picocontainer.ComponentFactory;
+import org.picocontainer.ComponentLocator;
 import org.picocontainer.ComponentMonitor;
 import org.picocontainer.ComponentMonitorStrategy;
 import org.picocontainer.Disposable;
@@ -90,6 +91,8 @@
     private boolean disposed = false;
     // Keeps track of child containers started status
     private Set<Integer> childrenStarted = new HashSet<Integer>();
+    
+    private List<ComponentLocator> locators = new ArrayList<ComponentLocator>();
 
     private LifecycleManager lifecycleManager = new OrderedComponentAdapterLifecycleManager();
     private LifecycleStrategy lifecycleStrategy;
@@ -278,6 +281,7 @@
         if (componentType == null) {
             return Collections.emptyList();
         }
+        
         List<ComponentAdapter<T>> found = new ArrayList<ComponentAdapter<T>>();
         for (ComponentAdapter<?> componentAdapter : getComponentAdapters()) {
             if (componentType.isAssignableFrom(componentAdapter.getComponentImplementation())) {
@@ -285,6 +289,18 @@
                 found.add(typedComponentAdapter);
             }
         }
+        if (found.size() == 0) {
+        // try to process the locator
+        for (ComponentLocator locator:locators) {
+        	if (locator.locateComponentType(componentType, this))
+                for (ComponentAdapter<?> componentAdapter : getComponentAdapters()) {
+                    if (componentType.isAssignableFrom(componentAdapter.getComponentImplementation())) {
+                        ComponentAdapter<T> typedComponentAdapter = typeComponentAdapter(componentAdapter);
+                        found.add(typedComponentAdapter);
+                    }
+                }        		
+        }        
+        }
         return found;
     }
 
@@ -804,5 +820,10 @@
         }
     }
 
+	public void addLocator(ComponentLocator locator) {
+		locators.add(locator);
+		
+	}
+
 
 }
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/defaults/DynamicRegistrationLocator.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/defaults/DynamicRegistrationLocator.java	(revision 0)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/defaults/DynamicRegistrationLocator.java	(revision 0)
@@ -0,0 +1,29 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *****************************************************************************/
+package org.picocontainer.defaults;
+
+import java.lang.reflect.Modifier;
+
+import org.picocontainer.ComponentLocator;
+import org.picocontainer.MutablePicoContainer;
+
+/**
+ * This locator will register any component not found in the container
+ * except JDK classes starting with java.xxx
+ * @author putrycze
+ *
+ */
+public class DynamicRegistrationLocator implements ComponentLocator {
+
+	public boolean locateComponentType(Class cl, MutablePicoContainer container) {
+		if (!cl.isInterface() && !Modifier.isAbstract(cl.getModifiers()) && !cl.getName().startsWith("java."))
+			container.addComponent(cl);
+		return true;
+	}
+
+}
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/Inject.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/Inject.java	(revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/Inject.java	(working copy)
@@ -6,6 +6,6 @@
 import java.lang.annotation.RetentionPolicy;
 
 @Retention(RetentionPolicy.RUNTIME)
-@Target(ElementType.METHOD)
+@Target(value={ElementType.METHOD,ElementType.FIELD})
 public @interface Inject {
 }
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/MutablePicoContainer.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/MutablePicoContainer.java	(revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/java/org/picocontainer/MutablePicoContainer.java	(working copy)
@@ -170,4 +170,11 @@
      */
     MutablePicoContainer as(ComponentCharacteristic... characteristics);
 
+    /**
+     * If a component has not been found, then locators have a chance to provide mecanisms to locate it
+     * 
+     * @param locator
+     */
+	void addLocator(ComponentLocator locator);
+
 }
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/test/org/picocontainer/defaults/LocatorTestCase.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/test/org/picocontainer/defaults/LocatorTestCase.java	(revision 0)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/test/org/picocontainer/defaults/LocatorTestCase.java	(revision 0)
@@ -0,0 +1,52 @@
+package org.picocontainer.defaults;
+
+import java.util.LinkedList;
+
+import org.junit.Test;
+import org.picocontainer.ComponentLocator;
+import org.picocontainer.Inject;
+import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.PicoBuilder;
+import org.picocontainer.adapters.AnnotationInjectionAdapter;
+
+import static org.junit.Assert.*;
+
+public class LocatorTestCase {
+
+	@Test
+	public void testDynamicRegistration() {
+		PicoBuilder builder = new PicoBuilder();
+		MutablePicoContainer pico = builder.build();
+		pico.addLocator(new DynamicRegistrationLocator());
+		TestClass cl = pico.getComponent(TestClass.class);
+		assertNotNull(cl);
+		LinkedList<Object> list = pico.getComponent(LinkedList.class);
+		assertNull(list);
+	}
+	
+	@Test
+	public void testDynamicRegistrationWInjection() {
+		PicoBuilder builder = new PicoBuilder();
+		builder.withAnnotationInjection();
+		MutablePicoContainer pico = builder.build();
+		pico.addLocator(new DynamicRegistrationLocator());
+		TestClass cl = pico.getComponent(TestClass.class);
+		assertNotNull(cl);
+		assertNotNull(cl._testCl2);
+	}
+
+	
+	public static class TestClass {
+		private TestClass2 _testCl2;			
+		
+		@Inject
+		public void setTestClass2(TestClass2 ts2) {
+			_testCl2 = ts2;
+		}
+	}
+	
+	public static class TestClass2 {		
+		
+	}
+	
+}
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/test/org/picocontainer/PicoBuilderTestCase.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/test/org/picocontainer/PicoBuilderTestCase.java	(revision 3460)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/container/src/test/org/picocontainer/PicoBuilderTestCase.java	(working copy)
@@ -57,6 +57,7 @@
                 "    cdiDelegate\n" +
                 "    sdiDelegate\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
                 "PICO",foo);
@@ -70,6 +71,7 @@
                 "    cdiDelegate\n" +
                 "    sdiDelegate\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.StartableLifecycleStrategy\n" +
                 "    componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
                 "  lifecycleStrategy\n" +
@@ -85,6 +87,7 @@
                 "    cdiDelegate\n" +
                 "    sdiDelegate\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.ReflectionLifecycleStrategy\n" +
                 "    methodNames\n" +
                 "      stringstartstring\n" +
@@ -106,6 +109,7 @@
                 "    cdiDelegate\n" +
                 "    sdiDelegate\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.ConsoleComponentMonitor\n" +
                 "    delegate=org.picocontainer.monitors.NullComponentMonitor\n" +
@@ -121,6 +125,7 @@
                 "    cdiDelegate\n" +
                 "    sdiDelegate\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.ConsoleComponentMonitor\n" +
                 "    delegate=org.picocontainer.monitors.NullComponentMonitor\n" +
@@ -146,6 +151,7 @@
                 "      cdiDelegate\n" +
                 "      sdiDelegate\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
                 "PICO",foo);
@@ -160,6 +166,7 @@
                 "      cdiDelegate\n" +
                 "      sdiDelegate\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
                 "PICO",foo);
@@ -174,6 +181,7 @@
                 "      delegate=org.picocontainer.adapters.ImplementationHidingBehaviorFactory\n" +
                 "        delegate=org.picocontainer.adapters.SetterInjectionFactory\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
                 "PICO",foo);
@@ -190,6 +198,7 @@
                 "    cdiDelegate\n" +
                 "    sdiDelegate\n" +
                 "  parent=org.picocontainer.PicoBuilderTestCase_CustomParentcontainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
                 "PICO",foo);
@@ -211,6 +220,7 @@
         assertEquals("PICO\n" +
                 "  componentAdapterFactory=org.picocontainer.adapters.SetterInjectionFactory\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
                 "PICO",foo);
@@ -222,6 +232,7 @@
         assertEquals("PICO\n" +
                 "  componentAdapterFactory=org.picocontainer.adapters.AnnotationInjectionFactory\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
                 "PICO",foo);
@@ -233,6 +244,7 @@
         assertEquals("PICO\n" +
                 "  componentAdapterFactory=org.picocontainer.adapters.ConstructorInjectionFactory\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
                 "PICO",foo);
@@ -245,6 +257,7 @@
                 "  componentAdapterFactory=org.picocontainer.adapters.ImplementationHidingBehaviorFactory\n" +
                 "    delegate=org.picocontainer.adapters.SetterInjectionFactory\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
                 "PICO",foo);
@@ -258,6 +271,7 @@
                 "    delegate=org.picocontainer.adapters.ImplementationHidingBehaviorFactory\n" +
                 "      delegate=org.picocontainer.adapters.SetterInjectionFactory\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
                 "PICO",foo);
@@ -272,6 +286,7 @@
                 "      cdiDelegate\n" +
                 "      sdiDelegate\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
                 "PICO",foo);
@@ -285,6 +300,7 @@
                 "    cdiDelegate\n" +
                 "    sdiDelegate\n" +
                 "  parent=org.picocontainer.containers.EmptyPicoContainer\n" +
+                "  locators\n" +
                 "  lifecycleStrategy=org.picocontainer.lifecycle.NullLifecycleStrategy\n" +
                 "  componentMonitor=org.picocontainer.monitors.NullComponentMonitor\n" +
                 "org.picocontainer.PicoBuilderTestCase_-TestPicoContainer",foo);
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/gems/src/java/org/picocontainer/gems/containers/CommonsLoggingTracingContainerDecorator.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/gems/src/java/org/picocontainer/gems/containers/CommonsLoggingTracingContainerDecorator.java	(revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/gems/src/java/org/picocontainer/gems/containers/CommonsLoggingTracingContainerDecorator.java	(working copy)
@@ -2,6 +2,7 @@
 
 import org.picocontainer.ComponentAdapter;
 import org.picocontainer.ComponentCharacteristic;
+import org.picocontainer.ComponentLocator;
 import org.picocontainer.MutablePicoContainer;
 import org.picocontainer.Parameter;
 import org.picocontainer.PicoContainer;
@@ -462,4 +463,9 @@
     public MutablePicoContainer as(ComponentCharacteristic... characteristics) {
         return delegate.as(characteristics);
     }
+
+
+	public void addLocator(ComponentLocator locator) {
+		delegate.addLocator(locator);
+	}
 }
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/gems/src/java/org/picocontainer/gems/containers/Log4jTracingContainerDecorator.java
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/gems/src/java/org/picocontainer/gems/containers/Log4jTracingContainerDecorator.java	(revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/gems/src/java/org/picocontainer/gems/containers/Log4jTracingContainerDecorator.java	(working copy)
@@ -12,6 +12,7 @@
 
 import org.picocontainer.ComponentAdapter;
 import org.picocontainer.ComponentCharacteristic;
+import org.picocontainer.ComponentLocator;
 import org.picocontainer.MutablePicoContainer;
 import org.picocontainer.Parameter;
 import org.picocontainer.PicoContainer;
@@ -523,4 +524,8 @@
     public MutablePicoContainer as(ComponentCharacteristic... characteristics) {
         return delegate.as(characteristics);
     }
+
+	public void addLocator(ComponentLocator locator) {
+		delegate.addLocator(locator);
+	}
 }
Index: /home/putrycze/workspaces/workspace-cobol/Pico2/pom.xml
===================================================================
--- /home/putrycze/workspaces/workspace-cobol/Pico2/pom.xml	(revision 3459)
+++ /home/putrycze/workspaces/workspace-cobol/Pico2/pom.xml	(working copy)
@@ -141,7 +141,7 @@
             <dependency>
                 <groupId>junit</groupId>
                 <artifactId>junit</artifactId>
-                <version>3.8.1</version>
+                <version>4.0</version>
             </dependency>
             <dependency>
                 <groupId>jmock</groupId>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.