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
The following commit(s) were added to refs/heads/master by this push: new 7646138 CAMEL-14302: Fixed invoking static methods on beans by class name (type) that does not have default no-arg constructors. Should not autowire constructor as we did not do that in Camel 2.x 7646138 is described below commit 7646138c57983800f56a30bfd43a30464d7174fe Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Dec 16 06:44:00 2019 +0100 CAMEL-14302: Fixed invoking static methods on beans by class name (type) that does not have default no-arg constructors. Should not autowire constructor as we did not do that in Camel 2.x --- .../org/apache/camel/component/bean/BeanInfo.java | 10 ++++++ .../camel/component/bean/ConstantBeanHolder.java | 7 ---- .../component/bean/ConstantTypeBeanHolder.java | 18 ++++------ .../spring/SpringBeanIoCStaticMethodTest.java | 42 ++++++++++++++++++++++ .../camel/spring/SpringBeanIoCStaticMethodTest.xml | 37 +++++++++++++++++++ 5 files changed, 96 insertions(+), 18 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 4cd5478..899b03e 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 @@ -76,6 +76,7 @@ public class BeanInfo { private List<MethodInfo> operationsWithHandlerAnnotation = new ArrayList<>(); private Map<Method, MethodInfo> methodMap = new HashMap<>(); private boolean publicConstructors; + private boolean publicNoArgConstructors; static { // exclude all java.lang.Object methods as we dont want to invoke them @@ -125,6 +126,7 @@ public class BeanInfo { operationsWithHandlerAnnotation = beanInfo.operationsWithHandlerAnnotation; methodMap = beanInfo.methodMap; publicConstructors = beanInfo.publicConstructors; + publicNoArgConstructors = beanInfo.publicNoArgConstructors; return; } @@ -306,6 +308,7 @@ public class BeanInfo { // does the class have any public constructors? publicConstructors = clazz.getConstructors().length > 0; + publicNoArgConstructors = org.apache.camel.util.ObjectHelper.hasDefaultPublicNoArgConstructor(clazz); MethodsFilter methods = new MethodsFilter(getType()); introspect(clazz, methods); @@ -1174,6 +1177,13 @@ public class BeanInfo { } /** + * Returns whether the bean class has any public no-arg constructors. + */ + public boolean hasPublicNoArgConstructors() { + return publicNoArgConstructors; + } + + /** * Gets the list of methods sorted by A..Z method name. * * @return the methods. diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java index 8fecf1d..bbb758f 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantBeanHolder.java @@ -44,13 +44,6 @@ public class ConstantBeanHolder implements BeanHolder { this.beanInfo = new BeanInfo(context, bean.getClass()); } - public ConstantBeanHolder(Object bean, CamelContext context, ParameterMappingStrategy parameterMappingStrategy) { - ObjectHelper.notNull(bean, "bean"); - - this.bean = bean; - this.beanInfo = new BeanInfo(context, bean.getClass(), parameterMappingStrategy); - } - @Override public String toString() { // avoid invoke toString on bean as it may be a remote proxy diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java index f1e42bf..400330e 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/ConstantTypeBeanHolder.java @@ -27,6 +27,10 @@ public class ConstantTypeBeanHolder implements BeanTypeHolder { private final Class<?> type; private final BeanInfo beanInfo; + public ConstantTypeBeanHolder(Class<?> type, CamelContext context) { + this(type, new BeanInfo(context, type)); + } + public ConstantTypeBeanHolder(Class<?> type, BeanInfo beanInfo) { ObjectHelper.notNull(type, "type"); ObjectHelper.notNull(beanInfo, "beanInfo"); @@ -35,14 +39,6 @@ public class ConstantTypeBeanHolder implements BeanTypeHolder { this.beanInfo = beanInfo; } - public ConstantTypeBeanHolder(Class<?> type, CamelContext context) { - this(type, new BeanInfo(context, type)); - } - - public ConstantTypeBeanHolder(Class<?> type, CamelContext context, ParameterMappingStrategy parameterMappingStrategy) { - this(type, new BeanInfo(context, type, parameterMappingStrategy)); - } - /** * Creates a cached and constant {@link org.apache.camel.component.bean.BeanHolder} from this holder. * @@ -60,9 +56,9 @@ public class ConstantTypeBeanHolder implements BeanTypeHolder { @Override public Object getBean() { - // only create a bean if we have constructors - if (beanInfo.hasPublicConstructors()) { - return getBeanInfo().getCamelContext().getInjector().newInstance(type); + // only create a bean if we have a default no-arg constructor + if (beanInfo.hasPublicNoArgConstructors()) { + return getBeanInfo().getCamelContext().getInjector().newInstance(type, false); } else { return null; } diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/SpringBeanIoCStaticMethodTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/SpringBeanIoCStaticMethodTest.java new file mode 100644 index 0000000..3141653 --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/spring/SpringBeanIoCStaticMethodTest.java @@ -0,0 +1,42 @@ +/* + * 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.spring; + +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class SpringBeanIoCStaticMethodTest extends SpringTestSupport { + + @Override + protected AbstractXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/spring/SpringBeanIoCStaticMethodTest.xml"); + } + + @Test + public void testStaticBean() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("Hello World"); + mock.message(0).header("foo").isNotNull(); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + +} diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/SpringBeanIoCStaticMethodTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/SpringBeanIoCStaticMethodTest.xml new file mode 100644 index 0000000..25d18ac --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/SpringBeanIoCStaticMethodTest.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <route> + <from uri="direct:start"/> + <setHeader name="foo"> + <method beanType="java.util.UUID" method="randomUUID"/> + </setHeader> + <to uri="mock:result"/> + </route> + </camelContext> + +</beans>