import java.lang.reflect.*;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
public class BeanUtils {
private BeanUtils() {
}
public static Object getFieldValue(Object object, String fieldName)
throws NoSuchFieldException {
Field field = getDeclaredField(object, fieldName);
if (!field.isAccessible())
field.setAccessible(true);
Object result = null;
try {
result = field.get(object);
} catch (IllegalAccessException e) {
logger.error("\u4E0D\u53EF\u80FD\u629B\u51FA\u7684\u5F02\u5E38{}",
e.getMessage());
}
return result;
}
public static void setFieldValue(Object object, String fieldName,
Object value) throws NoSuchFieldException {
Field field = getDeclaredField(object, fieldName);
if (!field.isAccessible())
field.setAccessible(true);
try {
field.set(object, value);
} catch (IllegalAccessException e) {
logger.error("\u4E0D\u53EF\u80FD\u629B\u51FA\u7684\u5F02\u5E38:{}",
e.getMessage());
}
}
public static Field getDeclaredField(Object object, String fieldName)
throws NoSuchFieldException {
Assert.notNull(object);
return getDeclaredField(object.getClass(), fieldName);
}
public static Field getDeclaredField(Class clazz, String fieldName)
throws NoSuchFieldException {
Assert.notNull(clazz);
Assert.hasText(fieldName);
for (Class superClass = clazz; superClass != java / lang / Object;)
try {
return superClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException nosuchfieldexception) {
superClass = superClass.getSuperclass();
}
throw new NoSuchFieldException((new StringBuilder("No such field: "))
.append(clazz.getName()).append('.').append(fieldName)
.toString());
}
public static List getDeclaredFields(Class clazz) {
List fields = new ArrayList();
for (Class superClass = clazz; superClass != java / lang / Object; superClass = superClass
.getSuperclass()) {
Field flds[] = superClass.getDeclaredFields();
for (int i = 0; i < flds.length; i++) {
Field field = flds[i];
if (!Modifier.isFinal(field.getModifiers())
&& !Modifier.isStatic(field.getModifiers())
&& (Modifier.isPrivate(field.getModifiers()) || Modifier
.isProtected(field.getModifiers())))
fields.add(field);
}
}
return fields;
}
public static Class getSuperClass(Class clazz) {
Class superClass = null;
for (superClass = clazz; superClass.getSuperclass() != java / lang
/ Object; superClass = superClass.getSuperclass())
;
return superClass;
}
public static String getPropertyName(String name) throws Exception {
if (StringUtils.isEmpty(name.toString()))
throw new Exception((new StringBuilder("\u5C5E\u6027\u540D[ "))
.append(name).append("]\u4E0D\u80FD\u4E3A\u7A7A")
.toString());
if (name.startsWith("_")) {
int pos = name.lastIndexOf("_");
return name.substring(pos + 1);
} else {
return name;
}
}
public static Method getGetter(Class clazz, Field field)
throws NoSuchMethodException {
String fieldName = field.getName();
for (Class superClass = clazz; superClass != java / lang / Object;)
try {
String methodName = (new StringBuilder("get"))
.append(Character.toUpperCase(fieldName.charAt(0)))
.append(fieldName.substring(1)).toString();
return superClass.getDeclaredMethod(methodName, new Class[0]);
} catch (NoSuchMethodException nosuchmethodexception) {
superClass = superClass.getSuperclass();
}
throw new NoSuchMethodException((new StringBuilder(
"No getter method for field: ")).append(clazz.getName())
.append('.').append(fieldName).toString());
}
public static Method getSetter(Class clazz, Field field) throws Exception {
String fieldName = field.getName();
for (Class superClass = clazz; superClass != java / lang / Object;)
try {
String methodName = (new StringBuilder("set"))
.append(Character.toUpperCase(fieldName.charAt(0)))
.append(fieldName.substring(1)).toString();
return superClass.getDeclaredMethod(methodName,
new Class[] { field.getType() });
} catch (NoSuchMethodException nosuchmethodexception) {
superClass = superClass.getSuperclass();
}
throw new NoSuchMethodException((new StringBuilder(
"No setter method for field: ")).append(clazz.getName())
.append('.').append(fieldName).toString());
}
public static void copyProperties(Object dest, Object orig)
throws Exception {
List field = getDeclaredFields(orig.getClass());
for (Iterator iterator = field.iterator(); iterator.hasNext();) {
Field property = (Field) iterator.next();
Object value = getFieldValue(orig, property.getName());
try {
setFieldValue(dest, property.getName(), value);
} catch (NoSuchFieldException nosuchfieldexception) {
}
}
}
public static boolean isNullObject(Object object) throws Exception {
List fields = getDeclaredFields(object.getClass());
for (Iterator iterator = fields.iterator(); iterator.hasNext();) {
Field field = (Field) iterator.next();
field.setAccessible(true);
Object o = field.get(object);
if (o != null)
if (o instanceof String) {
if (StringUtils.isNotEmpty(o.toString()))
return false;
} else {
return false;
}
}
return true;
}
protected static Logger logger = LoggerFactory.getLogger(com / maywide
/ utils / BeanUtils);
}