0

I have made traversal through object graph to pick up all fields and their given annotations and would like to validate domain objects built from the XSDs based on the annotations.

However, i got stuck on the @XmlElement as i don't know how to get the value of required attribute.

import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlElement;
    public class SomeClass {

     @XmlElement(name = "user_id", required = true)
     @NotNull
     protected String userId;

    }

This must be simple but i cannot figure how to check if the attribute required is set to true once i detected that the given annotation is of type @XmlElement.

    if(annotation.annotationType().equals(XmlElement.class)) {

            // how to check value of required atrribute         
    }
John
  • 5,189
  • 2
  • 38
  • 62

1 Answers1

1

You can achieve it this way:

// iterate over the fields in the required class. check if the annotatino is present
if (inputField.isAnnotationPresent(XmlElement.class)) {
    XmlElement xmlElementAnnotation = inputField.getAnnotation(XmlElement.class);

    // get 'required' value
    if(xmlElementAnnotation.required()) {
        // logic
    }
}
Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68