📄 beanutils.java
字号:
package org.langsin.core.utils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
/**
* 扩展的BeanUtils类。提供BeanUtils没有的功能
*
* @author 不落的太阳
*
*/
public class BeanUtils extends org.apache.commons.beanutils.BeanUtils {
protected static final Log logger = LogFactory.getLog(BeanUtils.class);
private BeanUtils() {
}
/**
* 循环向上转型,获取对象的DeclaredField.
*
* @throws NoSuchFieldException
* 如果没有该Field时抛出.
*/
public static Field getDeclaredField(Object object, String propertyName)
throws NoSuchFieldException {
Assert.notNull(object);
Assert.hasText(propertyName);
return getDeclaredField(object.getClass(), propertyName);
}
/**
* 循环向上转型,获取对象的DeclaredField.
*
* @throws NoSuchFieldException
* 如果没有该Field时抛出.
*/
public static Field getDeclaredField(Class clazz, String propertyName)
throws NoSuchFieldException {
Assert.notNull(clazz);
Assert.hasText(propertyName);
for (Class superClass = clazz; superClass != Object.class; superClass = superClass
.getSuperclass()) {
try {
return superClass.getDeclaredField(propertyName);
} catch (NoSuchFieldException e) {
}
}
throw new NoSuchFieldException("No such field: " + clazz.getName()
+ '.' + propertyName);
}
/**
* 强制获取对象变量值
*
* @param object
* @param propertyName
* @return
* @throws NoSuchFieldException
*/
public static Object forceGetProperty(Object object, String propertyName)
throws NoSuchFieldException {
Assert.notNull(object);
Assert.hasText(propertyName);
Field field = getDeclaredField(object, propertyName);
boolean accessible = field.isAccessible();
field.setAccessible(true);
Object result = null;
try {
result = field.get(object);
} catch (IllegalAccessException e) {
logger.info("error wont' happen");
}
field.setAccessible(accessible);
return result;
}
/**
* 强制设置对象变量值
*
* @param object
* @param propertyName
* @param newValue
* @throws NoSuchFieldException
*/
public static void forceSetProperty(Object object, String propertyName,
Object newValue) throws NoSuchFieldException {
Assert.notNull(object);
Assert.hasText(propertyName);
Field field = getDeclaredField(object, propertyName);
boolean accessible = field.isAccessible();
field.setAccessible(true);
try {
field.set(object, newValue);
} catch (IllegalAccessException e) {
}
field.setAccessible(accessible);
}
/**
* 按Field类型取得Field列表
*
* @param object
* @param type
* @return
*/
public static List<Field> getFieldsByType(Object object, Class type) {
List<Field> list = new ArrayList<Field>();
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.getType().isAssignableFrom(type)) {
list.add(field);
}
}
return list;
}
/**
* 按FiledName获得Field的类型.
*/
public static Class getPropertyType(Class type, String name)
throws NoSuchFieldException {
return getDeclaredField(type, name).getType();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -