reflection - java.lang.reflect.field.set(obj,value) fails for applicationscoped - Stack Overflow
The following code runs successfully but the value isn't changed when the reference is an applicationscoped bean. When the bean is a Singleton EJB the value does change.
Changing the value via AnnotatedParameter and Method.invoke does change the value.
Does anyone know what might cause the difference in behaviour? I cannot find the reason in (java)docs, specs or elsewhere.
InjectionPoint ip = ....;
Class bc = ip.getMember().getDeclaringClass();
Object reference = CDI.current().select(bc).get();
Annotated a = ip.getAnnotated();
Object value = ....;
if (annotated instanceof AnnotatedField af) {
Field f = af.getJavaMember();
try {
boolean ac = f.canAccess(reference);
f.setAccessible(true);
f.set(reference, value);
f.setAccessible(ac);
} catch (IllegalAccessException e) {
log.error(String.format("error updating %s with %s",
f, value));
}
}
The following code runs successfully but the value isn't changed when the reference is an applicationscoped bean. When the bean is a Singleton EJB the value does change.
Changing the value via AnnotatedParameter and Method.invoke does change the value.
Does anyone know what might cause the difference in behaviour? I cannot find the reason in (java)docs, specs or elsewhere.
InjectionPoint ip = ....;
Class bc = ip.getMember().getDeclaringClass();
Object reference = CDI.current().select(bc).get();
Annotated a = ip.getAnnotated();
Object value = ....;
if (annotated instanceof AnnotatedField af) {
Field f = af.getJavaMember();
try {
boolean ac = f.canAccess(reference);
f.setAccessible(true);
f.set(reference, value);
f.setAccessible(ac);
} catch (IllegalAccessException e) {
log.error(String.format("error updating %s with %s",
f, value));
}
}
Share
Improve this question
asked yesterday
Eduard DrenthEduard Drenth
535 bronze badges
3
- 2 Is it possible you have reference to the proxy, not the actual instance? – Turing85 Commented yesterday
- Thanks, I don't think so, the id of the reference equals that of the object where I (later in time) read the value – Eduard Drenth Commented yesterday
- It is actually a long standing issue for me, see github.com/eduarddrenth/Configuration/issues/5 – Eduard Drenth Commented yesterday
1 Answer
Reset to default 1With CDI, the actual instance that's injected is a proxy. It's an instance of a generated sub class of the bean class. That's why CDI has some limitations like requiring a no-arg constructor and no final methods. The generated sub class has a no-arg constructor that calls the no-arg constructor of the bean class (super()
), and each method is overridden. Inside the proxy there is (in some way) a reference to the single instance; the overridden methods each call the same method of this instance.
The reference
you have has all of the fields of the bean class, but if you inspect them in a debugger you'll probably see they are all null
. That's my experience so far. The only field that matters is the internal reference to the single instance. The field you set is simply ignored by the proxy.
With @Singleton
on the other hand, both in JBoss and in Quarkus, there is no proxy. The injected instance is the singleton instance itself.
Some links that document this:
- https://docs.jboss.org/weld/reference/latest/en-US/html/scopescontexts.html#_the_singleton_pseudo_scope
- https://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#client_proxies
- https://quarkus.io/guides/cdi#applicationscoped-and-singleton-look-very-similar-which-one-should-i-choose-for-my-quarkus-application
- 从基础软硬件兼容性谈桌面云的应用
- 百度部分下线盗版视频 反盗版方敦促其全面停止
- python - How to define grammar for minimum 1 of foo and maximum 1 of bar - Stack Overflow
- opengl - How to render a FBO into an ImGui window? - Stack Overflow
- typescript - Npm 404 Not found - Stack Overflow
- c++ - Level-Order Traversal of a Binary Tree Without Recursion? - Stack Overflow
- curl - Error: SOAP client returned status of 401 php-ews - Stack Overflow
- swiftui - ShareLink with custom type produces error - Stack Overflow
- algorithm - LeetCode help , 3 letter palindrome question - medium - Stack Overflow
- React Native - Persist android ripple on state change - Stack Overflow
- graph theory - Algorithm to find a subgraph that includes all vertices, ensures single connection failure tolerance, and minimiz
- c - Validity of `__attribute__((pure))` - Stack Overflow
- sql - Rolling sum that resets at a flag - Stack Overflow
- c# - What could be wrong with the Import method on the oSets object here? - Stack Overflow
- python - Newton raphson fails to approximate beyond two decimal points - Stack Overflow
- Can't swap camera with AVCaptureMultiCamSession with SwiftAVKit for iOS - Stack Overflow
- java - YubiKey PIV AuthenticationDecryption returns 0x6A80 error - Stack Overflow