Author: davsclaus
Date: Mon May 18 09:09:21 2009
New Revision: 775864

URL: http://svn.apache.org/viewvc?rev=775864&view=rev
Log:
CAMEL-1549: BeanInfo now excludes by default methods from java.lang.Object.

Added:
    
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExcludedMethodTest.java
   (with props)
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverLoadBalanceWrappedExceptionTest.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverNotCatchedExceptionTest.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=775864&r1=775863&r2=775864&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
 Mon May 18 09:09:21 2009
@@ -56,6 +56,7 @@
  */
 public class BeanInfo {
     private static final transient Log LOG = LogFactory.getLog(BeanInfo.class);
+    private static final List<Method> EXCLUDED_METHODS = new 
ArrayList<Method>();
     private final CamelContext camelContext;
     private final Class type;
     private final ParameterMappingStrategy strategy;
@@ -74,6 +75,17 @@
         this.camelContext = camelContext;
         this.type = type;
         this.strategy = strategy;
+
+        // configure the default excludes methods
+        synchronized (EXCLUDED_METHODS) {
+            if (EXCLUDED_METHODS.size() == 0) {
+                // exclude all java.lang.Object methods as we dont want to 
invoke them
+                for (Method method : Object.class.getMethods()) {
+                    EXCLUDED_METHODS.add(method);
+                }
+            }
+        }
+
         introspect(getType());
         // if there are only 1 method with 1 operation then select it as a 
default/fallback method
         if (operations.size() == 1) {
@@ -169,6 +181,7 @@
      *
      * @param clazz the class
      * @param method the method
+     * @return the method info, is newer <tt>null</tt>
      */
     protected MethodInfo introspect(Class clazz, Method method) {
         if (LOG.isTraceEnabled()) {
@@ -319,9 +332,7 @@
             List<MethodInfo> possibles = new ArrayList<MethodInfo>();
             List<MethodInfo> possiblesWithException = new 
ArrayList<MethodInfo>();
             for (MethodInfo methodInfo : operationList) {
-                // TODO: AOP proxies have additional methods - consider having 
a static
-                // method exclude list to skip all known AOP proxy methods
-                // TODO: This class could use some TRACE logging
+                // TODO: AOP proxies have additional methods - well known 
methods should be added to EXCLUDE_METHODS
 
                 // test for MEP pattern matching
                 boolean out = exchange.getPattern().isOutCapable();
@@ -368,7 +379,6 @@
         } else if (possibles.size() == 1) {
             return possibles.get(0);
         } else if (possibles.isEmpty()) {
-            // TODO: This code is not covered by existing unit test in 
camel-core, need to be tested
             if (LOG.isTraceEnabled()) {
                 LOG.trace("No poosible methods trying to convert body to 
parameter types");
             }
@@ -426,6 +436,14 @@
      * @return true if valid, false to skip the method
      */
     protected boolean isValidMethod(Class clazz, Method method) {
+        // must not be in the excluded list
+        for (Method excluded : EXCLUDED_METHODS) {
+            if (ObjectHelper.isOverridingMethod(excluded, method)) {
+                // the method is overriding an excluded method so its not valid
+                return false;
+            }
+        }
+
         // must be a public method
         if (!Modifier.isPublic(method.getModifiers())) {
             return false;
@@ -447,28 +465,11 @@
      */
     private MethodInfo overridesExistingMethod(MethodInfo methodInfo) {
         for (MethodInfo info : methodMap.values()) {
+            Method source = info.getMethod();
+            Method target = methodInfo.getMethod();
 
-            // name test
-            if 
(!info.getMethod().getName().equals(methodInfo.getMethod().getName())) {
-                continue;
-            }
-
-            // parameter types
-            if (info.getMethod().getParameterTypes().length != 
methodInfo.getMethod().getParameterTypes().length) {
-                continue;
-            }
-
-            boolean found = false;
-            for (int i = 0; i < info.getMethod().getParameterTypes().length; 
i++) {
-                Class type1 = info.getMethod().getParameterTypes()[i];
-                Class type2 = methodInfo.getMethod().getParameterTypes()[i];
-                if (type1.equals(type2)) {
-                    found = true;
-                    break;
-                }
-            }
-
-            if (found) {
+            boolean override = ObjectHelper.isOverridingMethod(source, target);
+            if (override) {
                 // same name, same parameters, then its overrides an existing 
class
                 return info;
             }

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java?rev=775864&r1=775863&r2=775864&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java
 Mon May 18 09:09:21 2009
@@ -17,9 +17,9 @@
 package org.apache.camel.model;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
-import java.util.Arrays;
 
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=775864&r1=775863&r2=775864&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java 
(original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java 
Mon May 18 09:09:21 2009
@@ -601,6 +601,34 @@
     }
 
     /**
+     * Tests whether the target method overrides the source method.
+     * <p/>
+     * Tests whether they have the same name, return type, and parameter list.
+     *
+     * @param source  the source method
+     * @param target  the target method
+     * @return <tt>true</tt> if it override, <tt>false</tt> otherwise
+     */
+    public static boolean isOverridingMethod(Method source, Method target) {
+        if (source.getName().equals(target.getName()) &&
+                source.getReturnType().equals(target.getReturnType()) &&
+                source.getParameterTypes().length == 
target.getParameterTypes().length) {
+
+            // test if parameter types is the same as well
+            for (int i = 0; i < source.getParameterTypes().length; i++) {
+                if 
(!(source.getParameterTypes()[i].equals(target.getParameterTypes()[i]))) {
+                    return false;
+                }
+            }
+
+            // the have same name, return type and parameter list, so its 
overriding
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
      * Returns a list of methods which are annotated with the given annotation
      *
      * @param type the type to reflect on

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExcludedMethodTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExcludedMethodTest.java?rev=775864&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExcludedMethodTest.java
 (added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExcludedMethodTest.java
 Mon May 18 09:09:21 2009
@@ -0,0 +1,79 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.bean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultExchange;
+
+/**
+ * @version $Revision$
+ */
+public class BeanExcludedMethodTest extends ContextTestSupport {
+
+    public void testExcludedMethod() throws Exception {
+        BeanInfo info = new BeanInfo(context, MyDummyBean.class);
+
+        Exchange exchange = new DefaultExchange(context);
+        MyDummyBean pojo = new MyDummyBean();
+        MethodInvocation mi = info.createInvocation(pojo, exchange);
+        assertNull("Should not be possible to find a suitable method", mi);
+    }
+
+    public void testNotExcludedMethod() throws Exception {
+        BeanInfo info = new BeanInfo(context, MyOtherDummyBean.class);
+
+        Exchange exchange = new DefaultExchange(context);
+        MyOtherDummyBean pojo = new MyOtherDummyBean();
+        MethodInvocation mi = info.createInvocation(pojo, exchange);
+        assertNotNull(mi);
+        assertEquals("hello", mi.getMethod().getName());
+    }
+
+    public static class MyDummyBean {
+
+        @Override
+        public boolean equals(Object obj) {
+            fail("Should not call equals");
+            return true;
+        }
+
+        @Override
+        public String toString() {
+            return "dummy";
+        }
+    }
+
+    public static class MyOtherDummyBean {
+
+        @Override
+        public boolean equals(Object obj) {
+            fail("Should not call equals");
+            return true;
+        }
+
+        @Override
+        public String toString() {
+            return "dummy";
+        }
+
+        public String hello(String hi) {
+            return "Hello " + hi;
+        }
+    }
+
+}

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExcludedMethodTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExcludedMethodTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverLoadBalanceWrappedExceptionTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverLoadBalanceWrappedExceptionTest.java?rev=775864&r1=775863&r2=775864&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverLoadBalanceWrappedExceptionTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverLoadBalanceWrappedExceptionTest.java
 Mon May 18 09:09:21 2009
@@ -19,10 +19,10 @@
 import java.io.IOException;
 import java.net.SocketException;
 
+import org.apache.camel.CamelExchangeException;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
-import org.apache.camel.CamelExchangeException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverNotCatchedExceptionTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverNotCatchedExceptionTest.java?rev=775864&r1=775863&r2=775864&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverNotCatchedExceptionTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FailOverNotCatchedExceptionTest.java
 Mon May 18 09:09:21 2009
@@ -19,10 +19,10 @@
 import java.io.IOException;
 import java.net.SocketException;
 
+import org.apache.camel.CamelExecutionException;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
-import org.apache.camel.CamelExecutionException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 


Reply via email to