Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible in Java to check if objects fields are null and then add default value to all those attributes?

Tags:

java

null

I need to make sure that no object attribute is null and add default value in case if it is null. Is there any easy way to do this, or do I have to do it manually by checking every attribute by its getters and setters?

like image 570
newbie Avatar asked Jan 27 '10 10:01

newbie


People also ask

How do you check if the attributes of an object are null in Java?

Java Check if Object Is Null Using java. Objects class has static utility methods for operating an object. One of the methods is isNull() , which returns a boolean value if the provided reference is null, otherwise it returns false.

Why default value of object is null in Java?

Because local variables must be initialized in Java. In Java, class and instance variables assume a default value (null, 0, false) if they are not initialized manually.

Can objects be null in Java?

Intent. In most object-oriented languages, such as Java or C#, references may be null. These references need to be checked to ensure they are not null before invoking any methods, because methods typically cannot be invoked on null references.


1 Answers

You can use reflection to iterate over the object's field, and set them. You'd obviously need some sort of mapping between types or even field names and required default values but this can be done quite easily in a loop. For example:

for (Field f : obj.getClass().getFields()) {   f.setAccessible(true);   if (f.get(obj) == null) {      f.set(obj, getDefaultValueForType(f.getType()));   } } 

[Update]

With modern Java, you can use annotations to set the default values for fields on a per class basis. A complete implementation might look like this:

// DefaultString.java: import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy;  @Retention(RetentionPolicy.RUNTIME) public @interface DefaultString {     String value(); }  // DefaultInteger.java: import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy;  @Retention(RetentionPolicy.RUNTIME) public @interface DefaultInteger {     int value(); }  // DefaultPojo.java: import java.lang.annotation.Annotation; import java.lang.reflect.Field;  public class DefaultPojo {      public void setDefaults() {         for (Field f : getClass().getFields()) {             f.setAccessible(true);             try {                 if (f.get(this) == null) {                     f.set(this, getDefaultValueFromAnnotation(f.getAnnotations()));                 }             } catch (IllegalAccessException e) { // shouldn't happen because I used setAccessible             }         }     }      private Object getDefaultValueFromAnnotation(Annotation[] annotations) {         for (Annotation a : annotations) {             if (a instanceof DefaultString)                 return ((DefaultString)a).value();             if (a instanceof DefaultInteger)                 return ((DefaultInteger)a).value();         }         return null;     }  }  // Test Pojo public class TestPojo extends DefaultPojo {     @DefaultString("Hello world!")     public String stringValue;     @DefaultInteger(42);     public int integerValue; } 

Then default values for a TestPojo can be set just by running test.setDetaults()

like image 111
Guss Avatar answered Oct 20 '22 01:10

Guss