Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for generic field injection. #2910

Closed
wants to merge 4 commits into from
Closed

Conversation

RussiaVk
Copy link

@RussiaVk RussiaVk commented Feb 13, 2024

weld not support generic field injection yet.
For example:

public interface IService {
	void print();
}
@ApplicationScoped
public class ServiceImpl1 implements IService {
	@Override
	public void print() {
		System.out.println("-------ServiceImpl1---------");
	}
}
public abstract class AbstractCDIClass<T extends IService> {
	@Inject
	private T service;
	
	@Test
	public void test() {
		service.print();
	}
}
@ApplicationScoped
public class CDIImpl1 extends AbstractCDIClass<ServiceImpl1> {
	@Deployment
	public static JavaArchive getJavaArchive() {
		return ShrinkWrap.create(JavaArchive.class).addAsManifestResource("META-INF/beans.xml").addClass(CDIImpl1.class)
				.addClass(ServiceImpl1.class);

	}
}

If we run CDIImpl1,it will throw following exception :

org.jboss.weld.exceptions.DefinitionException: WELD-001407: Cannot declare an injection point with a type variable: [BackedAnnotatedField] @Inject private AbstractCDIClass.service
	at AbstractCDIClass.service(AbstractCDIClass.java:0)
  StackTrace
	at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDefinitionErrors(Validator.java:301)
	at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:287)
	at org.jboss.weld.bootstrap.Validator.validateProducer(Validator.java:425)
	at org.jboss.weld.injection.producer.InjectionTargetService.validateProducer(InjectionTargetService.java:36)
	at org.jboss.weld.manager.InjectionTargetFactoryImpl.validate(InjectionTargetFactoryImpl.java:153)
	at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:81)

Or refer to this stackoverflow question.

Could you please share your thoughts about the potential significance and value of the changes proposed in my PR? I would greatly appreciate any feedback or insights you could provide.
Thank you for your attention and guidance.

Add a threadLocal variable to cache beanClass for "org.jboss.weld.injection.attributes.AbstractInferringInjectionPointAttributes"
Add support for generic field injection.To solve problem about
"WELD-001407: Cannot declare an injection point with a type variable"
add guava dependency
add guava dependency
@manovotn
Copy link
Contributor

Weld is an implementation of the CDI specification and type variables in injection points are forbidden by the specification.
Besides, you can just declare the injection point as IService or use a wildcard with bounds, both of which are permitted and should work fine.

If an injection point type is a type variable, the container automatically detects the problem and treats it as a definition error.

Here's the relevant specification part - https://jakarta.ee/specifications/cdi/4.0/jakarta-cdi-spec-4.0#legal_injection_point_types

@RussiaVk
Copy link
Author

Weld is an implementation of the CDI specification and type variables in injection points are forbidden by the specification. Besides, you can just declare the injection point as IService or use a wildcard with bounds, both of which are permitted and should work fine.

If an injection point type is a type variable, the container automatically detects the problem and treats it as a definition error.

Here's the relevant specification part - https://jakarta.ee/specifications/cdi/4.0/jakarta-cdi-spec-4.0#legal_injection_point_typeI got it. Thank you for your explanation

Weld is an implementation of the CDI specification and type variables in injection points are forbidden by the specification. Besides, you can just declare the injection point as IService or use a wildcard with bounds, both of which are permitted and should work fine.

If an injection point type is a type variable, the container automatically detects the problem and treats it as a definition error.

Here's the relevant specification part - https://jakarta.ee/specifications/cdi/4.0/jakarta-cdi-spec-4.0#legal_injection_point_types

I got it. Thank you for your explanation

@RussiaVk RussiaVk closed this Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants