java reflection determination target empty field

public  class ReflectionUtils { 

    Private  Final  static Logger Logger = LoggerFactory.getLogger (ReflectionUtils. class ); 

    / ** 
     * check field 
     * / 
    public  static  class CheckFiled {
         / ** 
         * Check fields are null or "" 
         * @param Source correction required test entity 
         * @param excludeFields set of attributes need not be verified 
         * @return 
         * / 
        public  static  void checkFileNullAble (Source Object, String ... excludeFields) { 

            List <String> = excludeFieldsListnew new the ArrayList <> ();
             IF (excludeFields.length> 0 ) { 
                excludeFieldsList = Arrays.asList (excludeFields); 
            } 

            the try {
                 // get the obj to class, to get all the attributes and 
                Field, [] = FS source.getClass ( ) .getDeclaredFields ();
                 // iterate through all attributes 
                for (Field, F: FS) { 
                    isNullField (F, Source, excludeFieldsList); 
                } 
            } the catch (Exception E) { 
                logger.error (e.getMessage (), E); 
            } 
        }

        Private  static  void isNullField (Field, f, Source Object, List <String> excludeFieldsList) throws Exception {
             // set the private property is also accessible 
            f.setAccessible ( to true ); 

            // no need to exclude properties 
            IF (excludeFieldsList == null | | excludeFieldsList.size () == 0 ) { 
                Object filedValue = f.get (Source);
                 IF (filedValue == null || "" .equals (filedValue.toString ())) {
                     the throw  new new Exception ( "the Parameter" + f.getName () + "is null or empty!" );
                }
                return;
            }

            if(!excludeFieldsList.contains(f.getName())) {
                if ((f.get(source) == null || "".equals(f.get(source).toString()))){
                    throw new Exception("Parameter "+f.getName()+"  is null or empty!");
                }
            }
        }

    }

}

 

Guess you like

Origin www.cnblogs.com/zfzf1/p/11320503.html