You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by gn...@apache.org on 2015/07/10 18:09:24 UTC

svn commit: r1690283 - /aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java

Author: gnodet
Date: Fri Jul 10 16:09:24 2015
New Revision: 1690283

URL: http://svn.apache.org/r1690283
Log:
[ARIES-1288] ClassCastException with proxy set in bean of generic class

Modified:
    aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java

Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java?rev=1690283&r1=1690282&r2=1690283&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java (original)
+++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java Fri Jul 10 16:09:24 2015
@@ -24,7 +24,9 @@ import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
 import java.security.AccessControlContext;
 import java.security.AccessController;
 import java.security.PrivilegedActionException;
@@ -625,14 +627,57 @@ public class ReflectionUtils {
             Method setterMethod = findSetter(container, value);
 
             if (setterMethod != null) {
-                setterMethod.invoke(instance, convert(value, setterMethod.getGenericParameterTypes()[0]));
+                setterMethod.invoke(instance, convert(value, resolveParameterType(instance.getClass(), setterMethod)));
             } else {
                 throw new ComponentDefinitionException(
                         "No converter available to convert value "+value+" into a form applicable for the " + 
                         "setters of property "+getName());
             }
         }
-        
+
+        private Type resolveParameterType(Class<?> impl, Method setterMethod) {
+            Type type = setterMethod.getGenericParameterTypes()[0];
+            Class<?> declaringClass = setterMethod.getDeclaringClass();
+            TypeVariable<?>[] declaredVariables = declaringClass.getTypeParameters();
+
+            if (TypeVariable.class.isInstance(type)) {
+                // e.g.: "T extends Serializable"
+                TypeVariable variable = TypeVariable.class.cast(type);
+
+                int index = 0;
+                for (; index < declaredVariables.length; index++) {
+                    // find the class declaration index...
+                    if (variable == declaredVariables[index]) {
+                        break;
+                    }
+                }
+
+                if (index >= declaredVariables.length) {
+                    // not found - now what...
+                    return type;
+                }
+
+                // navigate from the implementation type up to the declaring super
+                // class to find the real generic type...
+                Class<?> c = impl;
+                while (c != null && c != declaringClass) {
+                    Type sup = c.getGenericSuperclass();
+                    if (sup != null && ParameterizedType.class.isInstance(sup)) {
+                        ParameterizedType pt = ParameterizedType.class.cast(sup);
+                        if (declaringClass == pt.getRawType()) {
+                            Type t = pt.getActualTypeArguments()[index];
+                            return t;
+                        }
+                    }
+                    c = c.getSuperclass();
+                }
+                return type;
+            } else {
+                // not a generic type...
+                return type;
+            }
+        }
+
         private Method findSetter(ExtendedBlueprintContainer container, Object value) throws Exception {
             Class<?> valueType = (value == null) ? null : value.getClass();