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 protected or private field or method can be accessed using the reflection API (using setAccessible(true) on the appropriate Field or Method object).
Remember - public, protected and private are controls on default visibility, nothing more. They do not (and cannot) prevent access using reflection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With