I saw this question
Inject into private, package or public field or provide a setter?
about how to manually inject into annotated private fields (The way is adding setters
or through a constructor)
But, the point is how do an application server (like glassfish, axis2, jboss, …)
is able to inject into a final private field (without adding setters or constructors
to the user class)?
Quoting the cited question:
public SomeClass {
@Inject
private SomeResource resource;
}
Do they use a customized JVM (not the standard one) that allows to access private fields?
Thanks
It’s a simple reflection “trick”. It relies on the
Field.setAccessible()method to force the member to be accessible programmatically:The Reflection API is used to get a handle on the field,
setAccessible()is called, and then it can be set by the injection framework.See an example here.
No magic, no custom VM.