• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XmlAccessType.FIELD definition

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a bit puzzled by this one. The docs rather glibly say:

Every non static, non transient field in a JAXB-bound class will be automatically bound to XML, unless annotated by XmlTransient. Getter/setter pairs are bound to XML only when they are explicitly annotated by some of the JAXB annotations.


Which leaves me wondering when compared to the other enum values, "if the field isn't public, and doesn't have a getter/setter pair, then how will the code get to the value?"

Options I've considered include:

1. private element, with a get OR set method, but not both, for one-way conversion. The examples I've seen seem to all have a private field with a get method but no set method. If this is the case, then is private field with only a set method OK as well, for XML to Java conversion only?

2. protected field, and somehow an adapter class gets created which exposes the field with public methods

Or could it be something else entirely?
 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I would prefer to write some code to test this, but don't have time at the moment, so I can just contribute another guess:

Which leaves me wondering when compared to the other enum values, "if the field isn't public, and doesn't have a getter/setter pair, then how will the code get to the value?"


I am guessing that JAXB uses reflection to set the value of the field when marshalling XML to Java objects. The application code would then use the getter method of the field to retrieve the value, which in effect is immutable, as far as the application is concerned (assuming there is no setter method, of course).
Best wishes!
 
steve claflin
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's another option I hadn't thought of. I had forgotten that reflection can access private elements without using a getter or setter.

I'm in the same boat as far as not being able to write up a test at the moment, but this is another thing I can look for in the generated classes when I do.
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic