java - Scan Objects for Null Properites -


in java, there way scan object any/all null values, , set them default value, no matter object class? (longs, booleans, strings, etc).

syntactically i'd imagine similar this, or simpler

for(key in obj){     if(key == null && key.isstring()) key.setvalue("undefined");      if(key == null && key.islong()) key.setvalue(000);      //so on , fourth } 

in case don't want catch exception, need value set "undefined" when displayed on web interface.

thanks in advance!

you can make user of reflection following :

  import java.lang.reflect.field;  import java.util.arraylist;  import java.util.collection;  import java.util.collections;  import java.util.date;  import java.util.list;  import java.util.scanner;   public class main { public static void main(string[] args) throws illegalargumentexception,  illegalaccessexception, instantiationexception {  user user=new user("islam",null);  field[] fields = user.getclass().getdeclaredfields(); for(field f : fields){  f.setaccessible(true);  if(f.get(user)==null){      object val=null;      if(number.class.isassignablefrom(f.gettype())){          val=0;      }else if(boolean.class.isassignablefrom(f.gettype())){          val=false;      }//complete other types      f.set(user, val);   } } system.out.println("user name : "+user.getname()); system.out.println("user age : "+user.getage());  }  }  class user{ private string name; private integer age; user(){  } user(string name,integer age){     this.name=name;     this.age=age; } public string getname() {     return name; } public void setname(string name) {     this.name = name; } public integer getage() {     return age; } public void setage(integer age) {     this.age = age; }   } 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -