You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/06/04 13:27:36 UTC

[camel] branch main updated: CAMEL-17999: camel-bean - Allow to invoke clone method (also via simple language). Thanks to Steffen Brauns for unit test.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 5ce335bc7c5 CAMEL-17999: camel-bean - Allow to invoke clone method (also via simple language). Thanks to Steffen Brauns for unit test.
5ce335bc7c5 is described below

commit 5ce335bc7c5689f647d3ab850a869c2caf440846
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Jun 4 15:27:10 2022 +0200

    CAMEL-17999: camel-bean - Allow to invoke clone method (also via simple language). Thanks to Steffen Brauns for unit test.
---
 .../org/apache/camel/component/bean/BeanInfo.java  |  7 ++-
 .../camel/language/simple/InvokeCloneTest.java     | 71 ++++++++++++++++++++++
 .../apache/camel/language/simple/MyCloneBean.java  | 47 ++++++++++++++
 3 files changed, 124 insertions(+), 1 deletion(-)

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 bcf117ff9d6..d15b5fa52eb 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
@@ -66,7 +66,7 @@ public class BeanInfo {
     private static final String BYTE_BUDDY_METHOD_MARKER = "$accessor$";
     private static final String CLIENT_PROXY_SUFFIX = "_ClientProxy";
     private static final String[] EXCLUDED_METHOD_NAMES = new String[] {
-            "clone", "equals", "finalize", "getClass", "hashCode", "notify", "notifyAll", "wait", // java.lang.Object
+            "equals", "finalize", "getClass", "hashCode", "notify", "notifyAll", "wait", // java.lang.Object
             "getInvocationHandler", "getProxyClass", "isProxyClass", "newProxyInstance" // java.lang.Proxy
     };
     private final CamelContext camelContext;
@@ -893,6 +893,11 @@ public class BeanInfo {
             }
         }
 
+        // special for Object where clone is not allowed to be called directly
+        if (Object.class == clazz && "clone".equals(name)) {
+            return false;
+        }
+
         // must not be a private method
         boolean privateMethod = Modifier.isPrivate(method.getModifiers());
         if (privateMethod) {
diff --git a/core/camel-core/src/test/java/org/apache/camel/language/simple/InvokeCloneTest.java b/core/camel-core/src/test/java/org/apache/camel/language/simple/InvokeCloneTest.java
new file mode 100644
index 00000000000..acd01089716
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/language/simple/InvokeCloneTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.language.simple;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class InvokeCloneTest extends ContextTestSupport  {
+
+    @Test
+    public void invokeClone() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        MyCloneBean source = new MyCloneBean(5);
+        template.sendBody("direct:startClone", source);
+
+        mock.assertIsSatisfied();
+
+        MyCloneBean target = mock.getReceivedExchanges().get(0).getMessage().getBody(MyCloneBean.class);
+        Assertions.assertNotNull(target);
+        Assertions.assertNotSame(source, target);
+        Assertions.assertEquals(source.getMyField(), target.getMyField());
+    }
+
+    @Test
+    public void invokeDeepCopy() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        MyCloneBean source = new MyCloneBean(5);
+        template.sendBody("direct:startDeepCopy", source);
+
+        mock.assertIsSatisfied();
+
+        MyCloneBean target = mock.getReceivedExchanges().get(0).getMessage().getBody(MyCloneBean.class);
+        Assertions.assertNotNull(target);
+        Assertions.assertNotSame(source, target);
+        Assertions.assertEquals(source.getMyField(), target.getMyField());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:startClone").to("language://simple:${body.clone()}").to("mock:result").end();
+
+                from("direct:startDeepCopy").to("language://simple:${body.deepCopy()}").to("mock:result").end();
+            }
+        };
+    }
+
+}
diff --git a/core/camel-core/src/test/java/org/apache/camel/language/simple/MyCloneBean.java b/core/camel-core/src/test/java/org/apache/camel/language/simple/MyCloneBean.java
new file mode 100644
index 00000000000..648ac09a227
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/language/simple/MyCloneBean.java
@@ -0,0 +1,47 @@
+/*
+ * 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.language.simple;
+
+public class MyCloneBean implements Cloneable {
+
+    private int myField;
+
+    public MyCloneBean() {
+    }
+
+    public MyCloneBean(int myField) {
+        this.myField = myField;
+    }
+
+    public MyCloneBean(MyCloneBean other) {
+        this.myField = other.myField;
+    }
+
+    public int getMyField() {
+        return myField;
+    }
+
+    @Override
+    public MyCloneBean clone() {
+        return new MyCloneBean(this);
+    }
+
+    public MyCloneBean deepCopy() {
+        return new MyCloneBean(this.myField);
+    }
+
+}