wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r1023029 - in /incubator/wink/trunk: wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ wink-server/src/test/java/org/apache/wink/server/internal/providers/entity/
Date Fri, 15 Oct 2010 17:31:59 GMT
Author: bluk
Date: Fri Oct 15 17:31:58 2010
New Revision: 1023029

URL: http://svn.apache.org/viewvc?rev=1023029&view=rev
Log:
Fix issue with the merging of metadata

In some inherited cases, lost the
@*Param annotations so would not
register the method parameters
correctly.

Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java
    incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/providers/entity/GenericInheritanceProviderTest.java

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java?rev=1023029&r1=1023028&r2=1023029&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java
Fri Oct 15 17:31:58 2010
@@ -27,6 +27,8 @@ import java.lang.reflect.Member;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DefaultValue;
@@ -255,7 +257,7 @@ public class ResourceMetadataCollector e
         if (Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) {
             return null;
         }
-        
+
         MethodMetadata metadata = new MethodMetadata(getMetadata());
         metadata.setReflectionMethod(method);
 
@@ -295,12 +297,13 @@ public class ResourceMetadataCollector e
             metadata.setEncoded(true);
             hasAnnotation = true;
         }
-    
+
         // if the method has no annotation at all,
         // then it may override a method in a superclass or interface that has
         // annotations,
         // so try looking at the overridden method annotations
-        // but keep the method params as the super may have declared a generic type param
+        // but keep the method params as the super may have declared a generic
+        // type param
         if (!hasAnnotation) {
 
             Class<?> declaringClass = method.getDeclaringClass();
@@ -311,9 +314,8 @@ public class ResourceMetadataCollector e
                 MethodMetadata createdMetadata = createMethodMetadata(superclass, method);
                 // stop with if the method found
                 if (createdMetadata != null) {
-                    createdMetadata.getFormalParameters().clear();
                     createdMetadata.setReflectionMethod(method);
-                    parseMethodParameters(method, createdMetadata);
+                    mergeFormalParameterMetadata(createdMetadata, method);
                     return createdMetadata;
                 }
             }
@@ -324,9 +326,8 @@ public class ResourceMetadataCollector e
                 MethodMetadata createdMetadata = createMethodMetadata(interfaceClass, method);
                 // stop with the first method found
                 if (createdMetadata != null) {
-                    createdMetadata.getFormalParameters().clear();
                     createdMetadata.setReflectionMethod(method);
-                    parseMethodParameters(method, createdMetadata);
+                    mergeFormalParameterMetadata(createdMetadata, method);
                     return createdMetadata;
                 }
             }
@@ -351,7 +352,7 @@ public class ResourceMetadataCollector e
             }
             return null;
         }
-        
+
         parseMethodParameters(method, metadata);
 
         return metadata;
@@ -368,17 +369,26 @@ public class ResourceMetadataCollector e
         } catch (NoSuchMethodException e) {
             // see if declaringClass's declaredMethod uses generic parameters
             Method[] methods = declaringClass.getMethods();
-            for(Method candidateMethod: methods) {
+            for (Method candidateMethod : methods) {
+                boolean matchFound = true;
                 if (candidateMethod.getName().equals(method.getName())) {
                     // name matches, now check the param signature:
                     if (candidateMethod.getParameterTypes().length == method.getParameterTypes().length)
{
-                        // so far so good.  Now make sure the params are acceptable:
+                        // so far so good. Now make sure the params are
+                        // acceptable:
                         for (int i = 0; i < candidateMethod.getParameterTypes().length;
i++) {
                             Class clazz = candidateMethod.getParameterTypes()[i];
-                            if (clazz.isPrimitive()) {
-                                break;  // signature doesn't match, otherwise it would have
been found in getDeclaredMethod above
+                            if (clazz.isPrimitive() && !clazz.equals(candidateMethod
+                                .getParameterTypes()[i])) {
+                                matchFound = false; // signature doesn't match,
+                                                    // otherwise it
+                                // would have been found in
+                                // getDeclaredMethod above
                             }
-                            if (clazz.isAssignableFrom(method.getParameterTypes()[i])) {
+                            if (!clazz.isAssignableFrom(method.getParameterTypes()[i])) {
+                                matchFound = false;
+                            }
+                            if (matchFound) {
                                 return createMethodMetadata(candidateMethod);
                             }
                         }
@@ -442,7 +452,8 @@ public class ResourceMetadataCollector e
             HttpMethod httpMethodCurr = annotation.annotationType().getAnnotation(HttpMethod.class);
             if (httpMethodCurr != null) {
                 if (httpMethod != null) {
-                    throw new IllegalStateException(Messages.getMessage("multipleHttpMethodAnnotations",
method //$NON-NLS-1$
+                    throw new IllegalStateException(Messages
+                        .getMessage("multipleHttpMethodAnnotations", method //$NON-NLS-1$
                             .getName(), method.getDeclaringClass().getCanonicalName()));
                 }
                 httpMethod = httpMethodCurr;
@@ -476,7 +487,8 @@ public class ResourceMetadataCollector e
                     // we are allowed to have only one entity parameter
                     String methodName =
                         method.getDeclaringClass().getName() + "." + method.getName(); //$NON-NLS-1$
-                    throw new IllegalStateException(Messages.getMessage("resourceMethodMoreThanOneEntityParam",
methodName)); //$NON-NLS-1$
+                    throw new IllegalStateException(Messages
+                        .getMessage("resourceMethodMoreThanOneEntityParam", methodName));
//$NON-NLS-1$
                 }
                 entityParamExists = true;
             }
@@ -484,6 +496,27 @@ public class ResourceMetadataCollector e
         }
     }
 
+    private void mergeFormalParameterMetadata(MethodMetadata metadata, Method method) {
+        logger.trace("mergeFormalParameterMetadata({})", new Object[] {metadata, method});
+        Type[] parameterTypes = method.getGenericParameterTypes();
+        List<Injectable> currentParameters =
+            new ArrayList<Injectable>(metadata.getFormalParameters());
+        metadata.getFormalParameters().clear();
+        int i = 0;
+        for (Injectable injectable : currentParameters) {
+            Injectable fp =
+                InjectableFactory.getInstance().create(parameterTypes[i],
+                                                       injectable.getAnnotations(),
+                                                       method,
+                                                       getMetadata().isEncoded() || metadata
+                                                           .isEncoded(),
+                                                       metadata.getDefaultValue());
+            metadata.getFormalParameters().add(fp);
+            ++i;
+        }
+        logger.trace("mergeFormalParameterMetadata exit");
+    }
+
     @Override
     protected final boolean isConstructorParameterValid(Injectable fp) {
         // This method is declared as final, since parseConstructors(), which

Modified: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/providers/entity/GenericInheritanceProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/providers/entity/GenericInheritanceProviderTest.java?rev=1023029&r1=1023028&r2=1023029&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/providers/entity/GenericInheritanceProviderTest.java
(original)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/providers/entity/GenericInheritanceProviderTest.java
Fri Oct 15 17:31:58 2010
@@ -28,6 +28,7 @@ import java.lang.reflect.Type;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyReader;
@@ -60,15 +61,39 @@ public class GenericInheritanceProviderT
         }
     }
     
-    public static interface GenericService<T> {
+    public static class Foo {
+        String x;
+        
+        public Foo(String x) {
+            this.x = x;
+        }
+
+        public String getValue() {
+            return x + "foo";
+        }
+    }
+    
+    public static class Bar extends Foo {
+        
+        public Bar(String x) {
+            super(x);
+        }
+        
+        public String getValue() {
+            return x + "bar";
+        }
+    }
+    
+    public static interface GenericService<T, X extends Foo> {
         @POST
         @Consumes(MediaType.APPLICATION_JSON)
-        public void doSomething(T obj);
+        @Path("{id1}/{id2}")
+        public void doSomething(@PathParam("id1") int id1, @PathParam("id2") X id2, T obj);
     }
 
     @Path("/impl")
-    public static class GenericServiceImpl implements GenericService<MyJAXBObject>
{
-        public void doSomething(MyJAXBObject obj) {
+    public static class GenericServiceImpl implements GenericService<MyJAXBObject, Bar>
{
+        public void doSomething(int id, Bar id2, MyJAXBObject obj) {
             reachedNirvana = true;
         }
     }
@@ -108,7 +133,7 @@ public class GenericInheritanceProviderT
     public void test() throws Exception {
         MockHttpServletRequest request =
             MockRequestConstructor.constructMockRequest("POST",
-                                                        "/impl",
+                                                        "/impl/5/6",
                                                         MediaType.TEXT_PLAIN,
                                                         MediaType.APPLICATION_JSON,
                                                         "{stringdata: \"hi\"}".getBytes());



Mime
View raw message