Repository: camel Updated Branches: refs/heads/master 5639b7860 -> 17dc42bcf
CAMEL-9690: bean parameter binding should check parameter types when using simple expressions to match best possible method when there are overloaded methods. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/17dc42bc Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/17dc42bc Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/17dc42bc Branch: refs/heads/master Commit: 17dc42bcf5e845c4bd6ff0dc3217e148ebdcf76e Parents: 5639b78 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Mar 22 12:38:13 2016 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Mar 22 12:38:13 2016 +0100 ---------------------------------------------------------------------- .../apache/camel/component/bean/BeanInfo.java | 19 ++++- .../BeanParameterBestTypeMatchIssueTest.java | 75 ++++++++++++++++++++ .../camel/component/bean/issues/ClassA.java | 40 +++++++++++ .../camel/component/bean/issues/ClassB.java | 23 ++++++ 4 files changed, 156 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/17dc42bc/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java index ed065a6..9a8d645 100644 --- a/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java +++ b/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java @@ -52,6 +52,7 @@ import org.apache.camel.spi.Registry; import org.apache.camel.util.CastUtils; import org.apache.camel.util.IntrospectionSupport; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.StringHelper; import org.apache.camel.util.StringQuoteHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -610,6 +611,7 @@ public class BeanInfo { String parameters = ObjectHelper.between(name, "(", ")"); if (parameters != null) { // special as we have hardcoded parameters, so we need to choose method that matches those parameters the best + LOG.trace("Choosing best matching method matching parameters: {}", parameters); answer = chooseMethodWithMatchingParameters(exchange, parameters, possibleOperations); } } @@ -659,16 +661,31 @@ public class BeanInfo { List<MethodInfo> candidates = new ArrayList<MethodInfo>(); MethodInfo fallbackCandidate = null; for (MethodInfo info : operations) { - it = ObjectHelper.createIterator(parameters); + it = ObjectHelper.createIterator(parameters, ",", false); int index = 0; boolean matches = true; while (it.hasNext()) { String parameter = (String) it.next(); + if (parameter != null) { + // must trim + parameter = parameter.trim(); + } + Class<?> parameterType = BeanHelper.getValidParameterType(parameter); Class<?> expectedType = info.getParameters().get(index).getType(); if (parameterType != null && expectedType != null) { + // if its a simple language then we need to evaluate the expression + // so we have the result and can find out what type the parameter actually is + if (StringHelper.hasStartToken(parameter, "simple")) { + LOG.trace("Evaluating simple expression for parameter #{}: {} to determine the class type of the parameter", index, parameter); + Object out = getCamelContext().resolveLanguage("simple").createExpression(parameter).evaluate(exchange, Object.class); + if (out != null) { + parameterType = out.getClass(); + } + } + // skip java.lang.Object type, when we have multiple possible methods we want to avoid it if possible if (Object.class.equals(expectedType)) { fallbackCandidate = info; http://git-wip-us.apache.org/repos/asf/camel/blob/17dc42bc/camel-core/src/test/java/org/apache/camel/component/bean/issues/BeanParameterBestTypeMatchIssueTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/issues/BeanParameterBestTypeMatchIssueTest.java b/camel-core/src/test/java/org/apache/camel/component/bean/issues/BeanParameterBestTypeMatchIssueTest.java new file mode 100644 index 0000000..9efb0d2 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/bean/issues/BeanParameterBestTypeMatchIssueTest.java @@ -0,0 +1,75 @@ +/** + * 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.issues; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; + +public class BeanParameterBestTypeMatchIssueTest extends ContextTestSupport { + + public void testNoParam() throws InterruptedException { + getMockEndpoint("mock:end").expectedBodiesReceived("A"); + + template.sendBodyAndHeader("direct:noParam", "body", "key", "value"); + + assertMockEndpointsSatisfied(); + } + + public void test1Param() throws InterruptedException { + getMockEndpoint("mock:end").expectedBodiesReceived("B"); + + template.sendBodyAndHeader("direct:1Param", "body", "key", "value"); + + assertMockEndpointsSatisfied(); + } + + public void test2ParamString() throws InterruptedException { + getMockEndpoint("mock:end").expectedBodiesReceived("C"); + + template.sendBodyAndHeader("direct:2Param", "body", "key", "value"); + + assertMockEndpointsSatisfied(); + } + + public void test2ParamClassB() throws InterruptedException { + getMockEndpoint("mock:end").expectedBodiesReceived("D"); + + template.sendBodyAndHeader("direct:2Param", "body", "key", new ClassB()); + + assertMockEndpointsSatisfied(); + } + + public void test2ParamBoolBody() throws InterruptedException { + getMockEndpoint("mock:end").expectedBodiesReceived("E"); + + template.sendBodyAndHeader("direct:2Param", true, "key", "value"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:noParam").bean(ClassA.class, "foo()").to("mock:end"); + from("direct:1Param").bean(ClassA.class, "foo(${body})").to("mock:end"); + from("direct:2Param").bean(ClassA.class, "foo(${body}, ${header.key})").to("mock:end"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/17dc42bc/camel-core/src/test/java/org/apache/camel/component/bean/issues/ClassA.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/issues/ClassA.java b/camel-core/src/test/java/org/apache/camel/component/bean/issues/ClassA.java new file mode 100644 index 0000000..ab20f04 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/bean/issues/ClassA.java @@ -0,0 +1,40 @@ +/** + * 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.issues; + +public class ClassA { + + public String foo() { + return "A"; + } + + public String foo(String param1) { + return "B"; + } + + public String foo(String param1, String param2) { + return "C"; + } + + public String foo(String param1, ClassB param2) { + return "D"; + } + + public String foo(boolean param1, String param2) { + return "E"; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/17dc42bc/camel-core/src/test/java/org/apache/camel/component/bean/issues/ClassB.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/issues/ClassB.java b/camel-core/src/test/java/org/apache/camel/component/bean/issues/ClassB.java new file mode 100644 index 0000000..52a3c50 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/bean/issues/ClassB.java @@ -0,0 +1,23 @@ +/** + * 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.issues; + +public class ClassB { + + // empty + +}