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 2020/10/30 09:02:48 UTC

[camel] branch master updated: CAMEL-15776: camel-core - Remove reflection in enum converter used as fallback

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 76708d0  CAMEL-15776: camel-core - Remove reflection in enum converter used as fallback
76708d0 is described below

commit 76708d044a34cb7bcb74c2da7087da95c9c28e48
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Oct 30 09:50:27 2020 +0100

    CAMEL-15776: camel-core - Remove reflection in enum converter used as fallback
---
 .../camel/impl/converter/EnumTypeConverter.java     | 21 ++++++---------------
 .../apache/camel/converter/EnumConverterTest.java   | 11 +++++++++--
 2 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/converter/EnumTypeConverter.java b/core/camel-base/src/main/java/org/apache/camel/impl/converter/EnumTypeConverter.java
index 6d51003..63e24cf 100644
--- a/core/camel-base/src/main/java/org/apache/camel/impl/converter/EnumTypeConverter.java
+++ b/core/camel-base/src/main/java/org/apache/camel/impl/converter/EnumTypeConverter.java
@@ -16,11 +16,8 @@
  */
 package org.apache.camel.impl.converter;
 
-import java.lang.reflect.Method;
-
 import org.apache.camel.Exchange;
-import org.apache.camel.RuntimeCamelException;
-import org.apache.camel.support.ObjectHelper;
+import org.apache.camel.TypeConversionException;
 import org.apache.camel.support.TypeConverterSupport;
 import org.apache.camel.util.StringHelper;
 
@@ -38,10 +35,10 @@ public class EnumTypeConverter extends TypeConverterSupport {
     public static <T> T doConvertTo(Class<T> type, Exchange exchange, Object value) {
         if (type.isEnum()) {
             String text = value.toString();
-            Class<Enum> enumClass = (Class<Enum>) type;
+            Class<Enum<?>> enumClass = (Class<Enum<?>>) type;
 
             // we want to match case insensitive for enums
-            for (Enum enumValue : enumClass.getEnumConstants()) {
+            for (Enum<?> enumValue : enumClass.getEnumConstants()) {
                 if (enumValue.name().equalsIgnoreCase(text)) {
                     return type.cast(enumValue);
                 }
@@ -49,21 +46,15 @@ public class EnumTypeConverter extends TypeConverterSupport {
 
             // add support for using dash or camel cased to common used upper cased underscore style for enum constants
             text = StringHelper.asEnumConstantValue(text);
-            for (Enum enumValue : enumClass.getEnumConstants()) {
+            for (Enum<?> enumValue : enumClass.getEnumConstants()) {
                 if (enumValue.name().equalsIgnoreCase(text)) {
                     return type.cast(enumValue);
                 }
             }
 
-            // fallback to the JDK valueOf which is case-sensitive and throws exception if not found
-            Method method;
-            try {
-                method = type.getMethod("valueOf", String.class);
-            } catch (NoSuchMethodException e) {
-                throw new RuntimeCamelException("Could not find valueOf method on enum type: " + type.getName());
-            }
-            return (T) ObjectHelper.invokeMethod(method, null, text);
+            throw new IllegalArgumentException("Enum class " + type + " does not have any constant with value: " + text);
         }
+
         return null;
     }
 
diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java b/core/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java
index bfdb60b..7a05e7c 100644
--- a/core/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java
@@ -23,8 +23,8 @@ import org.apache.camel.TypeConversionException;
 import org.apache.camel.support.DefaultExchange;
 import org.junit.jupiter.api.Test;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
+
+import static org.junit.jupiter.api.Assertions.*;
 
 public class EnumConverterTest extends ContextTestSupport {
 
@@ -60,6 +60,13 @@ public class EnumConverterTest extends ContextTestSupport {
     @Test
     public void testMandatoryConvertFailed() throws Exception {
         try {
+            LoggingLevel level = context.getTypeConverter().convertTo(LoggingLevel.class, "XXX");
+            fail("Should have thrown an exception");
+        } catch (TypeConversionException e) {
+            // expected
+        }
+
+        try {
             context.getTypeConverter().mandatoryConvertTo(LoggingLevel.class, "XXX");
             fail("Should have thrown an exception");
         } catch (TypeConversionException e) {