Java
copyProperties方法
- Spring的BeanUtils不会copy字段名相同但类型不同的属性
if (readMethod != null &&
ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
- apache的PropertyUtils::copyProperties若字段名相同但类型不同会抛
java.lang.IllegalArgumentException
异常 - apache的BeanUtils::copyProperties若字段名相同但类型不同会尝试转换类型后copy,
Throws:ConversionException - if conversion cannot be performed successfully
// Convert the specified value to the required type and store it
if (index >= 0) { // Destination must be indexed
value = convertForCopy(value, type.getComponentType());
try {
getPropertyUtils().setIndexedProperty(target, propName,
index, value);
} catch (final NoSuchMethodException e) {
throw new InvocationTargetException
(e, "Cannot set " + propName);
}
}
String::substring(startIndex, endIndex) 方法的endIndex参数若大于字符串长度会直接抛异常, 而并非认为取剩余的所有字符
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}
标签:endIndex,beginIndex,value,readMethod,debug,new,throw,小记
From: https://www.cnblogs.com/newbieking/p/18672298