AMQProperties: Fix IllegalArgumentException when setting properties
IllegalArgumentException is raised:
Attempt to get java.lang.Integer field "<name>" with illegal data
type conversion to int
Instead of calling getInt() and friends, use the valueOf() methods on
the boxing classes with toString() on the value returned from the basic
get() method.
Change-Id: Iba95aba743bb76eeef5a193950de4f1b1f7c3e71
diff --git a/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/config/AMQProperties.java b/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/config/AMQProperties.java
index f38a9cc..ed892a0 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/config/AMQProperties.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/config/AMQProperties.java
@@ -47,23 +47,27 @@
if (f.isAnnotationPresent(MessageHeader.class)) {
MessageHeader mh = f.getAnnotation(MessageHeader.class);
try {
+ Object value = f.get(section);
+ if (value == null) {
+ continue;
+ }
switch(f.getType().getSimpleName()) {
case "String":
- headers.put(mh.value(), f.get(section).toString());
+ headers.put(mh.value(), value.toString());
break;
case "Integer":
- headers.put(mh.value(), f.getInt(section));
+ headers.put(mh.value(), Integer.valueOf(value.toString()));
break;
case "Long":
- headers.put(mh.value(), f.getLong(section));
+ headers.put(mh.value(), Long.valueOf(value.toString()));
break;
case "Boolean":
- headers.put(mh.value(), f.getBoolean(section));
+ headers.put(mh.value(), Boolean.valueOf(value.toString()));
break;
default:
break;
}
- } catch (IllegalAccessException ex) {
+ } catch (IllegalAccessException | IllegalArgumentException ex) {
LOGGER.warn("Cannot access field {}. Cause: {}",
f.getName(), ex.getMessage());
}