blob: 5bb73b88cf2eb665ab6b7b4e3c51b9a6ae5f7652 [file] [log] [blame]
/*
* Copyright (c) Robert Bosch GmbH. All rights reserved.
*/
package org.eclipse.blockchain.core;
import java.math.BigInteger;
import org.web3j.utils.Numeric;
/**
* @author ADG5COB
*/
public class SolidityDynamicValueCaster {
private SolidityDynamicValueCaster() {}
/**
* This method is used to perform dynamic value casting of params that should be used while invvoking smart contract
* methods
*
* @param valueToCast - The String value obtained from the user
* @param type - The actual type to which the object should be casted to
* @return - Casted value
*/
public static Object castValue(final String valueToCast, final Class<?> type) {
/**
* Boolean Data types
*/
if (type.getName().equals(Boolean.class.getName())) {
return Boolean.parseBoolean(valueToCast);
}
else if (type.getName().equals(Byte.class.getName())) {
return Byte.parseByte(valueToCast);
}
else if (type.getName().equals(Short.class.getName())) {
return Short.parseShort(valueToCast);
}
else if (type.getName().equals(Integer.class.getName())) {
return Integer.parseInt(valueToCast);
}
else if (type.getName().equals(Long.class.getName())) {
return Long.parseLong(valueToCast);
}
else if (type.getName().equals(Float.class.getName())) {
return Float.parseFloat(valueToCast);
}
else if (type.getName().equals(Double.class.getName())) {
return Double.parseDouble(valueToCast);
}
else if (type.getName().equals(String.class.getName())) {
return valueToCast;
}
else if (type.getName().equals(Character.class)) {
return Character.valueOf(valueToCast.charAt(0));
}
else if (type.getName().equals(BigInteger.class.getName())) {
/**
* Non-Primitive Data types
*/
return new BigInteger(valueToCast);
}
else {
return null;
}
}
/**
* @param valueToCast - This is a comma separated value
* @param type - The class type to cast to
* @param dimensionDepth - The level of array
* @return - array
*/
public static Object castArrayArgs(final String valueToCast, final Class<?> type) {
Object obj = null;
MultidimensionalArrayValueCreator multidimensionalArrayValueCreator = new MultidimensionalArrayValueCreator();
String values = valueToCast.split(":")[1];
String dimensions = valueToCast.split(":")[0];
if (type.getName().equals("byte")) {// only for this list of byte array - multidimensional array in this has to be
// checked
String[] vals = values.split(",");
Object[] byteObjects = new Object[vals.length];
for (int i = 0; i < vals.length; i++) {
byteObjects[i] = getByteArray(vals[i]);
}
obj = multidimensionalArrayValueCreator.getValuesAsList(byteObjects, dimensions);
}
else {
obj = multidimensionalArrayValueCreator.getValuesAsList(getArray(type, values), dimensions);
}
return obj;
}
/**
* @param value
* @return
*/
public static byte[] getByteArray(final String value) {
return Numeric.hexStringToByteArray(value);
}
private static Object[] getArray(final Class<?> classType, final String values) {
Object[] objArray = null;
String[] vals = values.split(",");
String type = classType.getName();
if (type.contains("Short")) {
Short[] sh = new Short[vals.length];
for (int i = 0; i < vals.length; i++) {
sh[i] = (Short) castValue(vals[i], classType);
}
objArray = sh;
}
else if (type.contains("BigInteger")) {
BigInteger[] bg = new BigInteger[vals.length];
for (int i = 0; i < vals.length; i++) {
bg[i] = (BigInteger) castValue(vals[i], classType);
}
objArray = bg;
}
else if (type.contains("Integer")) {
Integer[] it = new Integer[vals.length];
for (int i = 0; i < vals.length; i++) {
it[i] = (Integer) castValue(vals[i], classType);
}
objArray = it;
}
else if (type.contains("Long")) {
Long[] lg = new Long[vals.length];
for (int i = 0; i < vals.length; i++) {
lg[i] = (Long) castValue(vals[i], classType);
}
objArray = lg;
}
else if (type.contains("Float")) {
Float[] ft = new Float[vals.length];
for (int i = 0; i < vals.length; i++) {
ft[i] = (Float) castValue(vals[i], classType);
}
objArray = ft;
}
else if (type.contains("Double")) {
Double[] db = new Double[vals.length];
for (int i = 0; i < vals.length; i++) {
db[i] = (Double) castValue(vals[i], classType);
}
objArray = db;
}
else if (type.contains("String")) {
String[] st = new String[vals.length];
for (int i = 0; i < vals.length; i++) {
st[i] = (String) castValue(vals[i], classType);
}
objArray = st;
}
else if (type.contains("Boolean")) {
Boolean[] bool = new Boolean[vals.length];
for (int i = 0; i < vals.length; i++) {
bool[i] = (Boolean) castValue(vals[i], classType);
}
objArray = bool;
}
else if (type.contains("Character")) {
Character[] chars = new Character[vals.length];
for (int i = 0; i < vals.length; i++) {
chars[i] = (Character) castValue(vals[i], classType);
}
}
return objArray;
}
}