Hello,

this is a good candidate for spec issue. I've created one - https://github.com/jakartaee/cdi/issues/829
My personal take is that Weld behavior is a way to go as otherwise you'd have no use for observing primitive types.
Strictly speaking, it is ATM Weld value-add but I don't see why the spec couldn't cover this or at least have some TCK for it (I found none).

Matej

On Thu, Oct 17, 2024 at 4:06 AM Laird Nelson <ljnelson@gmail.com> wrote:
The CDI 4.0 spec says in section 2.8.1 (https://jakarta.ee/specifications/cdi/4.0/jakarta-cdi-spec-4.0#event_types_and_qualifier_types):

"Any Java type may be an observed event type."

That means I can do this:

private void primitive(@Observes int i) { // any type, including int, can be an observed event type
  System.out.println("primitive: " + i);
}

And this:

private void wrapper(@Observes Integer i) {
  System.out.println("wrapper: " + i);
}

Then of course the only way to fire an event is by using the Event object and specifying the type of even you want to fire as its sole type argument:

// for example
@Inject
private Event<Integer> e;

(Type arguments of course cannot be primitive.)

No mention in the specification's section on observer resolution talks about primitive types and/or wrapper types and how they interact. (Obviously there are unrelated sections regarding typesafe resolution in the context of dependency injection that do talk about this, but they do not seem to apply here without a biiiiiig stretch (observed event parameters are not injection points; event types are not bean types).)

If I do e.fire(Integer.valueOf(42)), the primitive(int) observer method above is called (in Weld 5.1.3.Final). So is the wrapper(Integer) observer method. This certainly is convenient and I'm kind of glad it works that way, but is it correct? Is it a Weld value-add? Or should the spec have said something about interoperability of primitives and wrapper types in observer resolution?

Best,
Laird
_______________________________________________
weld-dev mailing list -- weld-dev@lists.jboss.org
To unsubscribe send an email to weld-dev-leave@lists.jboss.org
Privacy Statement: https://www.redhat.com/en/about/privacy-policy
List Archives: https://lists.jboss.org/archives/list/weld-dev@lists.jboss.org/message/IDWNDAJLY57OE6MJNNRVW5HSOV3MJLPJ/