下面列出了org.apache.commons.lang3.ArrayUtils#nullToEmpty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* This method goes through the file structure and create an object model of the tests.
*
* @param file File object for the test folder.
* @param testScenario test scenario associated with the test
* @return a List of Test objects.
*/
private List<Test> processTestStructure(File file, TestScenario testScenario) throws TestAutomationException {
List<Test> testsList = new ArrayList<>();
File tests = new File(file.getAbsolutePath());
List<String> testNGList = new ArrayList<>();
if (tests.exists()) {
for (String testFile : ArrayUtils.nullToEmpty(tests.list())) {
if (testFile.endsWith(JAR_EXTENSION)) {
testNGList.add(Paths.get(tests.getAbsolutePath(), testFile).toString());
}
}
}
Collections.sort(testNGList);
Test test = new Test(file.getName(), TestEngine.TESTNG, testNGList, testScenario);
testsList.add(test);
return testsList;
}
/**
* 循环向上转型, 获取对象的DeclaredMethod, 并强制设置为可访问.
*
* 如向上转型到Object仍无法找到, 返回null.
*
* 匹配函数名+参数类型.
*
* 因为class.getMethod() 不能获取父类的private函数, 因此采用循环向上的getDeclaredMethod();
*/
public static Method getAccessibleMethod(final Class<?> clazz, final String methodName,
Class<?>... parameterTypes) {
Validate.notNull(clazz, "class can't be null");
Validate.notEmpty(methodName, "methodName can't be blank");
Class[] theParameterTypes = ArrayUtils.nullToEmpty(parameterTypes);
// 处理原子类型与对象类型的兼容
ClassUtil.wrapClassses(theParameterTypes);
for (Class<?> searchType = clazz; searchType != Object.class; searchType = searchType.getSuperclass()) {
try {
Method method = searchType.getDeclaredMethod(methodName, theParameterTypes);
ClassUtil.makeAccessible(method);
return method;
} catch (NoSuchMethodException e) {
// Method不在当前类定义,继续向上转型
}
}
return null;
}
private File[] listFiles(File dir, String namePattern) {
File[] newFolderFiles = homeDir.listFiles((FileFilter) fileToCheck -> {
if (!fileToCheck.isDirectory()) {
return false;
}
if (!fileToCheck.getName().startsWith(namePattern)) {
return false;
}
return true;
});
return ArrayUtils.nullToEmpty(newFolderFiles, File[].class);
}
public AionTxReceipt(byte[] rlp) {
RLPList params = RLP.decode2(rlp);
RLPList receipt = (RLPList) params.get(0);
RLPItem postTxStateRLP = (RLPItem) receipt.get(0);
RLPItem bloomRLP = (RLPItem) receipt.get(1);
RLPList logs = (RLPList) receipt.get(2);
RLPItem result = (RLPItem) receipt.get(3);
postTxState = ArrayUtils.nullToEmpty(postTxStateRLP.getRLPData());
bloomFilter = new Bloom(bloomRLP.getRLPData());
executionResult =
(executionResult = result.getRLPData()) == null
? EMPTY_BYTE_ARRAY
: executionResult;
energyUsed = ByteUtil.byteArrayToLong(receipt.get(4).getRLPData());
if (receipt.size() > 5) {
byte[] errBytes = receipt.get(5).getRLPData();
error = errBytes != null ? new String(errBytes, StandardCharsets.UTF_8) : "";
}
for (RLPElement log : logs) {
Log logInfo = LogUtility.decodeLog(log.getRLPData());
if (logInfo != null) {
logInfoList.add(logInfo);
}
}
rlpEncoded = rlp;
}
/**
* Construct a child InternalTransaction
*/
public InternalTransaction(byte[] parentHash, int deep, int index,
byte[] sendAddress, byte[] transferToAddress, long value, byte[] data, String note,
long nonce, Map<String, Long> tokenInfo) {
this.parentHash = parentHash.clone();
this.deep = deep;
this.index = index;
this.note = note;
this.sendAddress = ArrayUtils.nullToEmpty(sendAddress);
this.transferToAddress = ArrayUtils.nullToEmpty(transferToAddress);
if ("create".equalsIgnoreCase(note)) {
this.receiveAddress = ByteUtil.EMPTY_BYTE_ARRAY;
} else {
this.receiveAddress = ArrayUtils.nullToEmpty(transferToAddress);
}
// in this case, value also can represent a tokenValue when tokenId is not null, otherwise it is a gsc callvalue.
this.value = value;
this.data = ArrayUtils.nullToEmpty(data);
this.nonce = nonce;
this.hash = getHash();
// in a contract call contract case, only one value should be used. gsc or a token. can't be both. We should avoid using
// tokenValue in this case.
if (tokenInfo != null) {
this.tokenInfo.putAll(tokenInfo);
}
}
/**
* 直接调用对象方法, 无视private/protected修饰符.
*
* 根据传入参数的实际类型进行匹配
*/
public static <T> T invokeMethod(Object obj, String methodName, Object... args) {
Object[] theArgs = ArrayUtils.nullToEmpty(args);
final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs);
return (T) invokeMethod(obj, methodName, theArgs, parameterTypes);
}
public byte[] serialize() {
Validate.isTrue(isManaged(), "Cannot serialize unmanaged data"); // TODO: replace temp check with D2S.serialize(CharData)
return ArrayUtils.nullToEmpty(data);
}
/**
* 反射调用对象方法, 无视private/protected修饰符.
*
* 根据传入参数的实际类型进行匹配, 支持方法参数定义是接口,父类,原子类型等情况
*
* 性能较差,仅用于单次调用.
*/
public static <T> T invokeMethod(Object obj, String methodName, Object... args) {
Object[] theArgs = ArrayUtils.nullToEmpty(args);
final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs);
return invokeMethod(obj, methodName, theArgs, parameterTypes);
}
/**
* 反射调用对象方法, 无视private/protected修饰符.
*
* 根据传入参数的实际类型进行匹配, 支持方法参数定义是接口,父类,原子类型等情况
*
* 性能较差,仅用于单次调用.
*/
public static <T> T invokeMethod(Object obj, String methodName, Object... args) {
Object[] theArgs = ArrayUtils.nullToEmpty(args);
final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs);
return invokeMethod(obj, methodName, theArgs, parameterTypes);
}