項目中有新需求,要求手機號加密,傳統的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