[zb4osgi-changeset] [scm] ZigBee 4 OSGi repository change: r558 - in /projects/zb4osgi/trunk/zigbee.tester/src: main/java/org/persona/zigbee/tester/gui/Command.java test/java/org/persona/zigbee/tester/gui/CommandTest.java

scm-notify at zb4osgi.aaloa.org scm-notify at zb4osgi.aaloa.org
Tue Oct 23 15:32:52 CEST 2012


Author: stefano.lenzi
Date: Tue Oct 23 15:32:52 2012
New Revision: 558

Log:
Initial support for Array types ( refs #158 )


Modified:
    projects/zb4osgi/trunk/zigbee.tester/src/main/java/org/persona/zigbee/tester/gui/Command.java
    projects/zb4osgi/trunk/zigbee.tester/src/test/java/org/persona/zigbee/tester/gui/CommandTest.java

Modified: projects/zb4osgi/trunk/zigbee.tester/src/main/java/org/persona/zigbee/tester/gui/Command.java
==============================================================================
--- projects/zb4osgi/trunk/zigbee.tester/src/main/java/org/persona/zigbee/tester/gui/Command.java (original)
+++ projects/zb4osgi/trunk/zigbee.tester/src/main/java/org/persona/zigbee/tester/gui/Command.java Tue Oct 23 15:32:52 2012
@@ -26,6 +26,10 @@
 import it.cnr.isti.zigbee.ha.cluster.glue.Cluster;
 
 import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import javax.swing.text.StyledEditorKit.ItalicAction;
 
 /**
  * 
@@ -70,27 +74,113 @@
 		return types;
 	}
 	
-	public String invoke(String[] values) throws Exception {
+	
+	private boolean assignValueFromString(Class<?> clz, Object[] objs, int i, String value) {
+		objs[i] = null; //Emptying value
+		try {
+			if ( clz.isAssignableFrom( long.class ) ) objs[i] = Long.decode(value).longValue();
+			else if ( clz.isAssignableFrom( int.class ) )objs[i] = Integer.decode(value).intValue();
+			else if ( clz.isAssignableFrom( short.class ) ) objs[i] = Short.decode(value).shortValue();
+			else if ( clz.isAssignableFrom( byte.class ) ) objs[i] = Byte.decode(value).byteValue();
+			else if ( clz.isAssignableFrom( double.class ) ) objs[i] = Double.valueOf(value).doubleValue();
+			else if ( clz.isAssignableFrom( float.class ) ) objs[i] = Float.valueOf(value).floatValue();
+		}catch (NumberFormatException ex){
+			throw new CommandParsingException(value,i,"The parameter is a number and "+value+" does not reppresent a number", ex);
+		}			
+		if ( objs[i] != null) return true; //Data already assigned 
+		
+		if ( clz.isAssignableFrom( boolean.class ) ) objs[i] = Boolean.valueOf(value).booleanValue() || "on".equalsIgnoreCase(value) || "1".equals(value);
+		else if ( clz.isAssignableFrom( String.class ) ) objs[i] = value;
+		return objs[i] != null;
+	}
+	
+	private Object objectArrayToNativeArray(Class<?> nativeType,Object[] array, int length){
+		if ( nativeType == int.class ){
+			int[] result = new int[length];
+			for (int i = 0; i < result.length; i++) {
+				result[i] = (Integer) array[i];
+			}
+			return result;
+		}
+		if ( nativeType == long.class ){
+			long[] result = new long[length];
+			for (int i = 0; i < result.length; i++) {
+				result[i] = (Long) array[i];
+			}
+			return result;
+		}
+		if ( nativeType == short.class ){
+			short[] result = new short[length];
+			for (int i = 0; i < result.length; i++) {
+				result[i] = (Short) array[i];
+			}
+			return result;
+		}
+		if ( nativeType == byte.class ){
+			byte[] result = new byte[length];
+			for (int i = 0; i < result.length; i++) {
+				result[i] = (Byte) array[i];
+			}
+			return result;
+		}
+		if ( nativeType == boolean.class ){
+			boolean[] result = new boolean[length];
+			for (int i = 0; i < result.length; i++) {
+				result[i] = (Boolean) array[i];
+			}
+			return result;
+		}
+		if ( nativeType == float.class ){
+			float[] result = new float[length];
+			for (int i = 0; i < result.length; i++) {
+				result[i] = (Float) array[i];
+			}
+			return result;
+		}
+		if ( nativeType == double.class ){
+			double[] result = new double[length];
+			for (int i = 0; i < result.length; i++) {
+				result[i] = (Double) array[i];
+			}
+			return result;
+		}
+		throw new IllegalArgumentException("Unable to convert Object[] to "+nativeType+"[]");
+	}
+	
+	private <T> boolean assignArrayFromString(Class<? extends T[]> clz, Object[] objs, int target, String value) {
+		objs[target] = null;
+		Class<?> type = clz.getComponentType();
+		String[] slices = value.split("[,;]");
+		Object[] array = new Object[slices.length];
+		int idx = 0;
+		for (int j = 0; j < slices.length; j++) {
+			slices[j] = slices[j].trim();
+			if ( "".equals( slices[j] ) ) continue;
+			assignValueFromString(type, array, idx, slices[j]);
+			idx++;
+		}
+		objs[target] = objectArrayToNativeArray(type,array,idx);
+		// objs[target] = Arrays.copyOfRange(array, 0, idx, clz); It's not working
+		return objs[target] != null;
+	}	
+	
+	public <T> String invoke(String[] values) throws Exception {
 		Class<?>[] params = method.getParameterTypes();
 		Object[] objs = new Object[params.length];
 		for (int i = 0; i < objs.length; i++) {
-			try {
-				if ( params[i].isAssignableFrom( long.class ) ) objs[i] = Long.decode(values[i]).longValue();
-				else if ( params[i].isAssignableFrom( int.class ) )objs[i] = Integer.decode(values[i]).intValue();
-				else if ( params[i].isAssignableFrom( short.class ) ) objs[i] = Short.decode(values[i]).shortValue();
-				else if ( params[i].isAssignableFrom( byte.class ) ) objs[i] = Byte.decode(values[i]).byteValue();
-				else if ( params[i].isAssignableFrom( double.class ) ) objs[i] = Double.valueOf(values[i]).doubleValue();
-				else if ( params[i].isAssignableFrom( float.class ) ) objs[i] = Float.valueOf(values[i]).floatValue();
-			}catch (NumberFormatException ex){
-				throw new CommandParsingException(values[i],i,"The parameter is a number and "+values[i]+" does not reppresent a number", ex);
-			}			
-			if ( objs[i] != null) continue; //Data already assigned 
-			
-			if ( params[i].isAssignableFrom( boolean.class ) ) objs[i] = Boolean.valueOf(values[i]).booleanValue() || "on".equalsIgnoreCase(values[i]) || "1".equals(values[i]);
-			else if ( params[i].isAssignableFrom( String.class ) ) objs[i] = values[i];
-			//TODO Add an option for ignoring type that we cannot convert
-			//TODO Define a plugin system for enabling data conversion
-			else throw new CommandParsingException(values[i],i,"No convertion defined from "+String.class+" to argument of type "+params[i]); 
+			if ( params[i].isArray() ){
+				Class<? extends T[]> type = (Class<? extends T[]>) params[i];
+				assignArrayFromString(type, objs, i, values[i]);
+			} else {
+				assignValueFromString(params[i], objs, i, values[i]);
+			}
+			if ( objs[i] == null ){
+				throw new CommandParsingException(
+						values[i],
+						i,
+						"No convertion defined from "+String.class+" to argument of type "+params[i]
+				);
+			}
 		}
 		if( method.getReturnType() == void.class ) {
 			method.invoke(cluster, objs);
@@ -100,6 +190,7 @@
 		}
 	}
 	
+
 	public String getName() {
 		return cluster.getName()+"."+method.getName();
 	}

Modified: projects/zb4osgi/trunk/zigbee.tester/src/test/java/org/persona/zigbee/tester/gui/CommandTest.java
==============================================================================
--- projects/zb4osgi/trunk/zigbee.tester/src/test/java/org/persona/zigbee/tester/gui/CommandTest.java (original)
+++ projects/zb4osgi/trunk/zigbee.tester/src/test/java/org/persona/zigbee/tester/gui/CommandTest.java Tue Oct 23 15:32:52 2012
@@ -57,5 +57,25 @@
 		assertTrue(ex.getClass() == NullPointerException.class);
 		assertNull("Expecting empty message for standard JRE NPE", ex.getMessage());
 	}
+	
+	@Test
+	public void testInvokeWithArray() {
+		Exception ex = null;
+		try {
+			//TODO We should use Mocking for better error handling rather then using the exception type
+			Command command = new Command(
+					new GroupsImpl(null), 
+					GroupsImpl.class.getMethod("getGroupMembership", int[].class)
+			);
+			command.invoke(new String[]{"100,200;300, 400; 500 "});
+		} catch (Exception e) {
+			e.printStackTrace();
+			assertTrue("Expected exception InvocationTargetException", e.getClass() == InvocationTargetException.class);
+			ex = (Exception) ((InvocationTargetException) e).getTargetException();
+		}
+		assertNotNull(ex);
+		assertTrue(ex.getClass() == NullPointerException.class);
+		assertNull("Expecting empty message for standard JRE NPE", ex.getMessage());
+	}	
 
 }




More information about the Commit mailing list