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

Kabir Khan (JIRA) jira-events at lists.jboss.org
Wed Oct 21 05:58:05 EDT 2009


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
             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