
io.nuls.sdk.contract.ContractUtil Maven / Gradle / Ivy
/**
* MIT License
*
* Copyright (c) 2017-2018 nuls.io
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.nuls.sdk.contract;
import io.nuls.sdk.core.utils.StringUtils;
import java.lang.reflect.Array;
import java.util.ArrayList;
/**
* @author: PierreLuo
*/
public class ContractUtil {
private static final String STRING = "String";
public static String[][] twoDimensionalArray(Object[] args, String[] types) {
if (args == null) {
return null;
} else {
int length = args.length;
String[][] two = new String[length][];
Object arg;
for (int i = 0; i < length; i++) {
arg = args[i];
if(arg == null) {
two[i] = new String[0];
continue;
}
if(arg instanceof String) {
String argStr = (String) arg;
// 非String类型参数,若传参是空字符串,则赋值为空一维数组,避免数字类型转化异常 -> 空字符串转化为数字
if(types != null && StringUtils.isBlank(argStr) && !STRING.equalsIgnoreCase(types[i])) {
two[i] = new String[0];
} else {
two[i] = new String[]{argStr};
}
} else if(arg.getClass().isArray()) {
int len = Array.getLength(arg);
String[] result = new String[len];
for(int k = 0; k < len; k++) {
result[k] = valueOf(Array.get(arg, k));
}
two[i] = result;
} else if(arg instanceof ArrayList) {
ArrayList resultArg = (ArrayList) arg;
int size = resultArg.size();
String[] result = new String[size];
for(int k = 0; k < size; k++) {
result[k] = valueOf(resultArg.get(k));
}
two[i] = result;
} else {
two[i] = new String[]{valueOf(arg)};
}
}
return two;
}
}
public static String[][] twoDimensionalArray(Object[] args) {
return twoDimensionalArray(args, null);
}
public static String valueOf(Object obj) {
return (obj == null) ? null : obj.toString();
}
}