Repository: wicket
Updated Branches:
  refs/heads/master 90d137a03 -> 95d265769


WICKET-5808 SpringBean, support generic beans

Improved code to support also Map and Set fields.

Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/95d26576
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/95d26576
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/95d26576

Branch: refs/heads/master
Commit: 95d265769a2f064437528070d28919c1f24ec928
Parents: 90d137a
Author: Andrea Del Bene <[email protected]>
Authored: Fri Jan 23 13:54:21 2015 +0100
Committer: Andrea Del Bene <[email protected]>
Committed: Fri Jan 23 13:55:49 2015 +0100

----------------------------------------------------------------------
 .../wicket/spring/FieldBeansCollector.java      | 110 +++++++++++++++++++
 .../apache/wicket/spring/SpringBeanLocator.java |  33 +++---
 .../annot/AnnotProxyFieldValueFactory.java      |   2 +-
 .../wicket/spring/FieldBeansCollectorTest.java  |  56 ++++++++++
 4 files changed, 184 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/95d26576/wicket-spring/src/main/java/org/apache/wicket/spring/FieldBeansCollector.java
----------------------------------------------------------------------
diff --git 
a/wicket-spring/src/main/java/org/apache/wicket/spring/FieldBeansCollector.java 
b/wicket-spring/src/main/java/org/apache/wicket/spring/FieldBeansCollector.java
new file mode 100644
index 0000000..5ceb8b3
--- /dev/null
+++ 
b/wicket-spring/src/main/java/org/apache/wicket/spring/FieldBeansCollector.java
@@ -0,0 +1,110 @@
+/*
+ * 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.spring;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.springframework.core.ResolvableType;
+
+public class FieldBeansCollector
+{
+       private final FieldType fieldType;
+       
+       private final Map<Object, Object> beansToInjectMap;
+       
+       private final Collection<Object> beansToInjectColl;
+       
+       public enum FieldType
+       {
+               LIST, SET, MAP, NONE
+       }
+
+       public FieldBeansCollector(final ResolvableType fieldResolvableType)
+       {
+               Class<?> clazz = fieldResolvableType.resolve();
+               
+               // The required code starts here which replaces
+               if (clazz == Map.class)
+               {
+                       fieldType = FieldType.MAP;
+                       // the getGeneric has to be called with 1 because the 
map contains the typified
+                       // information in the value generic
+                       beansToInjectColl = null;
+                       beansToInjectMap = new HashMap<>();
+               }
+               else if (clazz == Set.class)
+               {
+                       fieldType = FieldType.SET;
+                       beansToInjectColl = new HashSet<>();
+                       beansToInjectMap = null;
+               }
+               else if (clazz == List.class)
+               {
+                       fieldType = FieldType.LIST;
+                       beansToInjectColl = new ArrayList<>();
+                       beansToInjectMap = null;
+               }
+               else
+               {
+                       fieldType = FieldType.NONE;
+                       beansToInjectColl = null;
+                       beansToInjectMap = null;
+               }
+       }
+       
+       public Object getBeansToInject()
+       {
+               if (beansToInjectMap != null && beansToInjectMap.size() > 0)
+               {
+                       return beansToInjectMap;
+               }
+               
+               if (beansToInjectColl != null && beansToInjectColl.size() > 0)
+               {
+                       return beansToInjectColl;
+               }
+               
+               return null;
+       }
+       
+       public void addBean(String beanName, Object bean)
+       {
+               switch (fieldType)
+               {
+                       case LIST : 
+                       case SET :
+                               beansToInjectColl.add(bean);
+                               break;
+                       case MAP :
+                               beansToInjectMap.put(beanName, bean);
+                               break;
+                       default :
+                               break;
+               }
+       }
+       
+       public FieldType getFieldType()
+       {
+               return fieldType;
+       }
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/95d26576/wicket-spring/src/main/java/org/apache/wicket/spring/SpringBeanLocator.java
----------------------------------------------------------------------
diff --git 
a/wicket-spring/src/main/java/org/apache/wicket/spring/SpringBeanLocator.java 
b/wicket-spring/src/main/java/org/apache/wicket/spring/SpringBeanLocator.java
index 2626b14..49ba4fd 100644
--- 
a/wicket-spring/src/main/java/org/apache/wicket/spring/SpringBeanLocator.java
+++ 
b/wicket-spring/src/main/java/org/apache/wicket/spring/SpringBeanLocator.java
@@ -67,7 +67,7 @@ public class SpringBeanLocator implements IProxyTargetLocator
        /**
         * If the field to inject is a list this is the resolvable type of its 
elements
         */
-       private ResolvableType fieldCollectionResolvableType;
+       private ResolvableType fieldElementsResolvableType;
        
        private String fieldName;
        
@@ -129,7 +129,7 @@ public class SpringBeanLocator implements 
IProxyTargetLocator
                {
                        fieldName = beanField.getName();
                        fieldResolvableType = 
ResolvableType.forField(beanField);
-                       fieldCollectionResolvableType = 
fieldResolvableType.getGeneric();
+                       fieldElementsResolvableType = 
fieldResolvableType.getGeneric();
                }
        }
 
@@ -141,7 +141,8 @@ public class SpringBeanLocator implements 
IProxyTargetLocator
        {
                if (singletonCache == null)
                {
-                       singletonCache = 
getSpringContext().isSingleton(getBeanName());
+                       singletonCache = getBeanName() != null && 
+                               getSpringContext().isSingleton(getBeanName());
                }
                return singletonCache;
        }
@@ -235,9 +236,8 @@ public class SpringBeanLocator implements 
IProxyTargetLocator
                        }
 
                        // If the given class is a list try to get the generic 
of the list
-                       final boolean isList = clazz == List.class;
-                       Class<?> lookupClass = isList ? 
-                               fieldResolvableType.getGeneric(0).resolve() : 
clazz;
+                       Class<?> lookupClass = fieldElementsResolvableType != 
null ? 
+                               fieldElementsResolvableType.resolve() : clazz;
 
                        // Else the lookup is done via Generic
                        List<String> names = loadBeanNames(ctx, lookupClass);
@@ -311,8 +311,8 @@ public class SpringBeanLocator implements 
IProxyTargetLocator
         */
        private Object getBeansByName(ApplicationContext ctx, List<String> 
names)
        {
-               List<Object> beansAsList = new ArrayList<>();
-
+               FieldBeansCollector beansCollector = new 
FieldBeansCollector(fieldResolvableType);
+               
                for (String beanName : names)
                {
                        RootBeanDefinition beanDef = getBeanDefinition(ctx, 
beanName);
@@ -343,22 +343,23 @@ public class SpringBeanLocator implements 
IProxyTargetLocator
                        }
 
                        boolean exactMatch = 
fieldResolvableType.isAssignableFrom(candidateResolvableType);
-                       boolean elementMatch = fieldCollectionResolvableType != 
null ? 
-                               
fieldCollectionResolvableType.isAssignableFrom(candidateResolvableType) : false;
-
-                       if (exactMatch || elementMatch)
-                       {
-                               beansAsList.add(ctx.getBean(beanName));
-                       }
+                       boolean elementMatch = fieldElementsResolvableType != 
null ? 
+                               
fieldElementsResolvableType.isAssignableFrom(candidateResolvableType) : false;
 
                        if (exactMatch)
                        {
                                this.beanName = beanName;
                                return ctx.getBean(beanName);
                        }
+                       
+                       if (elementMatch)
+                       {
+                               beansCollector.addBean(beanName, 
ctx.getBean(beanName));
+                       }
+
                }
                
-               return beansAsList.size() > 0 ? beansAsList : null;
+               return beansCollector.getBeansToInject();
        }
 
        @Override

http://git-wip-us.apache.org/repos/asf/wicket/blob/95d26576/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java
----------------------------------------------------------------------
diff --git 
a/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java
 
b/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java
index 82f05bf..b508ebb 100644
--- 
a/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java
+++ 
b/wicket-spring/src/main/java/org/apache/wicket/spring/injection/annot/AnnotProxyFieldValueFactory.java
@@ -165,7 +165,7 @@ public class AnnotProxyFieldValueFactory implements 
IFieldValueFactory
                        }
 
                        // only put the proxy into the cache if the bean is a 
singleton
-                       if (field.getType() == List.class || 
locator.isSingletonBean())
+                       if (locator.isSingletonBean())
                        {
                                Object tmpTarget = cache.putIfAbsent(locator, 
target);
                                if (tmpTarget != null)

http://git-wip-us.apache.org/repos/asf/wicket/blob/95d26576/wicket-spring/src/test/java/org/apache/wicket/spring/FieldBeansCollectorTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-spring/src/test/java/org/apache/wicket/spring/FieldBeansCollectorTest.java
 
b/wicket-spring/src/test/java/org/apache/wicket/spring/FieldBeansCollectorTest.java
new file mode 100644
index 0000000..4a20d44
--- /dev/null
+++ 
b/wicket-spring/src/test/java/org/apache/wicket/spring/FieldBeansCollectorTest.java
@@ -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.spring;
+
+import static org.junit.Assert.assertTrue;
+
+import java.lang.reflect.Field;
+import java.util.List;
+
+import org.apache.wicket.spring.FieldBeansCollector.FieldType;
+import org.junit.Test;
+import org.springframework.core.ResolvableType;
+
+
+public class FieldBeansCollectorTest
+{
+       public List<List<String>> listOfStringList;
+       
+       public List genericList;
+       
+       @Test
+       public void resolveGenericList() throws Exception
+       {
+               Field field = getClass().getField("listOfStringList");
+               ResolvableType resolvableType = ResolvableType.forField(field);
+               
+               FieldBeansCollector fieldBeansCollector = new 
FieldBeansCollector(resolvableType);
+               
+               assertTrue(fieldBeansCollector.getFieldType() == 
FieldType.LIST);               
+       }
+       
+       @Test
+       public void resolveRowList() throws Exception
+       {
+               Field field = getClass().getField("genericList");
+               
+               ResolvableType resolvableType = ResolvableType.forField(field);
+               FieldBeansCollector fieldBeansCollector = new 
FieldBeansCollector(resolvableType);
+               
+               assertTrue(fieldBeansCollector.getFieldType() == 
FieldType.LIST);
+       }
+}
\ No newline at end of file

Reply via email to