You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/03/26 12:28:24 UTC

svn commit: r758600 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/bean/BeanInfo.java test/java/org/apache/camel/component/bean/BeanInfoAMoreComplexOverloadedTest.java

Author: davsclaus
Date: Thu Mar 26 11:28:10 2009
New Revision: 758600

URL: http://svn.apache.org/viewvc?rev=758600&view=rev
Log:
CAMEL-1424: BeanInfo better logic for overloaded methods.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoAMoreComplexOverloadedTest.java
      - copied, changed from r758565, camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoOverloadedTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java?rev=758600&r1=758599&r2=758600&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java Thu Mar 26 11:28:10 2009
@@ -59,7 +59,8 @@
     private final CamelContext camelContext;
     private final Class type;
     private final ParameterMappingStrategy strategy;
-    private final Map<String, MethodInfo> operations = new ConcurrentHashMap<String, MethodInfo>();
+    // TODO: A new key
+    private final Map<String, List<MethodInfo>> operations = new ConcurrentHashMap<String, List<MethodInfo>>();
     private final List<MethodInfo> operationsWithBody = new ArrayList<MethodInfo>();
     private final List<MethodInfo> operationsWithCustomAnnotation = new ArrayList<MethodInfo>();
     private final Map<Method, MethodInfo> methodMap = new ConcurrentHashMap<Method, MethodInfo>();
@@ -75,10 +76,11 @@
         this.type = type;
         this.strategy = strategy;
         introspect(getType());
+        // if there are only 1 method with 1 operation then select it as a default/fallback method
         if (operations.size() == 1) {
-            Collection<MethodInfo> methodInfos = operations.values();
-            for (MethodInfo methodInfo : methodInfos) {
-                defaultMethod = methodInfo;
+            List<MethodInfo> methods = operations.values().iterator().next();
+            if (methods.size() == 1) {
+                defaultMethod = methods.get(0);
             }
         }
     }
@@ -116,7 +118,9 @@
 
         String name = exchange.getIn().getHeader(Exchange.BEAN_METHOD_NAME, String.class);
         if (name != null) {
-            methodInfo = operations.get(name);
+            if (operations.get(name).size() == 1) {
+                methodInfo = operations.get(name).get(0);
+            }
         }
         if (methodInfo == null) {
             methodInfo = chooseMethod(pojo, exchange);
@@ -187,7 +191,17 @@
         if (LOG.isTraceEnabled()) {
             LOG.trace("Adding operation: " + opName + " for method: " + methodInfo);
         }
-        operations.put(opName, methodInfo);
+
+        if (operations.containsKey(opName)) {
+            // we have an overloaded method so add the method info to the same key
+            List<MethodInfo> existing = operations.get(opName);
+            existing.add(methodInfo);
+        } else {
+            // its a new method we have not seen before so wrap it in a list and add it
+            List<MethodInfo> methods = new ArrayList<MethodInfo>();
+            methods.add(methodInfo);
+            operations.put(opName, methods);
+        }
 
         if (methodInfo.hasBodyParameter()) {
             operationsWithBody.add(methodInfo);

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoAMoreComplexOverloadedTest.java (from r758565, camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoOverloadedTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoAMoreComplexOverloadedTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoAMoreComplexOverloadedTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoOverloadedTest.java&r1=758565&r2=758600&rev=758600&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoOverloadedTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInfoAMoreComplexOverloadedTest.java Thu Mar 26 11:28:10 2009
@@ -27,9 +27,25 @@
 /**
  * @version $Revision$
  */
-public class BeanInfoOverloadedTest extends ContextTestSupport {
+public class BeanInfoAMoreComplexOverloadedTest extends ContextTestSupport {
 
-    public void testBeanInfoOverloaded() throws Exception {
+    public void testRequestA() throws Exception {
+        BeanInfo beanInfo = new BeanInfo(context, Bean.class);
+
+        Message message = new DefaultMessage();
+        message.setBody(new RequestA());
+        Exchange exchange = new DefaultExchange(context);
+        exchange.setIn(message);
+
+        MethodInvocation methodInvocation = beanInfo.createInvocation(new Bean(), exchange);
+        Method method = methodInvocation.getMethod();
+
+        assertEquals("doSomething", method.getName());
+
+        assertEquals(RequestA.class, method.getGenericParameterTypes()[0]);
+    }
+
+    public void testRequestB() throws Exception {
         BeanInfo beanInfo = new BeanInfo(context, Bean.class);
 
         Message message = new DefaultMessage();
@@ -45,6 +61,18 @@
         assertEquals(RequestB.class, method.getGenericParameterTypes()[0]);
     }
 
+    public void testNoMatch() throws Exception {
+        BeanInfo beanInfo = new BeanInfo(context, Bean.class);
+
+        Message message = new DefaultMessage();
+        message.setBody("Hello World");
+        Exchange exchange = new DefaultExchange(context);
+        exchange.setIn(message);
+
+        MethodInvocation methodInvocation = beanInfo.createInvocation(new Bean(), exchange);
+        assertNull("Should not find a suitable method", methodInvocation);
+    }
+
     class Bean {
         public void doSomething(RequestA request) {
         }
@@ -53,12 +81,16 @@
         }
     }
 
-    class RequestA {
+    class BaseRequest {
+        public long id;
+    }
+
+    class RequestA extends BaseRequest {
         public int i;
     }
 
-    class RequestB {
+    class RequestB extends BaseRequest {
         public String s;
     }
 
-}
+}
\ No newline at end of file