Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how java jaxb works?

Tags:

java

jaxb

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?

like image 769
Alfred Avatar asked Aug 02 '10 19:08

Alfred


1 Answers

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.

like image 89
skaffman Avatar answered Oct 09 '22 00:10

skaffman