This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit b9e6b39323e20a7cef6eb644313abaa529ad1961 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Nov 5 12:06:38 2020 +0100 CAMEL-15810: simple language with OGNL method call should do better method choosing when there are overloaded methods. Thanks to Josh Smith for test case. --- .../org/apache/camel/component/bean/BeanInfo.java | 35 ++++++++++++++++------ .../simple/SimpleInheritanceIssueTest.java | 25 ++++------------ 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java index 2d30aae..b61f37c 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java @@ -664,7 +664,26 @@ public class BeanInfo { // okay we still got multiple operations, so need to match the best one List<MethodInfo> candidates = new ArrayList<>(); + // look for best method without any type conversion + MethodInfo fallbackCandidate = chooseBestPossibleMethod(exchange, parameters, false, operations, candidates); + if (fallbackCandidate == null && candidates.isEmpty()) { + // okay then look again for best method with type conversion + fallbackCandidate = chooseBestPossibleMethod(exchange, parameters, true, operations, candidates); + } + if (candidates.size() > 1) { + MethodInfo answer = getSingleCovariantMethod(candidates); + if (answer != null) { + return answer; + } + } + return candidates.size() == 1 ? candidates.get(0) : fallbackCandidate; + } + + private MethodInfo chooseBestPossibleMethod( + Exchange exchange, String parameters, boolean allowConversion, + List<MethodInfo> operations, List<MethodInfo> candidates) { MethodInfo fallbackCandidate = null; + for (MethodInfo info : operations) { Iterator<?> it = ObjectHelper.createIterator(parameters, ",", false); int index = 0; @@ -702,6 +721,10 @@ public class BeanInfo { } boolean matchingTypes = isParameterMatchingType(parameterType, expectedType); + if (!matchingTypes && allowConversion) { + matchingTypes + = getCamelContext().getTypeConverterRegistry().lookup(expectedType, parameterType) != null; + } if (!matchingTypes) { matches = false; break; @@ -715,14 +738,7 @@ public class BeanInfo { candidates.add(info); } } - - if (candidates.size() > 1) { - MethodInfo answer = getSingleCovariantMethod(candidates); - if (answer != null) { - return answer; - } - } - return candidates.size() == 1 ? candidates.get(0) : fallbackCandidate; + return fallbackCandidate; } private boolean isParameterMatchingType(Class<?> parameterType, Class<?> expectedType) { @@ -739,7 +755,8 @@ public class BeanInfo { return true; } } - return parameterType.isAssignableFrom(expectedType); + return expectedType.isAssignableFrom(parameterType); + // return parameterType.isAssignableFrom(expectedType); } private MethodInfo getSingleCovariantMethod(Collection<MethodInfo> candidates) { diff --git a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleInheritanceIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleInheritanceIssueTest.java index ca3e472..88d1a5e 100644 --- a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleInheritanceIssueTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleInheritanceIssueTest.java @@ -18,14 +18,13 @@ package org.apache.camel.language.simple; import java.io.ByteArrayInputStream; import java.io.InputStream; -import java.util.Scanner; import org.apache.camel.Expression; import org.apache.camel.LanguageTestSupport; -import org.junit.jupiter.api.Disabled; +import org.apache.camel.converter.IOConverter; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; public class SimpleInheritanceIssueTest extends LanguageTestSupport { @@ -58,18 +57,6 @@ public class SimpleInheritanceIssueTest extends LanguageTestSupport { } @Test - @Disabled - public void testMethodCallOverloaded() throws Exception { - MyParser parser = new MyParser(); - exchange.getIn().setBody(parser); - - Expression expression = context.resolveLanguage("simple").createExpression("${body.parse('data')}"); - String result = expression.evaluate(exchange, String.class); - assertEquals("data", result); - } - - @Test - @Disabled public void testMethodCallOverloadedHeader() throws Exception { MyParser parser = new MyParser(); exchange.getIn().setBody(parser); @@ -93,12 +80,12 @@ public class SimpleInheritanceIssueTest extends LanguageTestSupport { public static class MyParser { public String parse(byte[] input) { - return new String(input); + return "array"; } - public String parse(InputStream input) { - Scanner s = new Scanner(input).useDelimiter("\\A"); - return s.hasNext() ? s.next() : ""; + public String parse(InputStream input) throws Exception { + String data = IOConverter.toString(input, null); + return data; } }
