-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
ArC: detect unsupported Inject annotations #31012
Merged
Merged
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 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 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
21 changes: 21 additions & 0 deletions
21
...eployment/src/test/java/io/quarkus/arc/test/wrongannotations/BeanWithIncorrectInject.java
This file contains 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,21 @@ | ||
package io.quarkus.arc.test.wrongannotations; | ||
|
||
import jakarta.enterprise.inject.spi.BeanManager; | ||
|
||
import com.google.inject.Inject; | ||
|
||
public class BeanWithIncorrectInject { | ||
|
||
@Inject | ||
BeanManager bm1; | ||
|
||
@javax.inject.Inject | ||
BeanManager bm2; | ||
|
||
@com.oracle.svm.core.annotate.Inject | ||
BeanManager bm3; | ||
|
||
@org.gradle.internal.impldep.javax.inject.Inject | ||
BeanManager bm4; | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...ns/arc/deployment/src/test/java/io/quarkus/arc/test/wrongannotations/WrongInjectTest.java
This file contains 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,50 @@ | ||
package io.quarkus.arc.test.wrongannotations; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import javax.inject.Inject; | ||
|
||
import jakarta.enterprise.inject.spi.BeanManager; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.runtime.util.ExceptionUtil; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class WrongInjectTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(BeanWithIncorrectInject.class)) | ||
.assertException(t -> { | ||
Throwable rootCause = ExceptionUtil.getRootCause(t); | ||
assertTrue(rootCause.getMessage().contains( | ||
"@com.google.inject.Inject declared on io.quarkus.arc.test.wrongannotations.BeanWithIncorrectInject.bm1, use @jakarta.inject.Inject instead"), | ||
t.toString()); | ||
assertTrue(rootCause.getMessage().contains( | ||
"@javax.inject.Inject declared on io.quarkus.arc.test.wrongannotations.BeanWithIncorrectInject.bm2, use @jakarta.inject.Inject instead"), | ||
t.toString()); | ||
assertTrue(rootCause.getMessage().contains( | ||
"@com.oracle.svm.core.annotate.Inject declared on io.quarkus.arc.test.wrongannotations.BeanWithIncorrectInject.bm3, use @jakarta.inject.Inject instead"), | ||
t.toString()); | ||
assertTrue(rootCause.getMessage().contains( | ||
"@org.gradle.internal.impldep.javax.inject.Inject declared on io.quarkus.arc.test.wrongannotations.BeanWithIncorrectInject.bm4, use @jakarta.inject.Inject instead"), | ||
t.toString()); | ||
assertTrue(rootCause.getMessage().contains( | ||
"@javax.inject.Inject declared on io.quarkus.arc.test.wrongannotations.WrongInjectTest.beanManager, use @jakarta.inject.Inject instead"), | ||
t.toString()); | ||
}); | ||
|
||
@Inject | ||
BeanManager beanManager; | ||
|
||
@Test | ||
public void testValidationFailed() { | ||
// This method should not be invoked | ||
fail(); | ||
} | ||
|
||
} |
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.
Is there a reason why you didn't do it for let's say
@ApplicationScoped
?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.
@Inject
and@Singleton
are the most common ones and the problematic cases are mainly when you migrate to Quarkus 3 - the compiler won't tell you about these in tests (as seen from changed files).I am not aware of similar issue with
@ApplicationScoped
, at least not in basic dependency setup?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.
@ApplicationScoped
is CDI specific, so it's unlikely to be forked that much. I agree@Inject
and@Singleton
are the most important. We could add@Named
as well. I'm not sure about@Qualifier
and@Scope
.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.
Well, no reason really... except that if you use a wrong scope the bean class is just ignored and usually results in a build failure (because of an unsatisfied dependency) while if you use a wrong
@Inject
you can get NPEs at runtime.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.
Yeah OK. You will have
javax.inject
around in our test scope so I suppose it makes sense to limit ourselves to these.I was more thinking of someone who just upgraded the Quarkus version without changing the source code but they will have a ton of other issues anyway so I suppose we can ignore
@ApplicationScoped
.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.
We can try to address scopes in a separate pull request...