cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1332390 - /cxf/trunk/api/src/main/java/org/apache/cxf/common/util/ReflectionInvokationHandler.java
Date Mon, 30 Apr 2012 21:03:28 GMT
Author: dkulp
Date: Mon Apr 30 21:03:28 2012
New Revision: 1332390

URL: http://svn.apache.org/viewvc?rev=1332390&view=rev
Log:
Add ability to unwrap a parameter into a real type.

Modified:
    cxf/trunk/api/src/main/java/org/apache/cxf/common/util/ReflectionInvokationHandler.java

Modified: cxf/trunk/api/src/main/java/org/apache/cxf/common/util/ReflectionInvokationHandler.java
URL: http://svn.apache.org/viewvc/cxf/trunk/api/src/main/java/org/apache/cxf/common/util/ReflectionInvokationHandler.java?rev=1332390&r1=1332389&r2=1332390&view=diff
==============================================================================
--- cxf/trunk/api/src/main/java/org/apache/cxf/common/util/ReflectionInvokationHandler.java
(original)
+++ cxf/trunk/api/src/main/java/org/apache/cxf/common/util/ReflectionInvokationHandler.java
Mon Apr 30 21:03:28 2012
@@ -19,6 +19,7 @@
 
 package org.apache.cxf.common.util;
 
+import java.lang.annotation.Annotation;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -47,7 +48,7 @@ public class ReflectionInvokationHandler
     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
         WrapReturn wr = method.getAnnotation(WrapReturn.class);
         try {
-            Method m = target.getClass().getMethod(method.getName(), method.getParameterTypes());
+            Method m = target.getClass().getMethod(method.getName(), getParameterTypes(method,
args));
             ReflectionUtil.setAccessible(m);
             return wrapReturn(wr, m.invoke(target, args));                
         } catch (NoSuchMethodException e) {
@@ -70,6 +71,39 @@ public class ReflectionInvokationHandler
             throw e;
         }
     }
+    private Class<?>[] getParameterTypes(Method method, Object[] args) {
+        Class<?>[] types = method.getParameterTypes();
+        for (int x = 0; x < types.length; x++) {
+            UnwrapParam p = getUnwrapParam(method.getParameterAnnotations()[x]);
+            if (p != null) {
+                String s = p.methodName();
+                String tn = p.typeMethodName();
+                try {
+                    Method m = args[x].getClass().getMethod(s);
+                    if ("#default".equals(tn)) {
+                        types[x] = m.getReturnType();
+                    } else {
+                        Method m2 = args[x].getClass().getMethod(tn);
+                        types[x] = (Class<?>)ReflectionUtil.setAccessible(m2).invoke(args[x]);
+                    }
+                    args[x] = ReflectionUtil.setAccessible(m).invoke(args[x]);
+                } catch (Exception ex) {
+                    throw new RuntimeException(ex);
+                }
+            }
+        }
+        return types;
+    }
+
+    private UnwrapParam getUnwrapParam(Annotation[] annotations) {
+        for (Annotation a : annotations) {
+            if (a instanceof UnwrapParam) {
+                return (UnwrapParam)a;
+            }
+        }
+        return null;
+    }
+
     private static Object wrapReturn(WrapReturn wr, Object t) {
         if (wr == null || t == null) {
             return t;
@@ -92,6 +126,13 @@ public class ReflectionInvokationHandler
         boolean iterator() default false;
     }
     
+    @Target(ElementType.PARAMETER)
+    @Retention(RetentionPolicy.RUNTIME)
+    public static @interface UnwrapParam {
+        String methodName() default "getValue";
+        String typeMethodName() default "#default";
+    }
+    
     private static class WrapperIterator implements Iterator<Object> {
         Class<?> cls;
         Iterator<?> internal;



Mime
View raw message