CAMEL-8624: Bean component - Potential NPE in BeanInfo
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5caa6a0c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5caa6a0c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5caa6a0c Branch: refs/heads/camel-2.15.x Commit: 5caa6a0c3dd70cdde2def4bd322a0af7e4bdada1 Parents: 65d2f75 Author: Claus Ibsen <[email protected]> Authored: Mon Apr 13 10:40:48 2015 +0200 Committer: Claus Ibsen <[email protected]> Committed: Mon Apr 13 10:41:04 2015 +0200 ---------------------------------------------------------------------- .../apache/camel/component/bean/BeanInfo.java | 26 ++++++----- .../bean/issues/AbstractTransformer.java | 25 +++++++++++ .../component/bean/issues/Transformer.java | 25 +++++++++++ .../component/bean/issues/TransformerImpl.java | 27 ++++++++++++ .../bean/issues/TransformerIssueTest.java | 46 ++++++++++++++++++++ 5 files changed, 137 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/5caa6a0c/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 d33eb7f..67a0893 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 @@ -759,20 +759,22 @@ public class BeanInfo { MethodInfo matched = null; int matchCounter = 0; for (MethodInfo methodInfo : operationList) { - if (methodInfo.getBodyParameterType().isInstance(body)) { - return methodInfo; - } + if (methodInfo.getBodyParameterType() != null) { + if (methodInfo.getBodyParameterType().isInstance(body)) { + return methodInfo; + } - // we should only try to convert, as we are looking for best match - Object value = exchange.getContext().getTypeConverter().tryConvertTo(methodInfo.getBodyParameterType(), exchange, body); - if (value != null) { - if (LOG.isTraceEnabled()) { - LOG.trace("Converted body from: {} to: {}", - body.getClass().getCanonicalName(), methodInfo.getBodyParameterType().getCanonicalName()); + // we should only try to convert, as we are looking for best match + Object value = exchange.getContext().getTypeConverter().tryConvertTo(methodInfo.getBodyParameterType(), exchange, body); + if (value != null) { + if (LOG.isTraceEnabled()) { + LOG.trace("Converted body from: {} to: {}", + body.getClass().getCanonicalName(), methodInfo.getBodyParameterType().getCanonicalName()); + } + matchCounter++; + newBody = value; + matched = methodInfo; } - matchCounter++; - newBody = value; - matched = methodInfo; } } if (matchCounter > 1) { http://git-wip-us.apache.org/repos/asf/camel/blob/5caa6a0c/camel-core/src/test/java/org/apache/camel/component/bean/issues/AbstractTransformer.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/issues/AbstractTransformer.java b/camel-core/src/test/java/org/apache/camel/component/bean/issues/AbstractTransformer.java new file mode 100644 index 0000000..0209c2e --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/bean/issues/AbstractTransformer.java @@ -0,0 +1,25 @@ +/** + * 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 abstract class AbstractTransformer implements Transformer { + + protected String getTest() { + return "test"; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/5caa6a0c/camel-core/src/test/java/org/apache/camel/component/bean/issues/Transformer.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/issues/Transformer.java b/camel-core/src/test/java/org/apache/camel/component/bean/issues/Transformer.java new file mode 100644 index 0000000..f231e21 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/bean/issues/Transformer.java @@ -0,0 +1,25 @@ +/** + * 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.Exchange; + +public interface Transformer { + + Object transform(Exchange exchange); + +} http://git-wip-us.apache.org/repos/asf/camel/blob/5caa6a0c/camel-core/src/test/java/org/apache/camel/component/bean/issues/TransformerImpl.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/issues/TransformerImpl.java b/camel-core/src/test/java/org/apache/camel/component/bean/issues/TransformerImpl.java new file mode 100644 index 0000000..b6ccda1 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/bean/issues/TransformerImpl.java @@ -0,0 +1,27 @@ +/** + * 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.Exchange; + +public class TransformerImpl extends AbstractTransformer { + + @Override + public String transform(Exchange exchange) { + return getTest(); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/5caa6a0c/camel-core/src/test/java/org/apache/camel/component/bean/issues/TransformerIssueTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/issues/TransformerIssueTest.java b/camel-core/src/test/java/org/apache/camel/component/bean/issues/TransformerIssueTest.java new file mode 100644 index 0000000..5137f71 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/bean/issues/TransformerIssueTest.java @@ -0,0 +1,46 @@ +/** + * 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; + +/** + * @version + */ +public class TransformerIssueTest extends ContextTestSupport { + + public void testTransformer() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("test"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .bean(TransformerImpl.class, "transform") + .to("mock:result"); + } + }; + } +}
