You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by jk...@apache.org on 2007/04/30 20:57:22 UTC

svn commit: r533824 - in /tapestry/tapestry4/trunk/tapestry-framework/src: java/org/apache/tapestry/enhance/GenericsMethodSignatureImpl.java java/org/apache/tapestry/enhance/InjectObjectWorker.java test/org/apache/tapestry/enhance/MethodSignatureTest.java

Author: jkuhnert
Date: Mon Apr 30 11:57:21 2007
New Revision: 533824

URL: http://svn.apache.org/viewvc?view=rev&rev=533824
Log:
Fixes TAPESTRY-1410 (again). Wasn't handling nested generics parameter types when doing class resolution. Now handles infinite levels of recursive type specifiers.

Modified:
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/GenericsMethodSignatureImpl.java
    tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/InjectObjectWorker.java
    tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/MethodSignatureTest.java

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/GenericsMethodSignatureImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/GenericsMethodSignatureImpl.java?view=diff&rev=533824&r1=533823&r2=533824
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/GenericsMethodSignatureImpl.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/GenericsMethodSignatureImpl.java Mon Apr 30 11:57:21 2007
@@ -50,20 +50,61 @@
             TypeVariable tvar = (TypeVariable)ret;
             ParameterizedType param = (ParameterizedType)type.getGenericSuperclass();
 
+            Class resolvedType = resolveType(param, tvar);
+            if (resolvedType != null)
+                return resolvedType;
+            /*
             if (param.getActualTypeArguments().length > 0) {
 
                 for (int i = 0; i < tvar.getBounds().length; i++) {
-
+                    System.out.println("bounds value class is: " + tvar.getBounds()[i].getClass());
+                    Type t = tvar.getBounds()[i];
+                    if (ParameterizedType.class.isInstance(t)) {
+                        ParameterizedType pt = (ParameterizedType)t;
+
+                        //System.out.println("bounds param type arguments are: " + pt.getActualTypeArguments()[0].getClass() + " and param type arguments are: " + param.getA);
+                    }
+                    
                     Class resolvedType = findType(param.getActualTypeArguments(), (Class)tvar.getBounds()[i]);
                     if (resolvedType != null)
                         return resolvedType;
                 }
-            }
+            }*/
         }
 
         return m.getReturnType();
     }
 
+    static Class resolveType(ParameterizedType param, TypeVariable var)
+    {
+        if (param.getActualTypeArguments().length < 1)
+            return null;
+
+        for (int i=0; i < var.getBounds().length; i++) {
+
+            Type t = var.getBounds()[i];
+            Class resolvedType = null;
+            if (ParameterizedType.class.isInstance(t)) {
+
+                ParameterizedType pparam = (ParameterizedType)t;
+                for (int e=0; e < pparam.getActualTypeArguments().length; e++) {
+                    if (!TypeVariable.class.isInstance(pparam.getActualTypeArguments()[e]))
+                        continue;
+                    
+                    resolvedType = resolveType(pparam, (TypeVariable)pparam.getActualTypeArguments()[e]);
+                }
+            } else {
+
+                resolvedType = findType(param.getActualTypeArguments(), (Class)t);
+            }
+            
+            if (resolvedType != null)
+                return resolvedType;
+        }
+
+        return null;
+    }
+
     static Class findType(Type[] types, Class type)
     {
         for (int i = 0; i < types.length; i++) {
@@ -90,9 +131,14 @@
 
                 if (TypeVariable.class.isInstance(genTypes[i])) {
 
-                    TypeVariable tvar = (TypeVariable)genTypes[i];
+                    Class resolved = resolveType(param, (TypeVariable)genTypes[i]);
+                    System.out.println("Resolved parameter type is : " + resolved);
+
+                    /* TypeVariable tvar = (TypeVariable)genTypes[i];
                     for (int p = 0; p < tvar.getBounds().length; p++) {
 
+                        //System.out.println("Bounds parameter type is: " + tvar.getBounds()[p].getClass());
+
                         Class resolvedType = findType(param.getActualTypeArguments(), (Class)tvar.getBounds()[p]);
                         if (resolvedType != null) {
                             types[i] = resolvedType;
@@ -100,6 +146,10 @@
                         }
 
                         types[i] = m.getParameterTypes()[i];
+                    }*/
+                    if (resolved != null) {
+                        types[i] = resolved;
+                        continue;
                     }
                 }
 

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/InjectObjectWorker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/InjectObjectWorker.java?view=diff&rev=533824&r1=533823&r2=533824
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/InjectObjectWorker.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/enhance/InjectObjectWorker.java Mon Apr 30 11:57:21 2007
@@ -14,8 +14,6 @@
 
 package org.apache.tapestry.enhance;
 
-import java.lang.reflect.Modifier;
-
 import org.apache.hivemind.ApplicationRuntimeException;
 import org.apache.hivemind.Location;
 import org.apache.hivemind.service.MethodSignature;
@@ -23,6 +21,8 @@
 import org.apache.tapestry.services.InjectedValueProvider;
 import org.apache.tapestry.spec.InjectSpecification;
 
+import java.lang.reflect.Modifier;
+
 /**
  * Implementation for injection type "object" (the default). Adds read-only
  * properties to the enhanced class that contain objects injected from HiveMind.
@@ -53,7 +53,8 @@
         Defense.notNull(objectReference, "objectReference");
 
         Class propertyType = op.getPropertyType(propertyName);
-        if (propertyType == null) propertyType = Object.class;
+        if (propertyType == null)
+            propertyType = Object.class;
 
         op.claimReadonlyProperty(propertyName);
 
@@ -63,10 +64,9 @@
             throw new ApplicationRuntimeException(EnhanceMessages
                     .locatedValueIsNull(objectReference), location, null);
         
-        if (!propertyType.isAssignableFrom(injectedValue.getClass()))
-            throw new ApplicationRuntimeException(EnhanceMessages
-                    .incompatibleInjectType(objectReference, injectedValue,
-                            propertyType), location, null);
+        if (!propertyType.isInstance(injectedValue))
+            throw new ApplicationRuntimeException(EnhanceMessages.incompatibleInjectType(objectReference, injectedValue, propertyType),
+                    location, null);
         
         String fieldName = op.addInjectedField("_$" + propertyName,
                 propertyType, injectedValue);

Modified: tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/MethodSignatureTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/MethodSignatureTest.java?view=diff&rev=533824&r1=533823&r2=533824
==============================================================================
--- tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/MethodSignatureTest.java (original)
+++ tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/enhance/MethodSignatureTest.java Mon Apr 30 11:57:21 2007
@@ -113,7 +113,7 @@
 
     public void test_Generic_Method_Hash() {
 
-        Class testClass=MyTest.class;
+        Class testClass = MyTest.class;
 
         Method[] methods = testClass.getMethods();
 
@@ -122,11 +122,17 @@
             MethodSignatureImpl msi = new GenericsMethodSignatureImpl(testClass, method);
             msi.hashCode();
         }
+
+        ClassInspector ins = new GenericsClassInspectorImpl();
+        MethodSignature sig = ins.getPropertyAccessor(MyTest.class, "relativeObject");
+
+        assert sig.getReturnType() != null;
+        assertEquals(sig.getReturnType(), BaseTest.class);
     }
 
     public static abstract class BaseTest<T>{ }
     
-    public static abstract class MyTest<T,E>extends BaseTest<T> {
+    public static abstract class MyTest<T,E extends BaseTest<T>> extends BaseTest<T> {
 
         public abstract E getRelativeObject();
         public abstract void setRelativeObject(E e);