/** * 把修改的传入的值为空的抛出 * @param o * @param c * @param * @return */ public static T info(T o, T c){ try { //获取o,c类的方法 Method[] m1=o.getClass().getMethods(); Method[] m2=c.getClass().getMethods(); //遍历m2的get方法 method.invoke(c)通过反射判断m2的方法不为空 for (Method method : m2) { if (method.getName().startsWith(“get”)&&method.invoke(c)!=null) { boolean isCopy=true; if(method.getReturnType().getName().equals(“int”)){ isCopy=false; }else if(method.getReturnType().getName().equals(“Date”)){ isCopy=false; }else if(method.getReturnType().getName().equals(“boolean”)){ isCopy=false; } if(isCopy){ // method.getName().replaceFirst method的方法成功则返回替换的set方法,失败则返回get方法。 String n=method.getName().replaceFirst(“get”,”set”); //判断m1,n的方法是否存在 Method m=isMethodExist(m1,n); if(m!=null){ //如果存在,则返回m的方法 m.invoke(o,method.invoke(c)); //entity.setName(info.getName()); } } } } }catch (Exception e){ e.printStackTrace(); return null; } return o; }
private static Method isMethodExist(Method[] methods, String methodName){ for (Method method : methods) { if (method.getName().equals(methodName)) { return method; } } return null; }
使用方法 // ProductInfo source=null; // if(productInfo.getId()!=null){ // source = new ProductEntity( ).buildId( productInfo.getId()).buildCode( productInfo.getId() ).findOne( ); // info( source,productInfo ); // System.out.println(“=========”+source+”========”); // }