Problems with (Generic) Inheritance and Injection
andygibson.contact.andygibson.net Jun 12, 2009 8:03 PMI was messing around with a generic Dao type beans and hit a wall, so I trawled the groups and couldn't find anything so I created a simple test case that should work, but doesn't. In one post, Pete indicated it might be be issue (but I can't find that blasted post now) so I'm posting here to see if there is a problem or not. I'm running this with JBoss 5.1 (default Web beans install) and while I have the current Webbeans from the svn repository it isn't compiling right now.
Simple Test Case
public abstract class AbstractFactory<T> implements Serializable { public abstract T createObject(); }
and
@Named public class IntegerFactory extends AbstractFactory<Integer> { public Integer createObject() { return new Integer(123); } }
Used by
@Named public class SomeBean implements Serializable { @Current AbstractFactory<Integer> integerFactory; public String getObjectAsString() { return integerFactory.createObject().toString(); } }
This works fine. However, if I change SomeBean to use :
@Current IntegerFactory integerFactory;
Then I get an errror :
javax.inject.UnsatisfiedDependencyException: The injection point Field @Current integerFactory on Annotated class Class @Named SomeBean with binding types [@Current] in Dependent simple bean 'someBean' org.beanapp.jira.SomeBean, API types = [Object, Serializable, SomeBean], binding types = [@Current] has unsatisfied dependencies with binding types at org.jboss.webbeans.BeanValidator.validate(BeanValidator.java:112) at org.jboss.webbeans.bootstrap.WebBeansBootstrap.boot(WebBeansBootstrap.java:207) at org.jboss.webbeans.bootstrap.api.helpers.BootstrapBean.boot(BootstrapBean.java:120) at sun.reflect.GeneratedMethodAccessor446.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)
However, if I change the definition of the IntegerFactory to not inherit from the AbstractFactory, then it works fine :
@Named public class IntegerFactory implements Serializable { }
I tried using a binding type :
@Retention(RetentionPolicy.RUNTIME) @Target({TYPE,FIELD,METHOD}) @BindingType public @interface IntFactory { }
With the binding type instead of using @Named and @Current to inject, I found it failed and worked in the same scenarios.
I inherited the IntFactory from some non generic pojo and it worked fine. To test it, I'm using JSF (2.0) and testing #{someBean.objectAsString}
Do I have it wrong, am I missing something simple, or is there a problem with WebBeans?
Cheers,
Andy Gibson