关于java 反射 getMethods顺序引起的bug

客户那出现的bug   

public class TmJobInfoDto extends AbstractDto {

        private String flag;

        private int attr=0;

        ......
        public void setFlag (String flag){

                  if(this.attr==1){

                       this.flag="1";

                  }

                 this.flag=“0”

        }

        public void setAttr (int attr){

                this.attr=attr

        }

        ......
}
    protected List<AbstractDto> executeRefer(String sql, Map<String, Object> param,
            final AbstractDto columnValue){

        ParameterizedRowMapper<AbstractDto> mapper = new ParameterizedRowMapper<AbstractDto>() {

            @SuppressWarnings("unchecked")
            public AbstractDto mapRow(ResultSet rs, int rowNum) throws SQLException{
                Object dto = null;
                try{
                    ...............

                
                    Method[] methodAll = dto.getClass().getMethods();
                    for(Method setMethod: methodAll){
                        String fldName = (String) colNameMap.get(setMethod.getName());
                        if(fldName != null){
                            Class[] paramType = setMethod.getParameterTypes();
                            String type = paramType[0].getName();
                             if(STRING.equals(type)){
                                setMethod.invoke(dto, rs.getString(fldName) == null ? rs
                                        .getString(fldName) : rs.getString(fldName).trim());
                            }else if(INT.equals(type)){
                                setMethod.invoke(dto, rs.getInt(fldName));
                            }
                        }
                    }
                }catch(Exception e){
                  }

                return((AbstractDto) dto);
            }
        };

        LogWriter.debug("EXECUTING SQL: ");
        LogWriter.debug(sql);
        return template.query(sql, mapper, param);

数据库查询出的结果rs中 attr 的值为1.所以这时flag也应该为1.
但getMethods();得出的结果是无序的,导致executeRefer里的函数先调用了setFlag 这时(attr还是0),后调用setAttr 。
这个问题怎么解决?

如果单单这个class的话,你可以使用Arrays.sort(methodAll)方法排序一下试试,setAtrr在setFlag前,如果要一个通用的,需要灵活配置的,你需要开发个自定义注解之类的,在注解上定义调用顺序,写一个排序方法,按照注解的value来排序