대상 데이터 복사 처리
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
/**
*
* Title: common-util
*
*
* Description:
*
*/
public class BeanUtils {
private static Logger log = Logger.getLogger(BeanUtils.class);
/**
*
*
* @param base
* Object
* @param changed
* Object
* @return Object
*/
public static Object copyProperties(Object base, Object changed) {
Method m = null;
Method m2 = null;
// Method m3 = null;
Method tem = null;
Map map = null;
// Object te=base;
// Object testit=changed;
try {
map = new HashMap();
Class database = changed.getClass();
// Class database
// =Class.forName("com.gw.yxt.library.basebean.AbstractLibraryBooksort");
// changed=database.newInstance();
say(database.toString());
Class aa = base.getClass();
// Object changed=database.newInstance();
Method methlist[] = aa.getDeclaredMethods();
// Method methlist[] = aa.getClass().getDeclaredMethods();
Method methlist2[] = database.getSuperclass().getDeclaredMethods();
// Method methlist2[] = database.getClass().getDeclaredMethods();
Class partypes[] = new Class[0];
Class partypes2[] = new Class[1];
Object arglist[] = new Object[0];
Object arglist2[] = new Object[1];
// java.lang.reflect.Field[] fields=database.getDeclaredFields();
Class aaa = aa.getClass();
say("database.getClass()" + aa.getSuperclass() + ";;"
+ aaa.getSuperclass());
for (int ta = 0; ta < methlist2.length; ta++) {
tem = methlist2[ta];
if (tem != null) {
map.put(tem.getName(), tem.getName());
}
}
say("methlist.length" + methlist.length + ";;;;;;"
+ methlist2.length);
TT: for (int i = 0; i < methlist.length; i++) {
try {
m = methlist[i];
String classname = m.getName();
if (classname.indexOf("get") >= 0) {
m = aa.getMethod(classname, partypes);
Object sstem = m.invoke(base, arglist);
say(" GET = " + m.getName() + "; :=" + sstem);
arglist2[0] = sstem;
String temname = "s"
+ classname.substring(1, classname.length());
partypes2[0] = m.getReturnType();
if (map.get(temname) != null
&& !map.get(temname).equals("")) {
say(map.get(temname) + classname);
try {
// m2 = aa.getMethod(temname, partypes2);
m2 = database.getMethod(temname, partypes2);
say(m2.getName() + "---------");
// Field f=database.getField("writer");
// f.set(changed,"asdf");
if (arglist2[0] != null) {
if (partypes2[0] == String.class)
arglist2[0] = sstem.toString().trim(); //
if (partypes2[0] == String.class
&& arglist2[0].equals("")) {
// Do nothing.
} else
m2.invoke(changed, arglist2);
say("-------------" + m2.getName() + " !"
+ arglist2[0]);
}
} catch (Exception e) {
// log.error("", e);
continue TT;
}
}
}
} catch (NoSuchMethodException e) {
// log.error("", e);
}
}
} catch (Exception ex) {
// ex.printStackTrace();
log.error("", ex);
}
// say("================");
return changed;
}
public static Object invokeGetmethod(Object base, String methodname) {
Method m = null;
Object changed = null;
try {
Class aa = base.getClass();
// Method methlist[] = aa.getDeclaredMethods();
Class partypes[] = new Class[0];
// Class partypes2[] = new Class[1];
Object arglist[] = new Object[0];
say("=methlist.length;database.getClass()" + aa.getSuperclass()
+ ";;aa==" + aa.getName());
// for (int i = 0; i < methlist.length; i++) {
try {
// m = methlist[i];
// String classname = m.getName();
// if (classname.indexOf(methodname) >= 0) {
m = aa.getMethod(methodname, partypes);
changed = m.invoke(base, arglist);
say("111 GET = " + m.getName() + "; :=" + changed);
// }
} catch (NoSuchMethodException e) {
// log.error("", e);
}
// }
} catch (Exception ex) {
// log.error("", ex);
}
return changed;
}
public static Object invokeSetmethod(Object base, String methodname,
String value) {
Method m = null;
// Object changed=null;
try {
if (value == null || value.trim().equals(""))
return base;
Class aa = base.getClass();
// Method methlist[] = aa.getDeclaredMethods();
Class partypes[] = new Class[1];
// Class partypes2[] = new Class[1];
Object arglist[] = new Object[1];
say("database.getClass()" + aa.getSuperclass() + ";;");
// for (int i = 0; i < methlist.length; i++) {
// m = methlist[i];
// String classname = m.getName();
// if (classname.indexOf(methodname) >= 0) {
partypes[0] = String.class;
m = aa.getMethod(methodname, partypes);
arglist[0] = value.trim(); //
m.invoke(base, arglist);
say("-------------" + m.getName() + " !" + arglist[0]);
// }
// }
} catch (Exception ex) {
log.error("", ex);
}
return base;
}
// public static Object copyProperties(Object base, Object changed) {
//
// Method m = null;
// Method m2 = null;
// // Method m3 = null;
// Method tem = null;
// Map map = null;
// //Object te=base;
// // Object testit=changed;
// try {
// map = new HashMap();
// Class database = changed.getClass();
// //Class database
// =Class.forName("com.gw.yxt.library.basebean.AbstractLibraryBooksort");
// // changed=database.newInstance();
// say(database.toString());
// Class aa = base.getClass();
// // Object changed=database.newInstance();
// Method methlist[] = aa.getDeclaredMethods();
// // Method methlist[] = aa.getClass().getDeclaredMethods();
// Method methlist2[] = database.getSuperclass().getDeclaredMethods();
// // Method methlist2[] = database.getClass().getDeclaredMethods();
// Class partypes[] = new Class[0];
// Class partypes2[] = new Class[1];
// Object arglist[] = new Object[0];
// Object arglist2[] = new Object[1];
// // java.lang.reflect.Field[] fields=database.getDeclaredFields();
// Class aaa=aa.getClass();
// say("database.getClass()"+aa.getSuperclass()+";;"+aaa.getSuperclass());
// for (int ta = 0; ta < methlist2.length; ta++) {
// tem = methlist2[ta];
// if (tem != null) {
// map.put(tem.getName(), tem.getName());
// }
// }
// say("methlist.length" + methlist.length+";;;;;;"+methlist2.length);
// TT:for (int i = 0; i < methlist.length; i++) {
// try {
// m = methlist[i];
// String classname = m.getName();
// if (classname.indexOf("get") >= 0) {
// m = aa.getMethod(classname, partypes);
// Object sstem = m.invoke(base, arglist);
// say(" GET = " + m.getName()+"; :");
//
// arglist2[0] = sstem;
// String temname = "s" + classname.substring(1, classname.length());
// partypes2[0] = m.getReturnType();
//
// if (map.get(temname) != null && !map.get(temname).equals("")) {
// say(map.get(temname) + classname);
// try {
// //m2 = aa.getMethod(temname, partypes2);
// m2 = database.getMethod(temname, partypes2);
// say(m2.getName() + m2.getExceptionTypes() +
// "---------");
// //Field f=database.getField("writer");
// //f.set(changed,"asdf");
// if(arglist2[0]!=null){
//
// if(partypes2[0]==String.class)
// arglist2[0] = sstem.toString().trim(); //
// if(partypes2[0]==String.class&&arglist2[0].equals("")){
// //Do nothing.
// }else
// m2.invoke(changed, arglist2);
// say("-------------" + m2.getName() + " !" +
// arglist2[0]);}
// }
// catch (Exception e) {
// //e.printStackTrace();
// say(" " + m2.getName() + m2.getParameterTypes());
// continue TT;
// }
//
// }
// }
//
// }
// catch (NoSuchMethodException e) {
// say(" " + m.getName());
// }
// }
//
// }
// catch (Exception ex) {
// //ex.printStackTrace();
// say("UNKNOW ERROR!");
// }
// // say("================");
// return changed;
//
// }
public static void say(String temp) {
}
public static Field getDeclaredField(Object object, String propertyName)
throws NoSuchFieldException {
return getDeclaredField(object.getClass(), propertyName);
}
/**
*
*
* @param className
* @return
*/
public static String getProperty(String className) {
return BeanUtils.toLowerString(className.substring(className
.lastIndexOf(".") + 1, className.length()));
}
/**
* , DeclaredField.
*
* @throws NoSuchFieldException
* Field .
*/
public static Field getDeclaredField(Class clazz, String propertyName)
throws NoSuchFieldException {
for (Class superClass = clazz; superClass != Object.class; superClass = superClass
.getSuperclass()) {
try {
return superClass.getDeclaredField(propertyName);
} catch (NoSuchFieldException e) {
// Field ,
}
}
throw new NoSuchFieldException("No such field: " + clazz.getName()
+ '.' + propertyName);
}
public static List getDeclaredField(Object clazz) {
List list = new ArrayList();
for (Class superClass = clazz.getClass(); superClass != Object.class; superClass = superClass
.getSuperclass()) {
try {
Field[] fields = superClass.getDeclaredFields();
for (Field field : fields) {
list.add(field);
}
} catch (Exception e) {
}
}
return list;
}
/**
* , private,protected .
*
* @throws NoSuchFieldException
* Field .
*/
public static Object forceGetProperty(Object object, String propertyName)
throws NoSuchFieldException {
Field field = getDeclaredField(object, propertyName);
boolean accessible = field.isAccessible();
field.setAccessible(true);
Object result = null;
try {
result = field.get(object);
} catch (IllegalAccessException e) {
log.info("error wont' happen");
}
field.setAccessible(accessible);
return result;
}
/**
* , private,protected .
*
* @throws NoSuchFieldException
* Field .
*/
public static void forceSetProperty(Object object, String propertyName,
Object newValue) throws NoSuchFieldException {
Field field = getDeclaredField(object, propertyName);
boolean accessible = field.isAccessible();
field.setAccessible(true);
try {
field.set(object, newValue);
} catch (IllegalAccessException e) {
log.info("Error won't happen");
}
field.setAccessible(accessible);
}
/**
* , private,protected .
*
* @throws NoSuchMethodException
* Method .
*/
public static Object invokePrivateMethod(Object object, String methodName,
Object... params) throws NoSuchMethodException {
Class[] types = new Class[params.length];
for (int i = 0; i < params.length; i++) {
types[i] = params[i].getClass();
}
Class clazz = object.getClass();
Method method = null;
for (Class superClass = clazz; superClass != Object.class; superClass = superClass
.getSuperclass()) {
try {
method = superClass.getDeclaredMethod(methodName, types);
break;
} catch (NoSuchMethodException e) {
// ,
}
}
if (method == null)
throw new NoSuchMethodException("No Such Method:"
+ clazz.getSimpleName() + methodName);
boolean accessible = method.isAccessible();
method.setAccessible(true);
Object result = null;
try {
result = method.invoke(object, params);
} catch (Exception e) {
}
method.setAccessible(accessible);
return result;
}
/**
* Filed Field .
*/
public static List getFieldsByType(Object object, Class type) {
List list = new ArrayList();
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();
}
/**
* field getter .
*/
public static String getGetterName(Class type, String fieldName) {
if (type.getName().equals("boolean")) {
return "is" + StringUtils.capitalize(fieldName);
} else {
return "get" + StringUtils.capitalize(fieldName);
}
}
/**
* field getter , , null.
*/
public static Method getGetterMethod(Class type, String fieldName) {
try {
return type.getMethod(getGetterName(type, fieldName));
} catch (NoSuchMethodException e) {
log.error(e.getMessage(), e);
}
return null;
}
public static void copyMapToBean(Object bean, Map map) throws Exception {
String propirtes;//
String methodName;//
Method[] methods = bean.getClass().getDeclaredMethods();// bean set
for (Method method : methods) {
methodName = method.getName();
if (method.getName().contains("set")
&& method.getGenericParameterTypes().length > 0) {
// set
propirtes = methodName.substring(3);
propirtes = toLowerString(propirtes);
if (map.containsKey(propirtes) && map.get(propirtes) != null)// map
{
Class type = method.getParameterTypes()[0];//
Class value = map.get(propirtes).getClass();
if (type.getName().equals(value.getName()))//
bean.getClass().getMethod(methodName, type).invoke(
bean, map.get(propirtes));
}
}
}
}
/**
* bean value map, key,value map
*
* @param object
* @return
*/
public static Map getPropretiesByMap(Object object) {
Map map = new HashMap();
String propirtes;
Field[] fields = object.getClass().getDeclaredFields();
List list = new ArrayList();
for (Field field : fields)
list.add(field);
for (Field field : list) {
propirtes = field.getName();
Object ro = null;
try {
ro = forceGetProperty(object, propirtes);
// ro =
// object.getClass().getMethod("get"+convertString(propirtes),
// null).invoke(object, null);
map.put(propirtes, ro);
} catch (Exception e) {
e.printStackTrace();
}
}
return map;
}
/**
* bean value map, key,value map
*
* @param object
* @return
*/
public static Map getPropretiesByMapForPersents(
Object object) {
Map map = new HashMap();
String propirtes;
/*
* Field[] fields = object.getClass().getDeclaredFields(); Field[]
* supperField = object.getClass().getSuperclass()
* .getDeclaredFields();//
*/
List list = getDeclaredField(object);
/*
* for (Field field : fields) list.add(field); for (Field field :
* supperField) list.add(field);
*/
for (Field field : list) {
propirtes = field.getName();
Object ro = null;
try {
ro = forceGetProperty(object, propirtes);
/*
* ro = object.getClass().getMethod( "get" +
* convertString(propirtes), null).invoke(object, null);
*/
map.put(propirtes, ro);
} catch (Exception e) {
e.printStackTrace();
}
}
return map;
}
/**
*
*
* @param str
* @return
*/
public static String convertString(String str) {
String first = str.substring(0, 1);
first = first.toUpperCase();
return first + str.substring(1, str.length());
}
/**
*
*
* @param str
* @return
*/
public static String toLowerString(String str) {
String first = str.substring(0, 1);
first = first.toLowerCase();
return first + str.substring(1, str.length());
}
/**
* javabean properties null
*
* @param o
* @param properties
*/
public static void clearBeanProperties(Object o, String properties,
Object value) {
try {
Field field = o.getClass().getDeclaredField(properties);
Method method = o.getClass().getMethod(
"set" + convertString(properties), field.getType());
method.invoke(o, value);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* bean
*
* @param o
*/
public static void clearBeanAllProperties(Object o) {
try {
// Field[] fields = o.getClass().getDeclaredFields();
List fields = getDeclaredField(o);
String properties = null;
Object value = null;
for (Field field : fields) {
properties = field.getName();
// value=field.getType().newInstance();
if (field.getType().getName().equals("int")
|| field.getType().getName().equals("long")
|| field.getType().getName().equals("double")
|| field.getType().getName().equals("float"))
forceSetProperty(o, properties, 0);
// method.invoke(o, 0);
else
forceSetProperty(o, properties, null);
// method.invoke(o, value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* value null map
*
* @param map
* @return
*/
public static Map removeNullValue(Map map) {
Iterator it = map.keySet().iterator();
String key;
while (it.hasNext()) {
key = it.next();
if (map.get(key) == null || map.get(key).equals("")) {
it.remove();
map.remove(key);
}
}
return map;
}
/**
* bean
*
* @param bean
* @param properties
* @return
* @throws Exception
*/
public static Object newProperties(Object bean, String properties)
throws Exception {
Field field = bean.getClass().getDeclaredField(properties);
field.getName();
return Class.forName(field.getName()).newInstance();
}
/**
* javabean
*
* @param bean
* @return
*/
public static List getPropertyForBean(Object bean) {
List list = new ArrayList();
Field[] fields = bean.getClass().getDeclaredFields();
for (Field field : fields) {
String name = field.getName();
list.add(name);
}
return list;
}
/**
* bean
*
* @param bean
* @param properties
* @return
*/
public static boolean isProperties(Object bean, String properties) {
for (Field field : bean.getClass().getFields()) {
if (properties.equals(field.getName()))
return true;
}
return false;
}
public static Object getPropertyBygetMethod(Object bean, String property)
throws SecurityException, NoSuchMethodException,
IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
Object object = null;
Method method = bean.getClass().getMethod(
"get" + convertString(property), null);
object = method.invoke(bean, null);
return object;
}
public static Object setPropertyBygetMethod(Object bean, String property,
Object value) throws SecurityException, NoSuchMethodException,
IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
Object object = null;
Method method = bean.getClass().getMethod(
"set" + convertString(property), new Class[0]);
object = method.invoke(bean, value);
return object;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.