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 2024/04/25 14:25:37 UTC
(camel) branch main updated: CAMEL-20700: camel-core: ReflectionHelper.setField may fail for numeric type fields
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 760658259b8 CAMEL-20700: camel-core: ReflectionHelper.setField may fail for numeric type fields
760658259b8 is described below
commit 760658259b8e15984308fb5240b71bb342b39524
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Apr 25 16:25:24 2024 +0200
CAMEL-20700: camel-core: ReflectionHelper.setField may fail for numeric type fields
---
.../DefaultCamelBeanPostProcessorFieldFirstTest.java | 6 +++++-
.../java/org/apache/camel/util/ReflectionHelper.java | 16 +++++++---------
2 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelBeanPostProcessorFieldFirstTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelBeanPostProcessorFieldFirstTest.java
index 8e7b5df9d44..809bfd4c544 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelBeanPostProcessorFieldFirstTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultCamelBeanPostProcessorFieldFirstTest.java
@@ -67,10 +67,14 @@ public class DefaultCamelBeanPostProcessorFieldFirstTest extends ContextTestSupp
@PropertyInject("foo")
private String foo;
+ // should inject simple types first such as this property
+ @PropertyInject(value = "number", defaultValue = "123")
+ private Integer number;
+
@BindToRegistry("myCoolBean")
public MySerialBean myBean() {
MySerialBean myBean = new MySerialBean();
- myBean.setId(123);
+ myBean.setId(number);
myBean.setName(foo);
return myBean;
}
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/ReflectionHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/ReflectionHelper.java
index 649fca93bf2..1f89bf6845d 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/ReflectionHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/ReflectionHelper.java
@@ -21,8 +21,6 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
-import javax.swing.text.Document;
-
/**
* Helper for working with reflection on classes.
* <p/>
@@ -186,7 +184,7 @@ public final class ReflectionHelper {
}
// must use fine-grained for the correct type when setting a field value via reflection
Class<?> type = f.getType();
- if (boolean.class == type || Boolean.class == type) {
+ if (boolean.class == type) {
boolean val;
if (value instanceof Boolean) {
val = (boolean) value;
@@ -194,7 +192,7 @@ public final class ReflectionHelper {
val = Boolean.parseBoolean(value.toString());
}
f.setBoolean(instance, val);
- } else if (byte.class == type || Byte.class == type) {
+ } else if (byte.class == type) {
byte val;
if (value instanceof Byte) {
val = (byte) value;
@@ -202,7 +200,7 @@ public final class ReflectionHelper {
val = Byte.parseByte(value.toString());
}
f.setByte(instance, val);
- } else if (int.class == type || Integer.class == type) {
+ } else if (int.class == type) {
int val;
if (value instanceof Integer) {
val = (int) value;
@@ -210,7 +208,7 @@ public final class ReflectionHelper {
val = Integer.parseInt(value.toString());
}
f.setInt(instance, val);
- } else if (long.class == type || Long.class == type) {
+ } else if (long.class == type) {
long val;
if (value instanceof Long) {
val = (long) value;
@@ -218,7 +216,7 @@ public final class ReflectionHelper {
val = Long.parseLong(value.toString());
}
f.setLong(instance, val);
- } else if (float.class == type || Float.class == type) {
+ } else if (float.class == type) {
float val;
if (value instanceof Float) {
val = (float) value;
@@ -226,9 +224,9 @@ public final class ReflectionHelper {
val = Float.parseFloat(value.toString());
}
f.setFloat(instance, val);
- } else if (double.class == type || Double.class == type) {
+ } else if (double.class == type) {
double val;
- if (value instanceof Document) {
+ if (value instanceof Double) {
val = (double) value;
} else {
val = Double.parseDouble(value.toString());