This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-2.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-2.x by this push: new 6c3f91a Fix NPE at component.bean.MethodInfo.invoke 6c3f91a is described below commit 6c3f91aeaa7d6e64c17965c180f9e57266c494cf Author: Federico Valeri <fvaleri@localhost> AuthorDate: Thu Jul 4 10:32:18 2019 +0200 Fix NPE at component.bean.MethodInfo.invoke This happens only for a call with no args that raise an IllegalAccessException, for example using an inner private class in a CamelTest. In Camel 3 we have the same bug, but it is mitigated by a call to org.apache.camel.support.ObjectHelper.invokeMethodSafe which I also included in this fix. --- .../apache/camel/component/bean/MethodInfo.java | 8 +++--- .../java/org/apache/camel/util/ObjectHelper.java | 31 ++++++++++++++++++++++ .../org/apache/camel/util/ObjectHelperTest.java | 19 +++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java index 1461b2c..a06f011 100644 --- a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java +++ b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java @@ -60,6 +60,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static org.apache.camel.util.ObjectHelper.asString; +import static org.apache.camel.util.ObjectHelper.asList; +import static org.apache.camel.util.ObjectHelper.invokeMethodSafe; /** * Information about a method to be used for invocation. @@ -478,11 +480,11 @@ public class MethodInfo { protected Object invoke(Method mth, Object pojo, Object[] arguments, Exchange exchange) throws InvocationTargetException { try { - return mth.invoke(pojo, arguments); + return invokeMethodSafe(mth, pojo, arguments); } catch (IllegalAccessException e) { - throw new RuntimeExchangeException("IllegalAccessException occurred invoking method: " + mth + " using arguments: " + Arrays.asList(arguments), exchange, e); + throw new RuntimeExchangeException("IllegalAccessException occurred invoking method: " + mth + " using arguments: " + asList(arguments), exchange, e); } catch (IllegalArgumentException e) { - throw new RuntimeExchangeException("IllegalArgumentException occurred invoking method: " + mth + " using arguments: " + Arrays.asList(arguments), exchange, e); + throw new RuntimeExchangeException("IllegalArgumentException occurred invoking method: " + mth + " using arguments: " + asList(arguments), exchange, e); } } diff --git a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java index 1992e0a..a9d1fb0 100644 --- a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java +++ b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java @@ -2083,5 +2083,36 @@ public final class ObjectHelper { return object; } + + /** + * Turns the input array to a list of objects. + * + * @param args an array of objects or null + * @return an object list + */ + public static List<Object> asList(Object[] objects) { + return objects != null ? Arrays.asList(objects) : Collections.emptyList(); + } + + /** + * A helper method to invoke a method via reflection in a safe way by allowing to invoke + * methods that are not accessible by default and wrap any exceptions + * as {@link RuntimeCamelException} instances + * + * @param method the method to invoke + * @param instance the object instance (or null for static methods) + * @param parameters the parameters to the method + * @return the result of the method invocation + */ + public static Object invokeMethodSafe(Method method, Object instance, Object... parameters) throws InvocationTargetException, IllegalAccessException { + Object answer; + method.setAccessible(true); + if (parameters != null) { + answer = method.invoke(instance, parameters); + } else { + answer = method.invoke(instance); + } + return answer; + } } diff --git a/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java b/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java index ba7ec1a..14a3312 100644 --- a/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java +++ b/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java @@ -957,4 +957,23 @@ public class ObjectHelperTest extends Assert { Method m2 = InterfaceSize.class.getMethod("size"); assertFalse(ObjectHelper.isOverridingMethod(InterfaceSize.class, m2, m1, false)); } + + @Test + public void testAsList() { + List<Object> out0 = ObjectHelper.asList(null); + assertNotNull(out0); + assertTrue(out0 instanceof List && out0.size() == 0); + + List<Object> out1 = ObjectHelper.asList(new Object[0]); + assertNotNull(out1); + assertTrue(out1 instanceof List && out1.size() == 0); + + String[] args = new String[] {"foo", "bar"}; + List<Object> out2 = ObjectHelper.asList(args); + assertNotNull(out2); + assertTrue(out2 instanceof List && out2.size() == 2); + assertEquals("foo", out2.get(0)); + assertEquals("bar", out2.get(1)); + } + }