Just curious about how jaxb works, I have a class annotated as follows:
@XmlRootElement(name = "MyJaxb")
Class MyJaxb
{
@XmlElement
protected String str;
public void setStr(String str)
{
this.str = str;
}
}
The access modifier of field str is protected, why Jaxb can still marshall and unmarshall it?
It uses reflection. A
protectedorprivatefield or method can be accessed using the reflection API (usingsetAccessible(true)on the appropriateFieldorMethodobject).Remember –
public,protectedandprivateare controls on default visibility, nothing more. They do not (and cannot) prevent access using reflection.