项目中有新需求,要求手机号加密,传统的ssh模式项目。ssh
手机号存在于不少对象中,如何使众多对象共用封装好的方法,并正确返回各自加密解密后的对象呢,以某对象组成list集合为例。加密
好比加密:对象
/**get
*加密it
*/io
public <T> List<T> after100(List<T> o,Class tClass) throws Exception{
List<T> t=new ArrayList<T>();
if(o!= null && o.size() > 0){
for (int i=0;i<o.size();i++){
Object oo = tClass.cast(o.get(i));//tclass具体加密的类对象
if(o.get(i)!=null){
Method method=tClass.getMethod("getMobile");
Method method2=tClass.getMethod("setMobile", String.class);//加密手机号
Method method3=tClass.getMethod("setMobileNoYG", String.class);//未加密手机号
String mobile =(String) method.invoke(oo);
System.out.println("手机号:"+mobile);
if(mobile != null ){
if(!mobile.startsWith("YG")){
String encryptResultStr = encrypt(mobile, password);//具体加密规则
encryptResultStr = "YG|" + encryptResultStr;
method2.invoke(oo,encryptResultStr);
method3.invoke(oo,mobile);
System.out.println("加密手机号:"+encryptResultStr);
}
}
}
t.add(i, (T) oo);
}
}
return t;
}ast
/**class
*解密同理mobile
/
public <T> List<T> before100(List<T> o,Class tClass) throws Exception{
List<T> t=new ArrayList<T>();
return o;
}
public static void main(String[] args) throws Exception {
MobileEncryption mobileEncryption = new MobileEncryption();
Class aaa=PrpUinsured.class;
PrpUinsured prpUinsured=new PrpUinsured();
PrpUinsured prpUinsured2=new PrpUinsured();
prpUinsured.setMobile("17666666666");
prpUinsured2.setMobile("17663366666");
List<PrpUinsured> prpUinsureds=new ArrayList<PrpUinsured>();
prpUinsureds.add(prpUinsured);
prpUinsureds.add(prpUinsured2);
List<com.sinosoft.prpall.schema.model.unitedsale.PrpUinsured> ooo = mobileEncryption.after100(prpUinsureds, aaa);
System.out.println(ooo.get(0).getMobile()+ooo.get(1).getMobile());
}List