Literal array conversion of primitive arrays (#11500)

* [android] - literal array conversion of primitives types
This commit is contained in:
Tobrun
2018-03-22 12:40:43 -04:00
committed by GitHub
parent 4dc191b8da
commit 1eb90c1448
3 changed files with 39 additions and 1 deletions

View File

@@ -112,6 +112,9 @@ public class Expression {
* @return the expression
*/
public static Expression literal(@NonNull Object object) {
if (object.getClass().isArray()) {
return literal(ExpressionArray.toObjectArray(object));
}
return new ExpressionLiteral(object);
}
@@ -2033,6 +2036,11 @@ public class Expression {
};
}
/**
* Convert the expression array to a string representation.
*
* @return the string representation of the expression array
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder("[\"literal\"], [");
@@ -2053,4 +2061,20 @@ public class Expression {
return builder.toString();
}
}
/**
* Converts an object that is a primitive array to an Object[]
*
* @param object the object to convert to an object array
* @return the converted object array
*/
static Object[] toObjectArray(Object object) {
// object is a primitive array
int len = java.lang.reflect.Array.getLength(object);
Object[] objects = new Object[len];
for (int i = 0; i < len; i++) {
objects[i] = java.lang.reflect.Array.get(object, i);
}
return objects;
}
}

View File

@@ -1081,4 +1081,12 @@ public class ExpressionTest {
String actual = literal(array).toString();
assertEquals("literal array should match", expected, actual);
}
@Test
public void testLiteralPrimitiveArrayConversion() throws Exception {
float[] array = new float[] {0.2f, 0.5f};
Object[] expected = new Object[] {"literal", new Object[] {0.2f, 0.5f}};
Object[] actual = literal(array).toArray();
assertEquals("primitive array should be convered", expected, actual);
}
}