cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r666837 - in /cxf/trunk/common/common/src: main/java/org/apache/cxf/common/injection/ResourceInjector.java test/java/org/apache/cxf/common/injection/ResourceInjectorTest.java
Date Wed, 11 Jun 2008 21:04:27 GMT
Author: dkulp
Date: Wed Jun 11 14:04:27 2008
New Revision: 666837

URL: http://svn.apache.org/viewvc?rev=666837&view=rev
Log:
[CXF-1645] Fix for injection of subclasses of resources

Modified:
    cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java
    cxf/trunk/common/common/src/test/java/org/apache/cxf/common/injection/ResourceInjectorTest.java

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java?rev=666837&r1=666836&r2=666837&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java
(original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/common/injection/ResourceInjector.java
Wed Jun 11 14:04:27 2008
@@ -244,8 +244,10 @@
             if (method.getDeclaringClass().isAssignableFrom(getTarget().getClass())) {
                 method.invoke(getTarget(), resource);
             } else { // deal with the proxy setter method
-                Method targetMethod = getTarget().getClass().
-                getMethod(method.getName(), new Class[]{resource.getClass()});
+                Method targetMethod = findMethod(getTarget().getClass(), 
+                                                 method.getName(),
+                                                 resource);                
+
                 targetMethod.invoke(getTarget(), resource);
             }
         } catch (IllegalAccessException e) { 
@@ -260,6 +262,22 @@
     } 
 
 
+    private Method findMethod(Class<? extends Object> class1, String name, Object resource)
+        throws SecurityException, NoSuchMethodException {
+        try {
+            return class1.getMethod(name, new Class[]{resource.getClass()});
+        } catch (NoSuchMethodException e) {
+            for (Method m : class1.getMethods()) {
+                if (m.getName().equals(name)
+                    && m.getParameterTypes().length == 1
+                    && m.getParameterTypes()[0].isInstance(resource)) {
+                    return m;
+                }
+            }
+            throw e;
+        }
+    }
+
     private String getResourceName(Resource res, Method method) { 
         assert method != null; 
         assert res != null; 

Modified: cxf/trunk/common/common/src/test/java/org/apache/cxf/common/injection/ResourceInjectorTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/injection/ResourceInjectorTest.java?rev=666837&r1=666836&r2=666837&view=diff
==============================================================================
--- cxf/trunk/common/common/src/test/java/org/apache/cxf/common/injection/ResourceInjectorTest.java
(original)
+++ cxf/trunk/common/common/src/test/java/org/apache/cxf/common/injection/ResourceInjectorTest.java
Wed Jun 11 14:04:27 2008
@@ -47,6 +47,7 @@
 public class ResourceInjectorTest extends Assert {
     private static final String RESOURCE_ONE = "resource one";
     private static final String RESOURCE_TWO = "resource two";
+    private static final String RESOURCE_THREE = "resource three";
     
     private ResourceInjector injector; 
         
@@ -61,6 +62,8 @@
         EasyMock.expectLastCall().andReturn(RESOURCE_ONE);
         resMgr.resolveResource("resource2", String.class, resolvers);
         EasyMock.expectLastCall().andReturn(RESOURCE_TWO);
+        resMgr.resolveResource("resource3", CharSequence.class, resolvers);
+        EasyMock.expectLastCall().andReturn(RESOURCE_THREE);
         EasyMock.replay(resMgr);
         
         injector = new ResourceInjector(resMgr); 
@@ -145,7 +148,9 @@
 
         assertNotNull(target.getResource2()); 
         assertEquals(RESOURCE_TWO, target.getResource2());
-         
+        
+        assertNotNull(target.getResource3());
+        assertEquals(RESOURCE_THREE, target.getResource3());
     }
     
     private Target getProxyObject() {
@@ -168,6 +173,7 @@
 interface Target {
     String getResource1(); 
     String getResource2(); 
+    CharSequence getResource3();
 }
 
 class CallInterceptor implements MethodInterceptor {
@@ -189,6 +195,9 @@
 
     @Resource(name = "resource2")
     private String resource2foo;
+    
+    @Resource(name = "resource3")
+    private CharSequence resource3foo;
 
     public String getResource1() { 
         return resource1; 
@@ -197,9 +206,13 @@
     public String getResource2() { 
         return resource2foo;
     } 
+    
+    public CharSequence getResource3() {
+        return resource3foo;
+    }
 
     public String toString() { 
-        return "[" + resource1 + ":" + resource2foo + "]";
+        return "[" + resource1 + ":" + resource2foo + ":" + resource3foo + "]";
     }
 
 }
@@ -214,6 +227,7 @@
 interface ISetterTarget extends Target {    
     void setResource1(final String argResource1);    
     void setResource2(final String argResource2);
+    void setResource3(final CharSequence argResource3);
 }
 
 class ProxyClass implements InvocationHandler {
@@ -231,6 +245,9 @@
                 types = new Class[args.length];
                 for (int i = 0; i < args.length; i++) {
                     types[i] = args[i].getClass();
+                    if ("setResource3".equals(m.getName()) && types[i].equals(String.class))
{
+                        types[i] = CharSequence.class;
+                    }
                 }
             }    
             Method target = obj.getClass().getMethod(m.getName(), types);
@@ -249,6 +266,7 @@
 
     private String resource1;
     private String resource2;
+    private CharSequence resource3;
     private boolean injectionCompletePublic; 
     private boolean injectionCompletePrivate; 
     private boolean preDestroy; 
@@ -271,6 +289,15 @@
     public void setResource2(final String argResource2) {
         this.resource2 = argResource2;
     }
+    
+    public final CharSequence getResource3() {
+        return this.resource3;
+    }
+    
+    @Resource(name = "resource3")
+    public void setResource3(final CharSequence argResource3) {
+        this.resource3 = argResource3;
+    }
 
     @PostConstruct
     public void injectionIsAllFinishedNowThankYouVeryMuch() { 
@@ -310,9 +337,12 @@
     }
 }
 
+//CHECKSTYLE:OFF
 @Resource(name = "resource1")
 class ClassTarget implements Target {
 
+    @Resource(name = "resource3") 
+    public CharSequence resource3foo;
     @Resource(name = "resource2") 
     public String resource2foo; 
     private String res1; 
@@ -328,16 +358,22 @@
     public final String getResource2() {
         return resource2foo;
     }
+    
+    public final CharSequence getResource3() {
+        return resource3foo;
+    }
 }
 
 
 
 @Resources({@Resource(name = "resource1"), 
-            @Resource(name = "resource2") })
+            @Resource(name = "resource2"),
+            @Resource(name = "resource3") })
 class ResourcesContainerTarget implements Target {
 
     private String res1; 
     private String resource2; 
+    private CharSequence resource3;
 
     public final void setResource1(String res) { 
         res1 = res; 
@@ -350,4 +386,8 @@
     public final String getResource2() {
         return resource2;
     }
+    
+    public final CharSequence getResource3() {
+        return resource3;
+    }
 }



Mime
View raw message