dimanche 10 mai 2015

Don't print null values and its properties in the log for java object's using ReflectionToStringBuilder

I have to print object values in log[log file]. I used

ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE, true, true);

But its also print null values too.I don't want to print null values in log. how to suppress null values from log. Please help me to resolve this issue.

Thanks advance..

Sample Prog

public class Refelection {
private Integer id;
private String name;
private String description;
public static final String KEY = "APP-KEY";
private transient String secretKey;

public Refelection(Integer id, String name, String description, 
        String secretKey) {
    this.id = id;
    this.name = name;
    this.description = description;
    this.secretKey = secretKey;
}

@Override
public String toString() {
    /*
     * Generate toString including transient and static attributes.
     */
    return ReflectionToStringBuilder.toString(this, 
            ToStringStyle.MULTI_LINE_STYLE, true, true);
}

public static void main(String[] args) {
    Refelection demo = 
            new Refelection(1, "", "Manchester United", "Alex");
    System.out.println("Demo = " + demo);
}

}

Ans:Demo = Refelection@117d9a3[ id=1 ,name=null,description=Manchester United,KEY=APP-KEY,secretKey=Alex ]

Aucun commentaire:

Enregistrer un commentaire