You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2015/09/01 14:21:55 UTC

[2/2] camel git commit: Fix CS

Fix CS


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

Branch: refs/heads/master
Commit: 803d148883248c140209fc1c20b10686af3d1029
Parents: c53a5c8
Author: Jonathan Anstey <ja...@gmail.com>
Authored: Tue Sep 1 09:51:09 2015 -0230
Committer: Jonathan Anstey <ja...@gmail.com>
Committed: Tue Sep 1 09:51:09 2015 -0230

----------------------------------------------------------------------
 .../camel/component/dozer/CustomMapper.java     | 176 ++++++++++---------
 .../camel/component/dozer/DozerProducer.java    |   4 +-
 .../dozer/CustomMapperParametersTest.java       |  12 +-
 3 files changed, 102 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/803d1488/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 0807160..b26cd73 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
@@ -39,9 +39,9 @@ public class CustomMapper extends BaseConverter {
 
     @Override
     public Object convert(Object existingDestinationFieldValue,
-    					  Object sourceFieldValue,
-    					  Class<?> destinationClass,
-    					  Class<?> sourceClass) {
+                          Object sourceFieldValue,
+                          Class<?> destinationClass,
+                          Class<?> sourceClass) {
         try {
             return mapCustom(sourceFieldValue);
         } finally {
@@ -50,26 +50,26 @@ public class CustomMapper extends BaseConverter {
     }
 
     private Object invokeFunction(Method method,
-    							  Object customObj,
-    							  Object source,
-    							  String[][] parameters) throws Exception {
-		Class<?>[] prmTypes = method.getParameterTypes();
-		Object[] methodPrms = new Object[prmTypes.length];
-		methodPrms[0] = source;
-		for (int parameterNdx = 0, methodPrmNdx = 1; parameterNdx < parameters.length; parameterNdx++, methodPrmNdx++) {
-			if (method.isVarArgs() && methodPrmNdx == prmTypes.length - 1) {
-				Object array = Array.newInstance(prmTypes[methodPrmNdx].getComponentType(), parameters.length - parameterNdx);
-				for (int arrayNdx = 0; parameterNdx < parameters.length; parameterNdx++, arrayNdx++) {
-					String[] parts = parameters[parameterNdx];
-					Array.set(array, arrayNdx, resolver.resolveClass(parts[0]).getConstructor(String.class).newInstance(parts[1]));
-				}
-				methodPrms[methodPrmNdx] = array;
-			} else {
-				String[] parts = parameters[parameterNdx];
-				methodPrms[methodPrmNdx] = resolver.resolveClass(parts[0]).getConstructor(String.class).newInstance(parts[1]);;
-			}
-		}
-		return method.invoke(customObj, methodPrms);
+                                  Object customObj,
+                                  Object source,
+                                  String[][] parameters) throws Exception {
+        Class<?>[] prmTypes = method.getParameterTypes();
+        Object[] methodPrms = new Object[prmTypes.length];
+        methodPrms[0] = source;
+        for (int parameterNdx = 0, methodPrmNdx = 1; parameterNdx < parameters.length; parameterNdx++, methodPrmNdx++) {
+            if (method.isVarArgs() && methodPrmNdx == prmTypes.length - 1) {
+                Object array = Array.newInstance(prmTypes[methodPrmNdx].getComponentType(), parameters.length - parameterNdx);
+                for (int arrayNdx = 0; parameterNdx < parameters.length; parameterNdx++, arrayNdx++) {
+                    String[] parts = parameters[parameterNdx];
+                    Array.set(array, arrayNdx, resolver.resolveClass(parts[0]).getConstructor(String.class).newInstance(parts[1]));
+                }
+                methodPrms[methodPrmNdx] = array;
+            } else {
+                String[] parts = parameters[parameterNdx];
+                methodPrms[methodPrmNdx] = resolver.resolveClass(parts[0]).getConstructor(String.class).newInstance(parts[1]);
+            }
+        }
+        return method.invoke(customObj, methodPrms);
     }
 
     Object mapCustom(Object source) {
@@ -89,16 +89,20 @@ public class CustomMapper extends BaseConverter {
         // parameters = ["java.lang.Integer=3","java.lang.Integer=10"]
         String[][] prmTypesAndValues;
         if (prms.length > 2) {
-	    	// Break parameters down into types and values
-	    	prmTypesAndValues = new String[prms.length - 2][2];
-	    	for (int ndx = 0; ndx < prmTypesAndValues.length; ndx++) {
-	    		String prm = prms[ndx + 2];
-	    		String[] parts = prm.split("=");
-	    		if (parts.length != 2) throw new RuntimeException("Value missing for parameter " + prm);
-	    		prmTypesAndValues[ndx][0] = parts[0];
-	    		prmTypesAndValues[ndx][1] = parts[1];
-	    	}
-        } else prmTypesAndValues = null;
+            // Break parameters down into types and values
+            prmTypesAndValues = new String[prms.length - 2][2];
+            for (int ndx = 0; ndx < prmTypesAndValues.length; ndx++) {
+                String prm = prms[ndx + 2];
+                String[] parts = prm.split("=");
+                if (parts.length != 2) {
+                    throw new RuntimeException("Value missing for parameter " + prm);
+                }
+                prmTypesAndValues[ndx][0] = parts[0];
+                prmTypesAndValues[ndx][1] = parts[1];
+            }
+        } else {
+            prmTypesAndValues = null;
+        }
 
         Object customObj;
         Method method = null;
@@ -108,7 +112,7 @@ 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, source, prmTypesAndValues);
             } else if (operation != null) {
                 method = customClass.getMethod(operation, source.getClass());
             } else {
@@ -125,40 +129,46 @@ public class CustomMapper extends BaseConverter {
 
         // Invoke the custom mapping method
         try {
-        	if (prmTypesAndValues != null) {
-        		return invokeFunction(method, customObj, source, prmTypesAndValues);
-        	} else {
-        		return method.invoke(customObj, source);
-        	}
+            if (prmTypesAndValues != null) {
+                return invokeFunction(method, customObj, source, prmTypesAndValues);
+            } else {
+                return method.invoke(customObj, source);
+            }
         } catch (Exception e) {
             throw new RuntimeException("Error while invoking custom function", e);
         }
     }
 
     private boolean parametersMatchParameterList(Class<?>[] prmTypes,
-    											 String[][] parameters) {
-    	int ndx = 0;
-    	while (ndx < prmTypes.length) {
-    		Class<?> prmType = prmTypes[ndx];
-    		if (ndx >= parameters.length) return ndx == prmTypes.length - 1 && prmType.isArray();
-    		if (ndx == prmTypes.length - 1 && prmType.isArray()) { // Assume this only occurs for functions with var args
-    			Class<?> varArgClass = prmType.getComponentType();
-    			while (ndx < parameters.length) {
-    	    		Class<?> prmClass = resolver.resolveClass(parameters[ndx][0]);
-    	    		if (!varArgClass.isAssignableFrom(prmClass)) return false;
-    	    		ndx++;
-    			}
-    		} else {
-        		Class<?> prmClass = resolver.resolveClass(parameters[ndx][0]);
-        		if (!prmTypes[ndx].isAssignableFrom(prmClass)) return false;
-    		}
-    		ndx++;
-    	}
-    	return true;
+                                                 String[][] parameters) {
+        int ndx = 0;
+        while (ndx < prmTypes.length) {
+            Class<?> prmType = prmTypes[ndx];
+            if (ndx >= parameters.length) {
+                return ndx == prmTypes.length - 1 && prmType.isArray();
+            }
+            if (ndx == prmTypes.length - 1 && prmType.isArray()) { // Assume this only occurs for functions with var args
+                Class<?> varArgClass = prmType.getComponentType();
+                while (ndx < parameters.length) {
+                    Class<?> prmClass = resolver.resolveClass(parameters[ndx][0]);
+                    if (!varArgClass.isAssignableFrom(prmClass)) {
+                        return false;
+                    }
+                    ndx++;
+                }
+            } else {
+                Class<?> prmClass = resolver.resolveClass(parameters[ndx][0]);
+                if (!prmTypes[ndx].isAssignableFrom(prmClass)) {
+                    return false;
+                }
+            }
+            ndx++;
+        }
+        return true;
     }
 
     Method selectMethod(Class<?> customClass,
-    					Object source) {
+                        Object source) {
         Method method = null;
         for (Method m : customClass.getDeclaredMethods()) {
             if (m.getReturnType() != null
@@ -174,42 +184,44 @@ public class CustomMapper extends BaseConverter {
     // Assumes source is a separate parameter in method even if it has var args and that there are no
     // ambiguous calls based upon number and types of parameters
     private Method selectMethod(Class<?> customClass,
-    							String operation,
-    							Object source,
-    							String[][] parameters) {
-    	// Create list of potential methods
-    	List<Method> methods = new ArrayList<>();
+                                String operation,
+                                Object source,
+                                String[][] parameters) {
+        // Create list of potential methods
+        List<Method> methods = new ArrayList<>();
         for (Method method : customClass.getDeclaredMethods()) {
-        	methods.add(method);
+            methods.add(method);
         }
 
         // Remove methods that are not applicable
         for (Iterator<Method> iter = methods.iterator(); iter.hasNext();) {
-        	Method method = iter.next();
-    		Class<?>[] prmTypes = method.getParameterTypes();
+            Method method = iter.next();
+            Class<?>[] prmTypes = method.getParameterTypes();
             if (!method.getName().equals(operation)
-            		|| method.getReturnType() == null
-            		|| !prmTypes[0].isAssignableFrom(source.getClass())) {
-            	iter.remove();
-            	continue;
+                    || method.getReturnType() == null
+                    || !prmTypes[0].isAssignableFrom(source.getClass())) {
+                iter.remove();
+                continue;
             }
             prmTypes = Arrays.copyOfRange(prmTypes, 1, prmTypes.length); // Remove source from type list
-    		if (!method.isVarArgs() && prmTypes.length != parameters.length) {
-            	iter.remove();
-    			continue;
-    		}
-    		if (!parametersMatchParameterList(prmTypes, parameters)) {
-            	iter.remove();
-    			continue;
-    		}
+            if (!method.isVarArgs() && prmTypes.length != parameters.length) {
+                iter.remove();
+                continue;
+            }
+            if (!parametersMatchParameterList(prmTypes, parameters)) {
+                iter.remove();
+                continue;
+            }
         }
 
         // If more than one method is applicable, return the method whose prm list exactly matches the parameters
         // if possible
         if (methods.size() > 1) {
-	        for (Method method : methods) {
-	        	if (!method.isVarArgs()) return method;
-	        }
+            for (Method method : methods) {
+                if (!method.isVarArgs()) {
+                    return method;
+                }
+            }
         }
 
         return methods.size() > 0 ? methods.get(0) : null;

http://git-wip-us.apache.org/repos/asf/camel/blob/803d1488/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerProducer.java b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerProducer.java
index 5ae109c..b316197 100644
--- a/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerProducer.java
+++ b/components/camel-dozer/src/main/java/org/apache/camel/component/dozer/DozerProducer.java
@@ -54,7 +54,7 @@ public class DozerProducer extends DefaultProducer {
             LOG.debug("Unmarshalling input data using data format '{}'.", unmarshalId);
             resolveUnmarshaller(exchange, unmarshalId).process(exchange);
             if (exchange.getException() != null) {
-            	throw exchange.getException();
+                throw exchange.getException();
             }
         }
         
@@ -99,7 +99,7 @@ public class DozerProducer extends DefaultProducer {
             LOG.debug("Marshalling output data using data format '{}'.", marshalId);
             resolveMarshaller(exchange, marshalId).process(exchange);
             if (exchange.getException() != null) {
-            	throw exchange.getException();
+                throw exchange.getException();
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/803d1488/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 da3daec..eead8fe 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
@@ -48,15 +48,15 @@ public class CustomMapperParametersTest {
 class MapperWithMultiParmMethod {
 
     public Object add(String source, Integer... operands) {
-    	long sum = 0L;
-    	for (Integer operand : operands) {
-    		sum += operand;
-    	}
-    	return sum;
+        long sum = 0L;
+        for (Integer operand : operands) {
+            sum += operand;
+        }
+        return sum;
     }
 
     public Object test(String source, Integer beginindex, Integer endindex) {
-    	return source.substring(beginindex.intValue(), endindex.intValue());
+        return source.substring(beginindex.intValue(), endindex.intValue());
     }
 }