-
Notifications
You must be signed in to change notification settings - Fork 290
Allow contravariant wildcards in Event and covariant wildcards in Instance #3363
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
Open
manovotn
wants to merge
1
commit into
weld:6.0
Choose a base branch
from
manovotn:wildcard-event-contravariant-6.0
base: 6.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...st/java/org/jboss/weld/tests/event/wildcard/contravariant/BeanWithContravariantEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package org.jboss.weld.tests.event.wildcard.contravariant; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.event.Event; | ||
| import jakarta.inject.Inject; | ||
|
|
||
| @ApplicationScoped | ||
| public class BeanWithContravariantEvent { | ||
|
|
||
| @Inject | ||
| Event<? super LifecycleEvent<?>> lifecycleEvents; | ||
|
|
||
| public void fireEvent(LifecycleEvent<?> event) { | ||
| lifecycleEvents.fire(event); | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
...a/org/jboss/weld/tests/event/wildcard/contravariant/BeanWithSimpleContravariantEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package org.jboss.weld.tests.event.wildcard.contravariant; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.event.Event; | ||
| import jakarta.inject.Inject; | ||
|
|
||
| @ApplicationScoped | ||
| public class BeanWithSimpleContravariantEvent { | ||
|
|
||
| @Inject | ||
| Event<? super Widget> widgetEvents; | ||
|
|
||
| public void fireWidget(Widget widget) { | ||
| widgetEvents.fire(widget); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
...ava/org/jboss/weld/tests/event/wildcard/contravariant/EventContravariantWildcardTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package org.jboss.weld.tests.event.wildcard.contravariant; | ||
|
|
||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import org.jboss.arquillian.container.test.api.Deployment; | ||
| import org.jboss.arquillian.junit.Arquillian; | ||
| import org.jboss.shrinkwrap.api.Archive; | ||
| import org.jboss.shrinkwrap.api.BeanArchive; | ||
| import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
| import org.jboss.weld.test.util.Utils; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| /** | ||
| * Verifies that {@code Event<? super X>} injection points are valid and functional. | ||
| * {@code Event} is naturally contravariant — you fire subtypes into it — so a | ||
| * lower-bounded wildcard is a legitimate use case. | ||
| * <p> | ||
| * This reproduces the scenario reported by Gavin King where Jakarta Data injects | ||
| * {@code Event<? super LifecycleEvent<?>>}. | ||
| * | ||
| * @see <a href="https://github.com/jakartaee/cdi/issues/888">CDI #888</a> | ||
| */ | ||
| @RunWith(Arquillian.class) | ||
| public class EventContravariantWildcardTest { | ||
|
|
||
| @Deployment | ||
| public static Archive<?> getDeployment() { | ||
| return ShrinkWrap.create(BeanArchive.class, Utils.getDeploymentNameAsHash(EventContravariantWildcardTest.class)) | ||
| .addClasses(BeanWithContravariantEvent.class, LifecycleEvent.class, LifecycleEventObserver.class, | ||
| BeanWithSimpleContravariantEvent.class, Widget.class, WidgetObserver.class); | ||
| } | ||
|
|
||
| @Test | ||
| public void testParameterizedContravariantEventWildcard(BeanWithContravariantEvent bean, | ||
| LifecycleEventObserver observer) { | ||
| bean.fireEvent(new LifecycleEvent<>("test")); | ||
| assertTrue("LifecycleEvent should have been observed", observer.isObserved()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSimpleContravariantEventWildcard(BeanWithSimpleContravariantEvent bean, | ||
| WidgetObserver observer) { | ||
| bean.fireWidget(new Widget("test")); | ||
| assertTrue("Widget event should have been observed", observer.isObserved()); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
...llian/src/test/java/org/jboss/weld/tests/event/wildcard/contravariant/LifecycleEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package org.jboss.weld.tests.event.wildcard.contravariant; | ||
|
|
||
| public class LifecycleEvent<T> { | ||
|
|
||
| private final T payload; | ||
|
|
||
| public LifecycleEvent(T payload) { | ||
| this.payload = payload; | ||
| } | ||
|
|
||
| public T getPayload() { | ||
| return payload; | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
...c/test/java/org/jboss/weld/tests/event/wildcard/contravariant/LifecycleEventObserver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package org.jboss.weld.tests.event.wildcard.contravariant; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.event.Observes; | ||
|
|
||
| @ApplicationScoped | ||
| public class LifecycleEventObserver { | ||
|
|
||
| private boolean observed = false; | ||
|
|
||
| public void onLifecycleEvent(@Observes LifecycleEvent<?> event) { | ||
| observed = true; | ||
| } | ||
|
|
||
| public boolean isObserved() { | ||
| return observed; | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
tests-arquillian/src/test/java/org/jboss/weld/tests/event/wildcard/contravariant/Widget.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package org.jboss.weld.tests.event.wildcard.contravariant; | ||
|
|
||
| public class Widget { | ||
|
|
||
| private final String name; | ||
|
|
||
| public Widget(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
...llian/src/test/java/org/jboss/weld/tests/event/wildcard/contravariant/WidgetObserver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package org.jboss.weld.tests.event.wildcard.contravariant; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.event.Observes; | ||
|
|
||
| @ApplicationScoped | ||
| public class WidgetObserver { | ||
|
|
||
| private boolean observed = false; | ||
|
|
||
| public void onWidget(@Observes Widget event) { | ||
| observed = true; | ||
| } | ||
|
|
||
| public boolean isObserved() { | ||
| return observed; | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
...n/src/test/java/org/jboss/weld/tests/event/wildcard/covariant/BeanWithCovariantEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package org.jboss.weld.tests.event.wildcard.covariant; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.event.Event; | ||
| import jakarta.inject.Inject; | ||
|
|
||
| @ApplicationScoped | ||
| public class BeanWithCovariantEvent { | ||
|
|
||
| @Inject | ||
| Event<? extends Widget> covariantEvent; | ||
| } |
35 changes: 35 additions & 0 deletions
35
...c/test/java/org/jboss/weld/tests/event/wildcard/covariant/EventCovariantWildcardTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package org.jboss.weld.tests.event.wildcard.covariant; | ||
|
|
||
| import jakarta.enterprise.inject.spi.DefinitionException; | ||
|
|
||
| import org.jboss.arquillian.container.test.api.Deployment; | ||
| import org.jboss.arquillian.container.test.api.ShouldThrowException; | ||
| import org.jboss.arquillian.junit.Arquillian; | ||
| import org.jboss.shrinkwrap.api.Archive; | ||
| import org.jboss.shrinkwrap.api.BeanArchive; | ||
| import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
| import org.jboss.weld.test.util.Utils; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| /** | ||
| * Verifies that {@code Event<? extends X>} injection points are rejected. | ||
| * Covariant wildcards on Event are useless because you cannot call | ||
| * {@code fire()} on them. | ||
| * | ||
| * @see <a href="https://github.com/jakartaee/cdi/issues/888">CDI #888</a> | ||
| */ | ||
| @RunWith(Arquillian.class) | ||
| public class EventCovariantWildcardTest { | ||
|
|
||
| @Deployment | ||
| @ShouldThrowException(DefinitionException.class) | ||
| public static Archive<?> getDeployment() { | ||
| return ShrinkWrap.create(BeanArchive.class, Utils.getDeploymentNameAsHash(EventCovariantWildcardTest.class)) | ||
| .addClasses(BeanWithCovariantEvent.class, Widget.class); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCovariantEventWildcardRejected() { | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
tests-arquillian/src/test/java/org/jboss/weld/tests/event/wildcard/covariant/Widget.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package org.jboss.weld.tests.event.wildcard.covariant; | ||
|
|
||
| public class Widget { | ||
| } |
12 changes: 12 additions & 0 deletions
12
...a/org/jboss/weld/tests/instance/wildcard/contravariant/BeanWithContravariantInstance.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package org.jboss.weld.tests.instance.wildcard.contravariant; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.inject.Instance; | ||
| import jakarta.inject.Inject; | ||
|
|
||
| @ApplicationScoped | ||
| public class BeanWithContravariantInstance { | ||
|
|
||
| @Inject | ||
| Instance<? super Widget> contravariantInstance; | ||
| } |
36 changes: 36 additions & 0 deletions
36
...g/jboss/weld/tests/instance/wildcard/contravariant/InstanceContravariantWildcardTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package org.jboss.weld.tests.instance.wildcard.contravariant; | ||
|
|
||
| import jakarta.enterprise.inject.spi.DefinitionException; | ||
|
|
||
| import org.jboss.arquillian.container.test.api.Deployment; | ||
| import org.jboss.arquillian.container.test.api.ShouldThrowException; | ||
| import org.jboss.arquillian.junit.Arquillian; | ||
| import org.jboss.shrinkwrap.api.Archive; | ||
| import org.jboss.shrinkwrap.api.BeanArchive; | ||
| import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
| import org.jboss.weld.test.util.Utils; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| /** | ||
| * Verifies that {@code Instance<? super X>} injection points are rejected. | ||
| * Contravariant wildcards on Instance are useless because Instance is | ||
| * naturally covariant. | ||
| * | ||
| * @see <a href="https://github.com/jakartaee/cdi/issues/888">CDI #888</a> | ||
| */ | ||
| @RunWith(Arquillian.class) | ||
| public class InstanceContravariantWildcardTest { | ||
|
|
||
| @Deployment | ||
| @ShouldThrowException(DefinitionException.class) | ||
| public static Archive<?> getDeployment() { | ||
| return ShrinkWrap.create(BeanArchive.class, | ||
| Utils.getDeploymentNameAsHash(InstanceContravariantWildcardTest.class)) | ||
| .addClasses(BeanWithContravariantInstance.class, Widget.class); | ||
| } | ||
|
|
||
| @Test | ||
| public void testContravariantInstanceWildcardRejected() { | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
...arquillian/src/test/java/org/jboss/weld/tests/instance/wildcard/contravariant/Widget.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package org.jboss.weld.tests.instance.wildcard.contravariant; | ||
|
|
||
| public class Widget { | ||
| } |
20 changes: 20 additions & 0 deletions
20
...test/java/org/jboss/weld/tests/instance/wildcard/covariant/BeanWithCovariantInstance.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.jboss.weld.tests.instance.wildcard.covariant; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.inject.Instance; | ||
| import jakarta.inject.Inject; | ||
|
|
||
| @ApplicationScoped | ||
| public class BeanWithCovariantInstance { | ||
|
|
||
| @Inject | ||
| Instance<? extends Widget> covariantInstance; | ||
|
|
||
| public boolean isResolvable() { | ||
| return covariantInstance.isResolvable(); | ||
| } | ||
|
|
||
| public Widget get() { | ||
| return covariantInstance.get(); | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
.../java/org/jboss/weld/tests/instance/wildcard/covariant/InstanceCovariantWildcardTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package org.jboss.weld.tests.instance.wildcard.covariant; | ||
|
|
||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import org.jboss.arquillian.container.test.api.Deployment; | ||
| import org.jboss.arquillian.junit.Arquillian; | ||
| import org.jboss.shrinkwrap.api.Archive; | ||
| import org.jboss.shrinkwrap.api.BeanArchive; | ||
| import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
| import org.jboss.weld.test.util.Utils; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| /** | ||
| * Verifies that {@code Instance<? extends X>} injection points are valid and | ||
| * functional. Instance is naturally covariant so an upper-bounded wildcard is | ||
| * a legitimate use case. | ||
| * | ||
| * @see <a href="https://github.com/jakartaee/cdi/issues/888">CDI #888</a> | ||
| */ | ||
| @RunWith(Arquillian.class) | ||
| public class InstanceCovariantWildcardTest { | ||
|
|
||
| @Deployment | ||
| public static Archive<?> getDeployment() { | ||
| return ShrinkWrap.create(BeanArchive.class, Utils.getDeploymentNameAsHash(InstanceCovariantWildcardTest.class)) | ||
| .addClasses(BeanWithCovariantInstance.class, Widget.class); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCovariantInstanceWildcardDeploys(BeanWithCovariantInstance bean) { | ||
| assertTrue("Instance<? extends Widget> should be resolvable", bean.isResolvable()); | ||
| Widget widget = bean.get(); | ||
| assertNotNull("Instance.get() should return a Widget", widget); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it's obvious, but clearly it's not, because you did the exact opposite of what I did :-) I'll have to specify this explicitly.