camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [camel] branch master updated: CAMEL-12847: IntrospectionSupport - Allow to use dash style naming
Date Mon, 01 Oct 2018 16:14:10 GMT
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 b2798f3  CAMEL-12847: IntrospectionSupport - Allow to use dash style naming
b2798f3 is described below

commit b2798f33c9ed9dc8a504bb07f11d00265932214b
Author: Claus Ibsen <claus.ibsen@gmail.com>
AuthorDate: Mon Oct 1 18:13:04 2018 +0200

    CAMEL-12847: IntrospectionSupport - Allow to use dash style naming
---
 .../apache/camel/util/IntrospectionSupport.java    | 10 ++---
 .../java/org/apache/camel/util/StringHelper.java   | 43 ++++++++++++++++++++++
 .../camel/util/IntrospectionSupportTest.java       | 17 +++++++++
 .../org/apache/camel/util/StringHelperTest.java    | 11 ++++++
 4 files changed, 76 insertions(+), 5 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java b/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java
index a552b11..0e52272 100644
--- a/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java
+++ b/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java
@@ -387,14 +387,14 @@ public final class IntrospectionSupport {
 
     public static Method getPropertyGetter(Class<?> type, String propertyName) throws
NoSuchMethodException {
         if (isPropertyIsGetter(type, propertyName)) {
-            return type.getMethod("is" + StringHelper.capitalize(propertyName));
+            return type.getMethod("is" + StringHelper.capitalize(propertyName, true));
         } else {
-            return type.getMethod("get" + StringHelper.capitalize(propertyName));
+            return type.getMethod("get" + StringHelper.capitalize(propertyName, true));
         }
     }
 
     public static Method getPropertySetter(Class<?> type, String propertyName) throws
NoSuchMethodException {
-        String name = "set" + StringHelper.capitalize(propertyName);
+        String name = "set" + StringHelper.capitalize(propertyName, true);
         for (Method method : type.getMethods()) {
             if (isSetter(method) && method.getName().equals(name)) {
                 return method;
@@ -405,7 +405,7 @@ public final class IntrospectionSupport {
 
     public static boolean isPropertyIsGetter(Class<?> type, String propertyName) {
         try {
-            Method method = type.getMethod("is" + StringHelper.capitalize(propertyName));
+            Method method = type.getMethod("is" + StringHelper.capitalize(propertyName, true));
             if (method != null) {
                 return method.getReturnType().isAssignableFrom(boolean.class) || method.getReturnType().isAssignableFrom(Boolean.class);
             }
@@ -688,7 +688,7 @@ public final class IntrospectionSupport {
         Set<Method> candidates = new LinkedHashSet<>();
 
         // Build the method name.
-        name = "set" + StringHelper.capitalize(name);
+        name = "set" + StringHelper.capitalize(name, true);
         while (clazz != Object.class) {
             // Since Object.class.isInstance all the objects,
             // here we just make sure it will be add to the bottom of the set.
diff --git a/camel-core/src/main/java/org/apache/camel/util/StringHelper.java b/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
index 0f7e0b4..c5731d1 100644
--- a/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
@@ -386,6 +386,20 @@ public final class StringHelper {
      * @return the string capitalized (upper case first character)
      */
     public static String capitalize(String text) {
+        return capitalize(text, false);
+    }
+
+    /**
+     * Capitalize the string (upper case first character)
+     *
+     * @param text  the string
+     * @param dashToCamelCase whether to also convert dash format into camel case (hello-great-world
-> helloGreatWorld)
+     * @return the string capitalized (upper case first character)
+     */
+    public static String capitalize(String text, boolean dashToCamelCase) {
+        if (dashToCamelCase) {
+            text = dashToCamelCase(text);
+        }
         if (text == null) {
             return null;
         }
@@ -401,6 +415,35 @@ public final class StringHelper {
     }
 
     /**
+     * Converts the string from dash format into camel case (hello-great-world -> helloGreatWorld)
+     *
+     * @param text  the string
+     * @return the string camel cased
+     */
+    public static String dashToCamelCase(String text) {
+        if (text == null) {
+            return null;
+        }
+        int length = text.length();
+        if (length == 0) {
+            return text;
+        }
+
+        StringBuilder sb = new StringBuilder();
+
+        for (int i = 0; i < text.length(); ++i) {
+            char c = text.charAt(i);
+            if (c == '-') {
+                ++i;
+                sb.append(Character.toUpperCase(text.charAt(i)));
+            } else {
+                sb.append(c);
+            }
+        }
+        return sb.toString();
+    }
+
+    /**
      * Returns the string after the given token
      *
      * @param text  the text
diff --git a/camel-core/src/test/java/org/apache/camel/util/IntrospectionSupportTest.java
b/camel-core/src/test/java/org/apache/camel/util/IntrospectionSupportTest.java
index 0b88a99..e33c0b1 100644
--- a/camel-core/src/test/java/org/apache/camel/util/IntrospectionSupportTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/IntrospectionSupportTest.java
@@ -272,6 +272,22 @@ public class IntrospectionSupportTest extends ContextTestSupport {
     }
 
     @Test
+    public void testSetPropertyDash() throws Exception {
+        AnotherExampleBean bean = new AnotherExampleBean();
+        bean.setName("Claus");
+        bean.setPrice(10.0);
+        Date date = new Date(0);
+        bean.setDate(date);
+        bean.setGoldCustomer(true);
+        bean.setLittle(true);
+
+        IntrospectionSupport.setProperty(bean, "name", "James");
+        IntrospectionSupport.setProperty(bean, "gold-customer", "false");
+        assertEquals("James", bean.getName());
+        assertEquals(false, bean.isGoldCustomer());
+    }
+
+    @Test
     public void testAnotherGetProperty() throws Exception {
         AnotherExampleBean bean = new AnotherExampleBean();
         bean.setName("Claus");
@@ -288,6 +304,7 @@ public class IntrospectionSupportTest extends ContextTestSupport {
         assertSame(date, IntrospectionSupport.getProperty(bean, "date"));
         assertSame(children, IntrospectionSupport.getProperty(bean, "children"));
         assertEquals(Boolean.TRUE, IntrospectionSupport.getProperty(bean, "goldCustomer"));
+        assertEquals(Boolean.TRUE, IntrospectionSupport.getProperty(bean, "gold-customer"));
         assertEquals(Boolean.TRUE, IntrospectionSupport.getProperty(bean, "little"));
     }
 
diff --git a/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java b/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
index 61ed566..d0a87a6 100644
--- a/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
@@ -292,4 +292,15 @@ public class StringHelperTest extends Assert {
     public void testHumanReadableBytesDefaultLocale() {
         assertNotNull(StringHelper.humanReadableBytes(110592));
     }
+
+    @Test
+    public void testCapitalizeDash() {
+        assertEquals(null, StringHelper.dashToCamelCase(null));
+        assertEquals("", StringHelper.dashToCamelCase(""));
+        assertEquals("hello", StringHelper.dashToCamelCase("hello"));
+        assertEquals("helloGreat", StringHelper.dashToCamelCase("helloGreat"));
+        assertEquals("helloGreat", StringHelper.dashToCamelCase("hello-great"));
+        assertEquals("helloGreatWorld", StringHelper.dashToCamelCase("hello-great-world"));
+    }
+
 }


Mime
View raw message