package com.ycxc.vmts.common.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MethodTest {
/**
* 添加创建用户,创建时间,修改用户,修改时间(单参数)
* @param userId 用户id
* @param obj 类
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
* @throws ParseException
*/
public static void setIdTime(String userId,Object obj) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ParseException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date();
String str=sdf.format(date);
String ab ="setCreateId("+userId+"),setCreateTime("+str+"),setUpdateId("+userId+"),setUpdateTime("+str+")";
String[] a = ab.split(",");
for(int l=0;l<a.length;l++){
//拆解方法字符串,找出方法名
String methodName = a[l].substring(0, a[l].indexOf('('));
//找出参数
String paramStr = a[l].substring(a[l].indexOf('(') + 1,a[l].indexOf(')'));
Class c = obj.getClass();
if(methodName.indexOf("Time")>0){
Method method = c.getMethod(methodName, Date.class);
SimpleDateFormat sdfDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟
Date dateString=sdfDate.parse(paramStr);
method.invoke(obj, dateString);
}else{
Method method = c.getMethod(methodName, String.class);
method.invoke(obj, paramStr);
}
}
}
/**
* 添加创建用户,创建时间,创建用户名,修改用户名,修改用户,修改时间(单参数)
* @param userId 用户id
* @param obj 类
* @param userName 用户名称
* @param setGetName 添加用户的方法名
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
* @throws ParseException
*/
public static void setIdTimeName(String userId,String userName,String setGetName,Object obj) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ParseException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date();
String str=sdf.format(date);
String ab ="setCreateId("+userId+"),setCreateTime("+str+"),setCreate"+setGetName+"("+userName+"),setUpdateId("+userId+"),setUpdateTime("+str+"),setUpdate"+setGetName+"("+userName+")";
String[] a = ab.split(",");
for(int l=0;l<a.length;l++){
//拆解方法字符串,找出方法名
String methodName = a[l].substring(0, a[l].indexOf('('));
//找出参数
String paramStr = a[l].substring(a[l].indexOf('(') + 1,a[l].indexOf(')'));
Class c = obj.getClass();
if(methodName.indexOf("Time")>0){
Method method = c.getMethod(methodName, Date.class);
SimpleDateFormat sdfDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟
Date dateString=sdfDate.parse(paramStr);
method.invoke(obj, dateString);
}else{
Method method = c.getMethod(methodName, String.class);
method.invoke(obj, paramStr);
}
}
}
/**
* 添加创建用户,创建时间(单参数)
* @param userId 用户id
* @param obj 类
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
* @throws ParseException
*/
public static void setCreate(String userId,Object obj) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ParseException {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=new Date();
String str=sdf.format(date);
String ab ="setCreateId("+userId+"),setCreateTime("+str+")";
String[] a = ab.split(",");
for(int l=0;l<a.length;l++){
//拆解方法字符串,找出方法名
String methodName = a[l].substring(0, a[l].indexOf('('));
//找出参数
String paramStr = a[l].substring(a[l].indexOf('(') + 1,a[l].indexOf(')'));
Class c = obj.getClass();
if(methodName.indexOf("Time")>0){
Method method = c.getMethod(methodName, Date.class);
SimpleDateFormat sdfDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟
Date dateString=sdfDate.parse(paramStr);
method.invoke(obj, dateString);
}else{
Method method = c.getMethod(methodName, String.class);
method.invoke(obj, paramStr);
}
}
}
}