Author: almaw
Date: Sat Nov  3 09:22:24 2007
New Revision: 591645

URL: http://svn.apache.org/viewvc?rev=591645&view=rev
Log:
WICKET-1063 - Support TypeLiteral and Provider injections.

Added:
    
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceTypeStore.java
Modified:
    
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java
    
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceInjectorHolder.java
    
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceProxyTargetLocator.java
    
wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/GuiceInjectorTest.java
    
wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java

Modified: 
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java?rev=591645&r1=591644&r2=591645&view=diff
==============================================================================
--- 
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java
 (original)
+++ 
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceComponentInjector.java
 Sat Nov  3 09:22:24 2007
@@ -20,6 +20,8 @@
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 
 import org.apache.wicket.Application;
 import org.apache.wicket.Component;
@@ -86,11 +88,12 @@
         * [EMAIL PROTECTED] Injector} instance.
         * 
         * @param app
-        * @param modules
+        * @param injector
         */
        public GuiceComponentInjector(Application app, Injector injector)
        {
                app.setMetaData(GuiceInjectorHolder.INJECTOR_KEY, new 
GuiceInjectorHolder(injector));
+               app.setMetaData(GuiceTypeStore.TYPESTORE_KEY, new 
GuiceTypeStore());
        }
 
        public void inject(Object object)
@@ -107,7 +110,8 @@
                                        {
                                                Annotation bindingAnnotation = 
findBindingAnnotation(field.getAnnotations());
                                                Object proxy = 
LazyInitProxyFactory.createProxy(field.getType(),
-                                                               new 
GuiceProxyTargetLocator(field.getType(), bindingAnnotation));
+                                                               new 
GuiceProxyTargetLocator(field.getGenericType(),
+                                                                               
bindingAnnotation));
 
                                                if (!field.isAccessible())
                                                {
@@ -136,22 +140,32 @@
                                {
                                        Annotation[][] paramAnnotations = 
method.getParameterAnnotations();
                                        Class< ? >[] paramTypes = 
method.getParameterTypes();
+                                       Type[] genericParamTypes = 
method.getGenericParameterTypes();
                                        Object[] args = new 
Object[paramTypes.length];
                                        for (int i = 0; i < paramTypes.length; 
i++)
                                        {
+                                               Type paramType;
+                                               if (genericParamTypes[i] 
instanceof ParameterizedType)
+                                               {
+                                                       paramType = 
((ParameterizedType)genericParamTypes[i]).getRawType();
+                                               }
+                                               else
+                                               {
+                                                       paramType = 
paramTypes[i];
+                                               }
                                                try
                                                {
                                                        Annotation 
bindingAnnotation = findBindingAnnotation(paramAnnotations[i]);
                                                        args[i] = 
LazyInitProxyFactory.createProxy(paramTypes[i],
-                                                                       new 
GuiceProxyTargetLocator(paramTypes[i], bindingAnnotation));
+                                                                       new 
GuiceProxyTargetLocator(genericParamTypes[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 class " +
-                                                                               
        object.getClass().getName());
+                                                                               
        "(" + paramType + ") of method " + method.getName() +
+                                                                               
        " of class " + object.getClass().getName());
                                                }
                                        }
                                        try

Modified: 
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceInjectorHolder.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceInjectorHolder.java?rev=591645&r1=591644&r2=591645&view=diff
==============================================================================
--- 
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceInjectorHolder.java
 (original)
+++ 
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceInjectorHolder.java
 Sat Nov  3 09:22:24 2007
@@ -47,7 +47,7 @@
        /**
         * Constructor
         * 
-        * @param context
+        * @param injector
         */
        public GuiceInjectorHolder(Injector injector)
        {

Modified: 
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceProxyTargetLocator.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceProxyTargetLocator.java?rev=591645&r1=591644&r2=591645&view=diff
==============================================================================
--- 
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceProxyTargetLocator.java
 (original)
+++ 
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceProxyTargetLocator.java
 Sat Nov  3 09:22:24 2007
@@ -17,38 +17,52 @@
 package org.apache.wicket.guice;
 
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
 
 import org.apache.wicket.Application;
 import org.apache.wicket.proxy.IProxyTargetLocator;
-import org.apache.wicket.util.lang.Classes;
 
 import com.google.inject.Key;
+import com.google.inject.TypeLiteral;
 
 class GuiceProxyTargetLocator implements IProxyTargetLocator
 {
        private static final long serialVersionUID = 1L;
 
-       private String typeName;
-       private Annotation bindingAnnotation;
+       private final String typeName;
+       private final Annotation bindingAnnotation;
 
-       GuiceProxyTargetLocator(Class< ? > type, Annotation bindingAnnotation)
+       GuiceProxyTargetLocator(Type type, Annotation bindingAnnotation)
        {
-               this.typeName = type.getName();
+               // I'm not too happy about
+               typeName = type.toString();
                this.bindingAnnotation = bindingAnnotation;
+
+               GuiceTypeStore typeStore = 
(GuiceTypeStore)Application.get().getMetaData(
+                               GuiceTypeStore.TYPESTORE_KEY);
+               typeStore.setType(typeName, type);
        }
 
        public Object locateProxyTarget()
        {
-               GuiceInjectorHolder holder = 
(GuiceInjectorHolder)Application.get().getMetaData(
+               final GuiceInjectorHolder holder = 
(GuiceInjectorHolder)Application.get().getMetaData(
                                GuiceInjectorHolder.INJECTOR_KEY);
+
+               final GuiceTypeStore typeStore = 
(GuiceTypeStore)Application.get().getMetaData(
+                               GuiceTypeStore.TYPESTORE_KEY);
+               final Type type = typeStore.getType(typeName);
+
+               // using TypeLiteral to retrieve the key gives us automatic 
support for
+               // Providers and other injectable TypeLiterals
                final Key< ? > key;
+
                if (bindingAnnotation == null)
                {
-                       key = Key.get(Classes.resolveClass(typeName));
+                       key = Key.get(TypeLiteral.get(type));
                }
                else
                {
-                       key = Key.get(Classes.resolveClass(typeName), 
bindingAnnotation);
+                       key = Key.get(TypeLiteral.get(type), bindingAnnotation);
                }
                return holder.getInjector().getInstance(key);
        }

Added: 
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceTypeStore.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceTypeStore.java?rev=591645&view=auto
==============================================================================
--- 
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceTypeStore.java
 (added)
+++ 
wicket/trunk/jdk-1.5/wicket-guice/src/main/java/org/apache/wicket/guice/GuiceTypeStore.java
 Sat Nov  3 09:22:24 2007
@@ -0,0 +1,56 @@
+/*
+ * 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;
+
+import org.apache.wicket.MetaDataKey;
+import org.apache.wicket.IClusterable;
+
+import java.lang.reflect.Type;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * This is a holder, of sorts, for a type store of runtime generified types. 
The reason this exists
+ * is because the generic type information gleaned from fields and methods is 
not Serializable.
+ * Unfortunately, to support all of the features of Guice, we MUST store this 
type information or go
+ * about rewriting large portions of it. It's not terribly nasty and it's just 
holding references,
+ * so it shouldn't be too bad. I hate that it sits in the metadata, but this 
is the only place to
+ * shelter the type information from Serialization.
+ * 
+ * @author jboyens
+ */
+public class GuiceTypeStore implements IClusterable
+{
+       private static final long serialVersionUID = 1L;
+
+       public static MetaDataKey TYPESTORE_KEY = new 
MetaDataKey(GuiceTypeStore.class)
+       {
+               private static final long serialVersionUID = 1L;
+       };
+
+       private final Map<String, Type> typeStore = new HashMap<String, Type>();
+
+       public Type getType(String typeName)
+       {
+               return typeStore.get(typeName);
+       }
+
+       public void setType(String typeName, Type type)
+       {
+               typeStore.put(typeName, type);
+       }
+}

Modified: 
wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/GuiceInjectorTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/GuiceInjectorTest.java?rev=591645&r1=591644&r2=591645&view=diff
==============================================================================
--- 
wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/GuiceInjectorTest.java
 (original)
+++ 
wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/GuiceInjectorTest.java
 Sat Nov  3 09:22:24 2007
@@ -16,6 +16,9 @@
  */
 package org.apache.wicket.guice;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import junit.framework.TestCase;
 
 import org.apache.wicket.Application;
@@ -28,6 +31,8 @@
 
 import com.google.inject.Binder;
 import com.google.inject.Module;
+import com.google.inject.Provider;
+import com.google.inject.TypeLiteral;
 
 public class GuiceInjectorTest extends TestCase
 {
@@ -47,6 +52,7 @@
                                return null;
                        }
 
+                       @Override
                        protected ISessionStore newSessionStore()
                        {
                                // Don't use a filestore, or we spawn lots of 
threads, which
@@ -72,6 +78,19 @@
                                                        TestServiceRed.class);
                                        
binder.bind(ITestService.class).annotatedWith(Blue.class).to(
                                                        TestServiceBlue.class);
+                                       binder.bind(new TypeLiteral<Map<String, 
String>>()
+                                       {
+                                       }).toProvider(new Provider<Map<String, 
String>>()
+                                       {
+                                               public Map<String, String> get()
+                                               {
+                                                       Map<String, String> 
strings = new HashMap<String, String>();
+
+                                                       
strings.put(ITestService.RESULT, ITestService.RESULT);
+
+                                                       return strings;
+                                               }
+                                       });
                                }
 
                        });
@@ -101,5 +120,13 @@
                assertEquals(ITestService.RESULT, 
component.getInjectedMethod().getString());
                assertEquals(ITestService.RESULT_BLUE, 
component.getInjectedMethodBlue().getString());
                assertEquals(ITestService.RESULT_RED, 
component.getInjectedMethodRed().getString());
+
+               assertEquals(ITestService.RESULT, 
component.getInjectedFieldProvider().get().getString());
+               assertEquals(ITestService.RESULT, 
component.getInjectedMethodProvider().get().getString());
+
+               assertEquals(ITestService.RESULT, 
component.getInjectedTypeLiteralField().get(
+                               ITestService.RESULT));
+               assertEquals(ITestService.RESULT, 
component.getInjectedTypeLiteralMethod().get(
+                               ITestService.RESULT));
        }
 }

Modified: 
wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java?rev=591645&r1=591644&r2=591645&view=diff
==============================================================================
--- 
wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java
 (original)
+++ 
wicket/trunk/jdk-1.5/wicket-guice/src/test/java/org/apache/wicket/guice/TestComponent.java
 Sat Nov  3 09:22:24 2007
@@ -16,10 +16,13 @@
  */
 package org.apache.wicket.guice;
 
+import java.util.Map;
+
 import org.apache.wicket.Component;
 import org.apache.wicket.markup.MarkupStream;
 
 import com.google.inject.Inject;
+import com.google.inject.Provider;
 
 public class TestComponent extends Component
 {
@@ -37,41 +40,30 @@
        private ITestService injectedFieldBlue;
 
        @Inject
-       public void injectService(ITestService service)
-       {
-               injectedMethod = service;
-       }
+       private Provider<ITestService> injectedFieldProvider;
 
        @Inject
-       public void injectServiceRed(@Red
-       ITestService service)
-       {
-               injectedMethodRed = service;
-       }
-
-       @Inject
-       public void injectServiceBlue(@Blue
-       ITestService service)
-       {
-               injectedMethodBlue = service;
-       }
+       private Map<String, String> injectedTypeLiteralField;
 
        private ITestService injectedMethod, injectedMethodRed, 
injectedMethodBlue;
 
+       private Provider<ITestService> injectedMethodProvider;
+
+       private Map<String, String> injectedTypeLiteralMethod;
+
        public TestComponent(String id)
        {
                super(id);
        }
 
-       @Override
-       protected void onRender(MarkupStream markupStream)
+       public ITestService getInjectedField()
        {
-               // Do nothing.
+               return injectedField;
        }
 
-       public ITestService getInjectedField()
+       public ITestService getInjectedFieldBlue()
        {
-               return injectedField;
+               return injectedFieldBlue;
        }
 
        public ITestService getInjectedFieldRed()
@@ -79,14 +71,14 @@
                return injectedFieldRed;
        }
 
-       public ITestService getInjectedFieldBlue()
+       public ITestService getInjectedMethod()
        {
-               return injectedFieldBlue;
+               return injectedMethod;
        }
 
-       public ITestService getInjectedMethod()
+       public ITestService getInjectedMethodBlue()
        {
-               return injectedMethod;
+               return injectedMethodBlue;
        }
 
        public ITestService getInjectedMethodRed()
@@ -94,8 +86,61 @@
                return injectedMethodRed;
        }
 
-       public ITestService getInjectedMethodBlue()
+       public Provider<ITestService> getInjectedFieldProvider()
        {
-               return injectedMethodBlue;
+               return injectedFieldProvider;
+       }
+
+       public Map<String, String> getInjectedTypeLiteralField()
+       {
+               return injectedTypeLiteralField;
+       }
+
+       public Provider<ITestService> getInjectedMethodProvider()
+       {
+               return injectedMethodProvider;
+       }
+
+       public Map<String, String> getInjectedTypeLiteralMethod()
+       {
+               return injectedTypeLiteralMethod;
+       }
+
+       @Inject
+       public void injectProvider(Provider<ITestService> provider)
+       {
+               injectedMethodProvider = provider;
+       }
+
+       @Inject
+       public void injectService(ITestService service)
+       {
+               injectedMethod = service;
+       }
+
+       @Inject
+       public void injectServiceBlue(@Blue
+       ITestService service)
+       {
+               injectedMethodBlue = service;
+       }
+
+       @Inject
+       public void injectServiceRed(@Red
+       ITestService service)
+       {
+               injectedMethodRed = service;
+       }
+
+       @Inject
+       public void injectTypeLiteral(Map<String, String> map)
+       {
+               injectedTypeLiteralMethod = map;
+       }
+
+       @Override
+       protected void onRender(MarkupStream markupStream)
+       {
+               // Do nothing.
        }
 }


Reply via email to