最近项目好难做啊!后台人员的活几乎都在前台,用 了一个前台框架kendo ui 里面对于js 的使用确实让我学到了不少的思想,对于js这方面的领悟更高了一层,但是对于json的操作实在是不喜欢,对于数据验证和安全这里特别不好做,还好用了spring作为后台框架,数据验证这一块视乎是简单不少,但是代码量也不少,所以自己应用反射技术写了一个进行bean属性的拷贝的功能,主要是因为要对json里面的内容存储数据库,又不想使用get 和set方法进行一个个的赋值
class User {
private int id;
private String name;
private boolean demo = false;
public boolean isDemo() {
return demo;
}
public void setDemo(boolean demo) {
this.demo = demo;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class BeanUtils<T,K> {
public static void main(String args[])
{
User user = new User();
User user1 = new User();
user1.setDemo(false);
user.setName("12");
user.setDemo(true);
user.setId(1);
user1.setName("21");
user1.setId(2);
BeanUtils<User,User> beanUtils = new BeanUtils<User,User>();
Set<String> stringSet = new HashSet<String>();
stringSet.add("id"); //设置不进行跟新的属性名称
System.out.println(user.getId());
System.out.println(user.getName());
System.out.println(user.isDemo());
try {
beanUtils.copyProperties(user1,user,stringSet);
System.out.println(user.getId());
System.out.println(user.getName());
System.out.println(user.isDemo());
} catch (InvocationTargetException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
/**
*
* t 源类
* k 需要进行属性更新的类
* */
public void copyProperties(T t,K k,Set<String> stringSet) throws InvocationTargetException, IllegalAccessException {
Class clazz = t.getClass();
Class target = k.getClass();
Field[] fields = clazz.getDeclaredFields(); //获取源类的所用字段
if (stringSet==null)
{
stringSet = new HashSet<String>();
}
for (Field field : fields) //遍历字段
{
String fieldName = field.getName(); //获取名称
if (stringSet.contains(fieldName)) //通过这个动态的指定需要更新的字段
continue;
Field targetField = null;
try {
targetField = target.getDeclaredField(fieldName); //根据源类的属性获取目标类的属性
} catch (NoSuchFieldException e) {
continue;
}
if (field.getType()==targetField.getType())
{
String getMethodName = null;
String setMethodName = null;
if (field.getType().getName()=="boolean")
{
getMethodName = "is"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
setMethodName = "set" + fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
} else
{
getMethodName = "get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
setMethodName = "set" + fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
}
Method getMethod = null; //源类的get方法
Method setMethod = null; //目标类的方法
try {
getMethod = clazz.getDeclaredMethod(getMethodName);
setMethod = target.getDeclaredMethod(setMethodName,field.getType());
Object value = getMethod.invoke(t); //通过get方法获得值
setMethod.invoke(k,value);
} catch (NoSuchMethodException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
}
//针对于list属性
public List<K> copyListProperties(List<T> list,Class clazz,List<K> newList,Set<String> stringSet)
{
Method[] methods = clazz.getMethods();
if(list==null)
return null;
for(T t : list)
{
Field[] fields = clazz.getDeclaredFields();
K object = null;
try {
object = (K)clazz.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
for(Field field : fields)
{
String newFieldName = field.getName();
if (stringSet.contains(newFieldName))
continue;
try {
Field oldField = t.getClass().getDeclaredField(field.getName());
} catch (NoSuchFieldException e) {
System.out.println(e.getMessage());
continue;
}
for(Method method : methods)
{
if(Modifier.isPublic(method.getModifiers()))//如果是公共的方法
{
String method_name = method.getName();
if(method_name.startsWith("set"))
{
String setMethodName = method.getName().substring(3).toLowerCase();
if(setMethodName.equals(newFieldName.toLowerCase()))
{
String getMethodName = "";
getMethodName = method.getName().substring(3);
String getName = "get"+getMethodName;
try {
method.invoke(object,t.getClass().getMethod(getName).invoke(t));
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
getMethodName = "is" + method.getName().substring(3);
try {
method.invoke(object,t.getClass().getMethod(getMethodName).invoke(t));
} catch (NoSuchMethodException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvocationTargetException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalAccessException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} catch (InvocationTargetException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
}
}
}
newList.add(object);
}
return newList;
}
}
完工 ,大概得作用相信大家应该都能看懂吧 ,看了看别人的自己改良了一些合适自己用的