[weld-issues] [JBoss JIRA] Closed: (WELD-218) Check and fix if type variables are allowed on observes methods

Pete Muir (JIRA) jira-events at lists.jboss.org
Tue Nov 3 12:55:05 EST 2009


     [ https://jira.jboss.org/jira/browse/WELD-218?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Pete Muir closed WELD-218.
--------------------------

    Resolution: Done
      Assignee: Pete Muir


Added a test to the TCK, and fixed in core

> Check and fix if type variables are allowed on observes methods
> ---------------------------------------------------------------
>
>                 Key: WELD-218
>                 URL: https://jira.jboss.org/jira/browse/WELD-218
>             Project: Weld
>          Issue Type: Feature Request
>    Affects Versions: 1.0.0.CR1
>            Reporter: Kabir Khan
>            Assignee: Pete Muir
>            Priority: Critical
>             Fix For: 1.0.0.CR2
>
>
> Kabir:
> I have tried;
> public void processInjectionTarget(@Observes ProcessInjectionTarget<?> event)
> {
>   AnnotatedType<?> type = event.getAnnotatedType();
>   WeldKernelControllerContext context = WeldFromMcRegistry.getContext(type);
>   if (context != null)
>   {
>      InjectionTarget<?> target = event.getInjectionTarget();
>      @SuppressWarnings("unchecked")
>      ExistingInstanceInjectionTarget<?> wrappedTarget = new ExistingInstanceInjectionTarget(target, context.getTarget());
>      event.setInjectionTarget(wrappedTarget);
>   }
> }
> This does not compile with the error:
> The method setInjectionTarget(InjectionTarget<capture#6-of ?>) in the type ProcessInjectionTarget<capture#6-of ?> is not applicable for the arguments (ExistingInstanceInjectionTarget<capture#7-of ?>)
> Pete:
> Yes, this is the correct approach, as it will observe all events.
> Kabir:
> However, the call to event.setInjectionTarget() does not compile in this case. I've had to do the following:
>  public void processInjectionTarget(@Observes @SuppressWarnings("unchecked") ProcessInjectionTarget event)
>  {
>     AnnotatedType<?> type = event.getAnnotatedType();
>     WeldKernelControllerContext context = WeldFromMcRegistry.getContext(type);
>     if (context != null)
>     {
>        InjectionTarget<?> target = event.getInjectionTarget();
>        @SuppressWarnings("unchecked")
>        ExistingInstanceInjectionTarget tgt = new ExistingInstanceInjectionTarget(target, context.getTarget());
>        event.setInjectionTarget(tgt);
>     }
>  }
> Pete:
> Ok, this is fixable if type variables are allowed on observes methods.
> Kabir:
> public <X> void processInjectionTarget(@Observes ProcessInjectionTarget<X> event)
> {
>   AnnotatedType<?> type = event.getAnnotatedType();
>   WeldKernelControllerContext context = WeldFromMcRegistry.getContext(type);
>   if (context != null)
>   {
>      InjectionTarget<X> target = event.getInjectionTarget();
>      ExistingInstanceInjectionTarget<X> wrappedTarget = new ExistingInstanceInjectionTarget<X>(target, context.getTarget());
>      event.setInjectionTarget(wrappedTarget);
>   }
> }
> which compiles, but gives:
> org.jboss.weld.DefinitionException: Cannot use a type variable X in an parameterized type Observer Implementation:
> Observer (Declaring) class: class org.jboss.test.kernel.weld.mctowb.support.validateobserver.TestObserver  Observer method: method public void org.jboss.test.kernel.weld.mctowb.support.validateobserver.TestObserver.processInjectionTarget(javax.enterprise.inject.spi.ProcessInjectionTarget)
> 	at org.jboss.weld.event.ObserverMethodImpl.checkObserverMethod(ObserverMethodImpl.java:118)
> 	at org.jboss.weld.event.ObserverMethodImpl.initialize(ObserverMethodImpl.java:191)
> Pete:
> I *believe* this is a bug in the RI. Could you do me a favour, and scour the Events chapter, and make sure it doesn't restrict you from putting a parameterized type in an @Observes parameter? If it doesn't, file a bug for CR2. I will fix it asap.
> Kabir:
> I'm not 100% sure, but I don't think I saw anything there regarding type variables.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://jira.jboss.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the weld-issues mailing list