2016-02-06 35 views
0

在Java對象掃描,是否有掃描任何對象的方法/所有null值,並將其設置爲默認值,不管他們的Object類? (Longs,Booleans,Strings等)。爲空性質在

語法上我想像這將是與此類似,或較簡單的

for(key in obj){ 
    if(key == null && key.isString()) key.setValue("Undefined"); 
    if(key == null && key.isLong()) key.setValue(000); 
    //so on and so fourth 
} 

在這種情況下,我不想簡單地捕捉異常,因爲我需要設置爲「未定義」當值顯示在Web界面上。

在此先感謝!

+0

你的意思是:OBJ [關鍵] = 「未定義」,OBJ [關鍵] = 0L;我猜 – Gavriel

+0

原始值如long和boolean已經有了默認值。不知道爲什麼你會使用拳擊對象類作爲字段 –

+0

我想你可能意思是「場」而不是「價值」。 –

回答

0

想知道爲什麼你會將它設置爲默認值,而不是拋出異常。由於這裏沒有清楚地理解用例,你嘗試過使用ConcurrentHashMap嗎?它不會讓你的密鑰爲空。

+2

我認爲這應該是一個評論。 – StepTNT

1

整理。需要注意的是Java的原始和對象區分(即intInteger一樣的。如果你有,比如說,int i = 0,然後做i = null將引發異常。如果你有Integer i = 0,然後i = null是罰款)。

Java有可能以簡單的方式提供您想要的東西 - 例如,您可以執行此操作。

public class Sample { 
    int i = =1; 
    String = "value"; 
    LocalDateTime time = LocalDateTime.now(); 
} 

等。根據其中數據是從(例如被從其中這些字段可以是空數據庫來嗎?)的到來,這可能是足夠的。

編輯:Java有默認的類級字段。原始類型的默認值是0private int i = 0;private int i;相同),對於它的對象是nullprivate String s = null;private String s;相同)。

否則,如果您使用的是框架(如春,JEE等),那麼他們可能會提供一個更好的方式來對你發回的響應設置的默認值。

是否有你需要將其設置爲Undefined具體理由嗎? JavaScript會將null的值視爲falsey默認情況下(例如var a = null; if (a) {...} else {...}將採用else路徑),所以我會假設無論向最終用戶顯示的信息都能夠沿着if (!field) { display("Undefined", fieldName); }的行發言。

如果完全沒有其他方式來做到這一點,那麼它可以完成,但考慮到上面的關於基元和對象的不同點,如果您想要在構建後掃描類並更改所有字段上課,你會有一個相當有趣的時間。

下面的代碼將獲取在類的所有字段(但不是在超類(ES)!),並將其設置爲-1"value",或者爲null,取決於類型:

try { 
     final Object o = new Object(); 
     Field[] declaredFields = o.getClass().getDeclaredFields(); 
     for (Field f : declaredFields) { 
      f.setAccessible(true); 
      if(f.getType().equals(int.class) || f.getType().equals(Integer.class)) { 
       f.setInt(o, -1); 
      } 
      if(f.getType().equals(long.class) || f.getType().equals(Long.class)) { 
       f.setLong(o, -1L); 
      } 
      if(f.getType().equals(double.class) || f.getType().equals(Double.class)) { 
       f.setDouble(o, -1.0D); 
      } 
      if(f.getType().equals(float.class) || f.getType().equals(Float.class)) { 
       f.setFloat(o, -1.0F); 
      } 
      if(f.getType().equals(byte.class) || f.getType().equals(Byte.class)) { 
       f.setByte(o, (byte)-1); 
      } 
      if(f.getType().equals(char.class) || f.getType().equals(Character.class)) { 
       f.setChar(o, (char)-1); 
      } 
      if(f.getType().equals(String.class)) { 
       f.set(o, "value"); 
      } 
      else { 
       f.set(o, null); 
      } 
     } 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } 

我強烈建議尋找一種更簡單的方法來做你想做的事情,如果可能的話。

0

我認爲你想要做這樣的例子,如果是的話,那麼Java反射可以幫助你實現你的目標。

import java.lang.reflect.Field; 

public class Testing { 
Integer x;//Will be null by default 
Integer y;//Will be null by default 
Integer z=1; 
public static void main(String[] args) { 
    try { 
     Testing testing = new Testing(); 
     Field[] fs = testing.getClass().getDeclaredFields(); 
     for (Field field : fs) { 
      field.setAccessible(true); 
      if (field.get(testing) == null && field.getName().equals("y")) { 
       field.set(testing, 2); 
      } 
     } 
     System.out.println(testing.x);//Prints null 
     System.out.println(testing.y);//Prints 2 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 

    } 
} 
+0

如果該字段除了「Integer」,「int」,「float」,「double」或「long」以外都會導致問題。 – ipsi

+0

@ipsi我不這麼認爲,給我們一個參考並解釋可能的問題。剛剛用字段類型字符串進行了測試。 – saurav

+0

該評論是基於你的原始答案,你沒有檢查字段名稱。即使如此,如果你有'Long y';'然後做'field.set(testing,2)'(假設'field'是'y'),那麼它會引發一個異常。 – ipsi

1

可以使反射的用戶像下面這樣:

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 with 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; 
} 


}