camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-9710 Use sourceClass instead of source.getClass() to prevent NPE
Date Wed, 16 Mar 2016 07:11:29 GMT
Repository: camel
Updated Branches:
  refs/heads/master 337a67564 -> e5a1e585c


CAMEL-9710 Use sourceClass instead of source.getClass() to prevent NPE


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

Branch: refs/heads/master
Commit: e5a1e585cc8fc9655c7edfac52341ce287a2e725
Parents: 337a675
Author: Tomas Rohovsky <tomasrohovsky@seznam.cz>
Authored: Tue Mar 15 21:13:27 2016 +0100
Committer: Tomas Rohovsky <tomasrohovsky@seznam.cz>
Committed: Tue Mar 15 21:39:50 2016 +0100

----------------------------------------------------------------------
 .../camel/component/dozer/CustomMapper.java     | 24 ++++++++------------
 .../dozer/CustomMapperParametersTest.java       |  4 ++--
 .../camel/component/dozer/CustomMapperTest.java | 16 +++++++++----
 3 files changed, 23 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e5a1e585/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/CustomMapper.java
----------------------------------------------------------------------
diff --git a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/CustomMapper.java
b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/CustomMapper.java
index 72bc783..ded1c7a 100644
--- a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/CustomMapper.java
+++ b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/CustomMapper.java
@@ -43,7 +43,7 @@ public class CustomMapper extends BaseConverter {
                           Class<?> destinationClass,
                           Class<?> sourceClass) {
         try {
-            return mapCustom(sourceFieldValue);
+            return mapCustom(sourceFieldValue, sourceClass);
         } finally {
             done();
         }
@@ -72,11 +72,7 @@ public class CustomMapper extends BaseConverter {
         return method.invoke(customObj, methodPrms);
     }
 
-    Object mapCustom(Object source) {
-        if (source == null) {
-            return null;
-        }
-
+    Object mapCustom(Object source, Class<?> sourceClass) {
         // The converter parameter is stored in a thread local variable, so
         // we need to parse the parameter on each invocation
         // ex: custom-converter-param="org.example.MyMapping,map"
@@ -116,11 +112,11 @@ public class CustomMapper extends BaseConverter {
 
             // If a specific mapping operation has been supplied use that
             if (operation != null && prmTypesAndValues != null) {
-                method = selectMethod(customClass, operation, source, prmTypesAndValues);
+                method = selectMethod(customClass, operation, sourceClass, prmTypesAndValues);
             } else if (operation != null) {
-                method = customClass.getMethod(operation, source.getClass());
+                method = customClass.getMethod(operation, sourceClass);
             } else {
-                method = selectMethod(customClass, source);
+                method = selectMethod(customClass, sourceClass);
             }
         } catch (Exception e) {
             throw new RuntimeException("Failed to load custom function", e);
@@ -172,12 +168,12 @@ public class CustomMapper extends BaseConverter {
     }
 
     Method selectMethod(Class<?> customClass,
-                        Object source) {
+                        Class<?> sourceClass) {
         Method method = null;
         for (Method m : customClass.getDeclaredMethods()) {
             if (m.getReturnType() != null
                     && m.getParameterTypes().length == 1
-                    && m.getParameterTypes()[0].isAssignableFrom(source.getClass()))
{
+                    && m.getParameterTypes()[0].isAssignableFrom(sourceClass)) {
                 method = m;
                 break;
             }
@@ -189,7 +185,7 @@ public class CustomMapper extends BaseConverter {
     // ambiguous calls based upon number and types of parameters
     private Method selectMethod(Class<?> customClass,
                                 String operation,
-                                Object source,
+                                Class<?> sourceClass,
                                 String[][] parameters) {
         // Create list of potential methods
         List<Method> methods = new ArrayList<>();
@@ -203,7 +199,7 @@ public class CustomMapper extends BaseConverter {
             Class<?>[] prmTypes = method.getParameterTypes();
             if (!method.getName().equals(operation)
                     || method.getReturnType() == null
-                    || !prmTypes[0].isAssignableFrom(source.getClass())) {
+                    || !prmTypes[0].isAssignableFrom(sourceClass)) {
                 iter.remove();
                 continue;
             }
@@ -230,4 +226,4 @@ public class CustomMapper extends BaseConverter {
 
         return methods.size() > 0 ? methods.get(0) : null;
     }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/e5a1e585/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperParametersTest.java
----------------------------------------------------------------------
diff --git a/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperParametersTest.java
b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperParametersTest.java
index eead8fe..24fc8b5 100644
--- a/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperParametersTest.java
+++ b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperParametersTest.java
@@ -33,14 +33,14 @@ public class CustomMapperParametersTest {
     @Test
     public void shouldExecuteCustomFunctionWithArguments() throws Exception {
         customMapper.setParameter(MapperWithMultiParmMethod.class.getName() + ",test,java.lang.Integer=12,java.lang.Integer=20");
-        Object result = customMapper.mapCustom("JeremiahWasABullfrog");
+        Object result = customMapper.mapCustom("JeremiahWasABullfrog", String.class);
         Assert.assertEquals("Bullfrog", result);
     }
 
     @Test
     public void shouldExecuteCustomFunctionWithVariableArguments() throws Exception {
         customMapper.setParameter(MapperWithMultiParmMethod.class.getName() + ",add,java.lang.Integer=12,java.lang.Integer=20");
-        Object result = customMapper.mapCustom("JeremiahWasABullfrog");
+        Object result = customMapper.mapCustom("JeremiahWasABullfrog", String.class);
         Assert.assertEquals(32L, result);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/e5a1e585/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperTest.java
----------------------------------------------------------------------
diff --git a/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperTest.java
b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperTest.java
index 259a426..c7e3271 100644
--- a/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperTest.java
+++ b/components/camel-dozer/src/test/java/org/apache/camel/component/dozer/CustomMapperTest.java
@@ -35,12 +35,12 @@ public class CustomMapperTest {
     @Test
     public void selectMapperOneMethod() {
         customMapper.setParameter(MapperWithOneMethod.class.getName());
-        Assert.assertNotNull(customMapper.selectMethod(MapperWithOneMethod.class, "test"));
+        Assert.assertNotNull(customMapper.selectMethod(MapperWithOneMethod.class, String.class));
     }
     
     @Test
     public void selectMapperMultipleMethods() throws Exception {
-        Method selectedMethod = customMapper.selectMethod(MapperWithTwoMethods.class, new
B());
+        Method selectedMethod = customMapper.selectMethod(MapperWithTwoMethods.class, B.class);
         Assert.assertNotNull(selectedMethod);
         Assert.assertEquals(
                 MapperWithTwoMethods.class.getMethod("convertToA", B.class),
@@ -50,25 +50,31 @@ public class CustomMapperTest {
     @Test
     public void mapCustomFindOperation() throws Exception {
         customMapper.setParameter(MapperWithTwoMethods.class.getName());
-        Assert.assertNotNull(customMapper.mapCustom(new B()));
+        Assert.assertNotNull(customMapper.mapCustom(new B(), B.class));
     }
     
     @Test
     public void mapCustomDeclaredOperation() throws Exception {
         customMapper.setParameter(MapperWithTwoMethods.class.getName() + ",convertToA");
-        Assert.assertNotNull(customMapper.mapCustom(new B()));
+        Assert.assertNotNull(customMapper.mapCustom(new B(), B.class));
     }
     
     @Test
     public void mapCustomInvalidOperation() {
         customMapper.setParameter(MapperWithTwoMethods.class.getName() + ",convertToB");
         try {
-            customMapper.mapCustom(new B());
+            customMapper.mapCustom(new B(), B.class);
             Assert.fail("Invalid operation should result in exception");
         } catch (RuntimeException ex) {
             Assert.assertTrue(ex.getCause() instanceof NoSuchMethodException);
         }
     }
+
+    @Test
+    public void mapCustomNullField() throws Exception {
+        customMapper.setParameter(MapperWithTwoMethods.class.getName());
+        Assert.assertNotNull(customMapper.mapCustom(null, B.class));
+    }
 }
 
 class A {


Mime
View raw message