You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by al...@apache.org on 2007/06/20 17:02:07 UTC

svn commit: r549124 - in /incubator/wicket/trunk/jdk-1.5/wicket-guice/src: main/java/org/apache/wicket/guice/ test/java/org/apache/wicket/guice/

Author: almaw
Date: Wed Jun 20 08:02:05 2007
New Revision: 549124

URL: http://svn.apache.org/viewvc?view=rev&rev=549124
Log:
Support method injection and Annotation Keys.

Added:
    incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/Blue.java   (with props)
    incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/Red.java   (with props)
    incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestServiceBlue.java   (with props)
    incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestServiceRed.java   (with props)
Modified:
    incubator/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java
    incubator/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceProxyTargetLocator.java
    incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/GuiceInjectorTest.java
    incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/ITestService.java
    incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java
    incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestService.java

Modified: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java?view=diff&rev=549124&r1=549123&r2=549124
==============================================================================
--- incubator/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java (original)
+++ incubator/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java Wed Jun 20 08:02:05 2007
@@ -16,7 +16,10 @@
  */
 package org.apache.wicket.guice;
 
+import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 
 import org.apache.wicket.Application;
 import org.apache.wicket.Component;
@@ -24,6 +27,7 @@
 import org.apache.wicket.application.IComponentInstantiationListener;
 import org.apache.wicket.proxy.LazyInitProxyFactory;
 
+import com.google.inject.BindingAnnotation;
 import com.google.inject.Guice;
 import com.google.inject.ImplementedBy;
 import com.google.inject.Inject;
@@ -96,22 +100,95 @@
 			{
 				if (field.getAnnotation(Inject.class) != null)
 				{
-					Object proxy = LazyInitProxyFactory.createProxy(field.getType(), new GuiceProxyTargetLocator(field.getType()));
-					try {
+					try
+					{
+						Annotation bindingAnnotation = findBindingAnnotation(field.getAnnotations());
+						Object proxy = LazyInitProxyFactory.createProxy(field.getType(), new GuiceProxyTargetLocator(field.getType(), bindingAnnotation));
+
 						if (!field.isAccessible())
 						{
 							field.setAccessible(true);
 						}
 						field.set(component, proxy);
 					}
-					catch (IllegalAccessException e) {
+					catch (IllegalAccessException e)
+					{
 						throw new WicketRuntimeException("Error Guice-injecting field " + field.getName() + " in " + component, e);
 					}
+					catch (MoreThanOneBindingException e)
+					{
+						throw new RuntimeException(
+								"Can't have more than one BindingAnnotation on field "
+										+ field.getName() + " of component class "
+										+ component.getClass().getName());
+					}
+				}
+			}
+			Method[] currentMethods = current.getDeclaredMethods();
+			for (final Method method : currentMethods)
+			{
+				if (method.getAnnotation(Inject.class) != null)
+				{
+					Annotation[][] paramAnnotations = method.getParameterAnnotations();
+					Class<?>[] paramTypes = method.getParameterTypes();
+					Object[] args = new Object[paramTypes.length];
+					for (int i = 0; i < paramTypes.length; i++)
+					{
+						try
+						{
+							Annotation bindingAnnotation = findBindingAnnotation(paramAnnotations[i]);
+							args[i] = LazyInitProxyFactory.createProxy(paramTypes[i], new GuiceProxyTargetLocator(paramTypes[i], bindingAnnotation));
+						}
+						catch (MoreThanOneBindingException e)
+						{
+							throw new RuntimeException(
+									"Can't have more than one BindingAnnotation on parameter "
+											+ i + "(" + paramTypes[i].getSimpleName() + ") of method " + method.getName()
+											+ " of component class "
+											+ component.getClass().getName());
+						}
+					}
+					try
+					{
+						method.invoke(component, args);
+					}
+					catch (IllegalAccessException e)
+					{
+						throw new WicketRuntimeException(e);
+					}
+					catch (InvocationTargetException e)
+					{
+						throw new WicketRuntimeException(e);
+					}
 				}
 			}
 			current = current.getSuperclass();
 		}
 		// use null check just in case Component is in a different classloader.
 		while (current != null && current != Component.class);
+	}
+	
+	private Annotation findBindingAnnotation(Annotation[] annotations) throws MoreThanOneBindingException
+	{
+		Annotation bindingAnnotation = null;
+		
+		// Work out if we have a BindingAnnotation on this parameter.
+		for (int i = 0; i < annotations.length; i++)
+		{
+			if (annotations[i].annotationType().getAnnotation(BindingAnnotation.class) != null)
+			{
+				if (bindingAnnotation != null)
+				{
+					throw new MoreThanOneBindingException();
+				}
+				bindingAnnotation = annotations[i];
+			}
+		}
+		return bindingAnnotation;
+	}
+	
+	private static class MoreThanOneBindingException extends Exception
+	{
+		private static final long serialVersionUID = 1L;
 	}
 }

Modified: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceProxyTargetLocator.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceProxyTargetLocator.java?view=diff&rev=549124&r1=549123&r2=549124
==============================================================================
--- incubator/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceProxyTargetLocator.java (original)
+++ incubator/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceProxyTargetLocator.java Wed Jun 20 08:02:05 2007
@@ -16,24 +16,39 @@
  */
 package org.apache.wicket.guice;
 
+import java.lang.annotation.Annotation;
+
 import org.apache.wicket.Application;
 import org.apache.wicket.proxy.IProxyTargetLocator;
 import org.apache.wicket.util.lang.Classes;
 
+import com.google.inject.Key;
+
 class GuiceProxyTargetLocator implements IProxyTargetLocator
 {
 	private static final long serialVersionUID = 1L;
 
 	private String typeName;
+	private Annotation bindingAnnotation;
 	
-	GuiceProxyTargetLocator(Class<?> type)
+	GuiceProxyTargetLocator(Class<?> type, Annotation bindingAnnotation)
 	{
 		this.typeName = type.getName();
+		this.bindingAnnotation = bindingAnnotation;
 	}
 	
 	public Object locateProxyTarget()
 	{
 		GuiceInjectorHolder holder = (GuiceInjectorHolder)Application.get().getMetaData(GuiceInjectorHolder.INJECTOR_KEY);
-		return holder.getInjector().getInstance(Classes.resolveClass(typeName));
+		final Key<?> key;
+		if (bindingAnnotation == null)
+		{
+			key = Key.get(Classes.resolveClass(typeName));
+		}
+		else
+		{
+			key = Key.get(Classes.resolveClass(typeName), bindingAnnotation);
+		}
+		return holder.getInjector().getInstance(key);
 	}
 }

Added: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/Blue.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/Blue.java?view=auto&rev=549124
==============================================================================
--- incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/Blue.java (added)
+++ incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/Blue.java Wed Jun 20 08:02:05 2007
@@ -0,0 +1,19 @@
+// $Id: $
+package org.apache.wicket.guice;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import com.google.inject.BindingAnnotation;
+
+/**
+ * Indicates we want the blue version of a binding.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.FIELD, ElementType.PARAMETER})
+@BindingAnnotation
+public @interface Blue {
+	// No values.
+}
\ No newline at end of file

Propchange: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/Blue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/GuiceInjectorTest.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/GuiceInjectorTest.java?view=diff&rev=549124&r1=549123&r2=549124
==============================================================================
--- incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/GuiceInjectorTest.java (original)
+++ incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/GuiceInjectorTest.java Wed Jun 20 08:02:05 2007
@@ -31,50 +31,65 @@
 {
 	public void testInjectionAndSerialization()
 	{
-		MockWebApplication mockApp = new MockWebApplication(new WebApplication() {
+		MockWebApplication mockApp = new MockWebApplication(new WebApplication()
+		{
 			@Override
 			protected void outputDevelopmentModeWarning()
 			{
 				// Do nothing.
 			}
+
 			@Override
 			public Class<WebPage> getHomePage()
 			{
 				return null;
 			}
 		}, null);
-		
+
 		// Make a new webapp and injector, and register the injector with the
 		// webapp as a component instantiation listener.
 		Application app = mockApp.getApplication();
-		
+
 		try
 		{
 			Application.set(app);
 			GuiceComponentInjector injector = new GuiceComponentInjector(app, new Module()
 			{
-			
+
 				public void configure(Binder binder)
 				{
 					binder.bind(ITestService.class).to(TestService.class);
+					binder.bind(ITestService.class).annotatedWith(Red.class).to(
+							TestServiceRed.class);
+					binder.bind(ITestService.class).annotatedWith(Blue.class).to(
+							TestServiceBlue.class);
 				}
-			
+
 			});
 			app.addComponentInstantiationListener(injector);
-			
-			// Create a new component. This should be automatically injected with the ITestService implementation.
+
+			// Create a new component, which should be automatically injected, and test to make sure the injection has worked.
 			TestComponent testComponent = new TestComponent("id");
-			
-			// Make sure the service is injected.
-			assertEquals(ITestService.EXPECTED_RESULT, testComponent.getTestService().getString());
-			
+			doChecksForComponent(testComponent);
+
 			// Serialize and deserialize the object, and check it still works.
-			TestComponent copiedComponent = (TestComponent)Objects.cloneObject(testComponent);
-			assertEquals(ITestService.EXPECTED_RESULT, copiedComponent.getTestService().getString());
+			TestComponent clonedComponent = (TestComponent)Objects.cloneObject(testComponent);
+			doChecksForComponent(clonedComponent);
+
 		}
 		finally
 		{
 			Application.unset();
 		}
+	}
+
+	private void doChecksForComponent(TestComponent component)
+	{
+		assertEquals(ITestService.RESULT, component.getInjectedField().getString());
+		assertEquals(ITestService.RESULT_RED, component.getInjectedFieldRed().getString());
+		assertEquals(ITestService.RESULT_BLUE, component.getInjectedFieldBlue().getString());
+		assertEquals(ITestService.RESULT, component.getInjectedMethod().getString());
+		assertEquals(ITestService.RESULT_BLUE, component.getInjectedMethodBlue().getString());
+		assertEquals(ITestService.RESULT_RED, component.getInjectedMethodRed().getString());
 	}
 }

Modified: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/ITestService.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/ITestService.java?view=diff&rev=549124&r1=549123&r2=549124
==============================================================================
--- incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/ITestService.java (original)
+++ incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/ITestService.java Wed Jun 20 08:02:05 2007
@@ -18,6 +18,8 @@
 
 public interface ITestService
 {
-	public static final String EXPECTED_RESULT = "foo";
+	public static final String RESULT = "foo";
+	public static final String RESULT_RED = "red";
+	public static final String RESULT_BLUE = "blue";
 	public String getString();
 }

Added: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/Red.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/Red.java?view=auto&rev=549124
==============================================================================
--- incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/Red.java (added)
+++ incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/Red.java Wed Jun 20 08:02:05 2007
@@ -0,0 +1,19 @@
+// $Id: $
+package org.apache.wicket.guice;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import com.google.inject.BindingAnnotation;
+
+/**
+ * Indicates we want the red version of a binding.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.FIELD, ElementType.PARAMETER})
+@BindingAnnotation
+public @interface Red {
+	// No values.
+}
\ No newline at end of file

Propchange: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/Red.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java?view=diff&rev=549124&r1=549123&r2=549124
==============================================================================
--- incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java (original)
+++ incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java Wed Jun 20 08:02:05 2007
@@ -26,8 +26,36 @@
 	private static final long serialVersionUID = 1L;
 
 	@Inject
-	private ITestService testService;
+	private ITestService injectedField;
 
+	@Inject
+	@Red
+	private ITestService injectedFieldRed;
+	
+	@Inject
+	@Blue
+	private ITestService injectedFieldBlue;
+
+	@Inject
+	public void injectService(ITestService service)
+	{
+		injectedMethod = service;
+	}
+	
+	@Inject
+	public void injectServiceRed(@Red ITestService service)
+	{
+		injectedMethodRed = service;
+	}
+
+	@Inject
+	public void injectServiceBlue(@Blue ITestService service)
+	{
+		injectedMethodBlue = service;
+	}
+	
+	private ITestService injectedMethod, injectedMethodRed, injectedMethodBlue;
+	
 	public TestComponent(String id)
 	{
 		super(id);
@@ -39,8 +67,33 @@
 		// Do nothing.
 	}
 
-	public ITestService getTestService()
+	public ITestService getInjectedField()
+	{
+		return injectedField;
+	}
+
+	public ITestService getInjectedFieldRed()
+	{
+		return injectedFieldRed;
+	}
+
+	public ITestService getInjectedFieldBlue()
+	{
+		return injectedFieldBlue;
+	}
+
+	public ITestService getInjectedMethod()
+	{
+		return injectedMethod;
+	}
+	
+	public ITestService getInjectedMethodRed()
+	{
+		return injectedMethodRed;
+	}
+
+	public ITestService getInjectedMethodBlue()
 	{
-		return testService;
+		return injectedMethodBlue;
 	}
 }

Modified: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestService.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestService.java?view=diff&rev=549124&r1=549123&r2=549124
==============================================================================
--- incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestService.java (original)
+++ incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestService.java Wed Jun 20 08:02:05 2007
@@ -20,6 +20,6 @@
 {
 	public String getString()
 	{
-		return EXPECTED_RESULT;
+		return RESULT;
 	}
 }

Added: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestServiceBlue.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestServiceBlue.java?view=auto&rev=549124
==============================================================================
--- incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestServiceBlue.java (added)
+++ incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestServiceBlue.java Wed Jun 20 08:02:05 2007
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.guice;
+
+public class TestServiceBlue implements ITestService
+{
+	public String getString()
+	{
+		return RESULT_BLUE;
+	}
+}

Propchange: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestServiceBlue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestServiceRed.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestServiceRed.java?view=auto&rev=549124
==============================================================================
--- incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestServiceRed.java (added)
+++ incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestServiceRed.java Wed Jun 20 08:02:05 2007
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.guice;
+
+public class TestServiceRed implements ITestService
+{
+	public String getString()
+	{
+		return RESULT_RED;
+	}
+}

Propchange: incubator/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestServiceRed.java
------------------------------------------------------------------------------
    svn:eol-style = native