jeudi 17 octobre 2019

Getting "last visited field" in a Java Bean

For Exceptionhandling, I want to know, which getter/field in a given Bean produced the Exception. What I do not want is a try/catch around every getter-call, or having to manually keeping track with a String variable.

Can I use Java Reflections for it? How could I design an Interface the Bean implements?

Pseudo-Code where I want the Exception handling to happen:

try {
            step.setNo(Long.parseLong(stepElement.getChildText("no", nameSpace)));
            step.setName(stepElement.getChildText("name", nameSpace));
            step.setDetail(stepElement.getChildText("detail", nameSpace));
            step.setEcuName(stepElement.getChildText("ecu-name", nameSpace));
}catch(Exception e){
            String fieldname = /*which field did throw? */
}

Part of the actual Bean:

public class CupProcStep {
    private long no;
    private String name;
    private String detail;
    private String ecuName;
    private String ecuVariant;
    private String diagnosticsId;
    private String errorText;
    private String stepResult;

public long getNo() {
        return no;
    }

    public void setNo(long no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public String getEcuName() {
        return ecuName;
    }

The name of the field, that threw the Exception, e.g. "no","name,"ecuName", etc.





Aucun commentaire:

Enregistrer un commentaire