camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/2] git commit: CAMEL-6481: Added support for array length field in simple language.
Date Tue, 25 Jun 2013 08:48:19 GMT
CAMEL-6481: Added support for array length field in simple language.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ddeae97f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ddeae97f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ddeae97f

Branch: refs/heads/camel-2.11.x
Commit: ddeae97f7facde525f752b356cf78148ebaee40c
Parents: 1b33dee
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Tue Jun 25 10:41:59 2013 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Tue Jun 25 10:47:39 2013 +0200

----------------------------------------------------------------------
 .../org/apache/camel/component/bean/BeanInfo.java     | 14 ++++++++++++++
 .../main/java/org/apache/camel/util/ObjectHelper.java |  4 ++++
 .../org/apache/camel/language/simple/SimpleTest.java  | 10 ++++++++++
 3 files changed, 28 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ddeae97f/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
index a511943..59e9588 100644
--- a/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
+++ b/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
@@ -216,6 +216,20 @@ public class BeanInfo {
                 } catch (NoSuchMethodException e) {
                     throw new MethodNotFoundException(exchange, pojo, "getClass");
                 }
+            // special for length on an array type
+            } else if ("length".equals(name) && pojo.getClass().isArray()) {
+                try {
+                    // need to use arrayLength method from ObjectHelper as Camel's bean OGNL
support is method invocation based
+                    // and not for accessing fields. And hence we need to create a MethodInfo
instance with a method to call
+                    // and therefore use arrayLength from ObjectHelper to return the array
length field.
+                    Method method = ObjectHelper.class.getMethod("arrayLength", Object[].class);
+                    ParameterInfo pi = new ParameterInfo(0, Object[].class, null, ExpressionBuilder.mandatoryBodyExpression(Object[].class,
true));
+                    List<ParameterInfo> lpi = new ArrayList<ParameterInfo>(1);
+                    lpi.add(pi);
+                    methodInfo = new MethodInfo(exchange.getContext(), pojo.getClass(), method,
lpi, lpi, false, false);
+                } catch (NoSuchMethodException e) {
+                    throw new MethodNotFoundException(exchange, pojo, "getClass");
+                }
             } else {
                 List<MethodInfo> methods = getOperations(name);
                 if (methods != null && methods.size() == 1) {

http://git-wip-us.apache.org/repos/asf/camel/blob/ddeae97f/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
index b55a2bd..cec7fab 100644
--- a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
@@ -1120,6 +1120,10 @@ public final class ObjectHelper {
         return PRIMITIVE_ARRAY_TYPES.contains(clazz);
     }
 
+    public static int arrayLength(Object[] pojo) {
+        return pojo.length;
+    }
+
     /**
      * Converts primitive types such as int to its wrapper type like
      * {@link Integer}

http://git-wip-us.apache.org/repos/asf/camel/blob/ddeae97f/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index 8cf75d9..0dcba7f8 100644
--- a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -1234,6 +1234,16 @@ public class SimpleTest extends LanguageTestSupport {
         assertExpression("${type:org.apache.camel.ExchangePattern.UNKNOWN}", null);
     }
 
+    public void testStringArrayLength() throws Exception {
+        exchange.getIn().setBody(new String[]{"foo", "bar"});
+        assertExpression("${body[0]}", "foo");
+        assertExpression("${body[1]}", "bar");
+        assertExpression("${body.length}", 2);
+
+        exchange.getIn().setBody(new String[]{"foo", "bar", "beer"});
+        assertExpression("${body.length}", 3);
+    }
+
     protected String getLanguageName() {
         return "simple";
     }


Mime
View raw message