-
Notifications
You must be signed in to change notification settings - Fork 24
Determine Priority Annotations at Compile Time #852
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
43c0c71
Determine Priority annotations at compile time
SentryMan 5ba0f5a
Update BeanEntry.java
SentryMan 614c5b2
flip signs
SentryMan 9c0af63
never beat primary priority
SentryMan 4089b03
Update Builder.java
SentryMan a9bcbb6
Merge branch 'master' into priority
SentryMan f97c625
Rename Util.priority() method, some javadoc
rbygrave 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
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
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
23 changes: 23 additions & 0 deletions
23
inject-test/src/test/java/org/example/coffee/priority/base/PriorityFactory.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,23 @@ | ||
package org.example.coffee.priority.base; | ||
|
||
import io.avaje.inject.Bean; | ||
import io.avaje.inject.Factory; | ||
import io.avaje.inject.Priority; | ||
|
||
@Factory | ||
public class PriorityFactory { | ||
|
||
@Bean | ||
@Priority(69) | ||
BaseIface iface() { | ||
return new DBasei(); | ||
} | ||
|
||
public static class DBasei implements BaseIface { | ||
|
||
@Override | ||
public String other() { | ||
return "b"; | ||
} | ||
} | ||
} |
16 changes: 12 additions & 4 deletions
16
inject-test/src/test/java/org/example/coffee/priority/base/PriorityTest.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 |
---|---|---|
@@ -1,23 +1,31 @@ | ||
package org.example.coffee.priority.base; | ||
|
||
import io.avaje.inject.xtra.ApplicationScope; | ||
import org.junit.jupiter.api.Test; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import org.example.coffee.priority.base.PriorityFactory.DBasei; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class PriorityTest { | ||
import io.avaje.inject.xtra.ApplicationScope; | ||
|
||
class PriorityTest { | ||
|
||
@Test | ||
void listByPriority() { | ||
final List<BaseIface> sorted = ApplicationScope.listByPriority(BaseIface.class); | ||
assertExpectedOrder(sorted); | ||
} | ||
|
||
@Test | ||
void testGet() { | ||
assertThat(ApplicationScope.get(BaseIface.class)).isInstanceOf(CBasei.class); | ||
} | ||
|
||
private void assertExpectedOrder(List<BaseIface> sorted) { | ||
assertThat(sorted.get(0)).isInstanceOf(CBasei.class); | ||
assertThat(sorted.get(1)).isInstanceOf(BBasei.class); | ||
assertThat(sorted.get(2)).isInstanceOf(ABasei.class); | ||
assertThat(sorted.get(3)).isInstanceOf(DBasei.class); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,26 +1,26 @@ | ||
package io.avaje.inject; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
/** | ||
* The <code>Priority</code> annotation can be applied to classes to indicate | ||
* in what order they should be returned via @{@link BeanScope#listByPriority(Class)}. | ||
* <p> | ||
* Beans can be returned using other Priority annotation such as <code>javax.annotation.Priority</code> | ||
* or any custom priority annotation that has an <code>int value()</code> attribute. | ||
* </p> | ||
* The <code>Priority</code> annotation can be applied to classes to indicate the wiring priority of | ||
* a bean to resolve cases where multiple beans of the same type exist. | ||
* | ||
* <p>Beans can be returned using other Priority annotation such as <code> | ||
* jakartak.annotation.Priority | ||
* </code> or any custom priority annotation that has an <code>int value()</code> attribute. | ||
* | ||
* @see BeanScope#listByPriority(Class) | ||
* @see BeanScope#listByPriority(Class, Class) | ||
* @see BeanScope#listByPriority(Type) | ||
*/ | ||
@Documented | ||
@Retention(RUNTIME) | ||
@Target(TYPE) | ||
@Target({TYPE, METHOD}) | ||
public @interface Priority { | ||
int value(); | ||
} |
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
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.
So if it's got a
@Priority
also wire asProvider<T>
which has the effect of only instantiating when needed.Needed by .. being the highest priority or BeanScope#list or injecting a List/Set/Map of that type.
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.
Indeed, the appearance of a priority annotation invites the possibility that a higher priority bean might exist