Skip to content

Commit 3b95132

Browse files
authored
Add @priority and make it the default (#73)
1 parent 2227272 commit 3b95132

File tree

15 files changed

+146
-25
lines changed

15 files changed

+146
-25
lines changed

inject-test/src/test/java/io/avaje/inject/SystemContextTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.avaje.inject;
22

33
import org.example.coffee.fruit.Fruit;
4+
import org.example.coffee.list.A2Somei;
45
import org.example.coffee.list.ASomei;
56
import org.example.coffee.list.BSomei;
67
import org.example.coffee.list.Somei;
@@ -16,28 +17,30 @@ public class SystemContextTest {
1617
@Test
1718
public void getBeansByPriority() {
1819

19-
final List<Somei> beans = SystemContext.getBeansByPriority(Somei.class);
20-
assertThat(beans).hasSize(2);
20+
final List<Somei> beans = SystemContext.context().getBeansByPriority(Somei.class, Priority.class);
21+
assertThat(beans).hasSize(3);
2122

2223
assertThat(beans.get(0)).isInstanceOf(BSomei.class);
2324
assertThat(beans.get(1)).isInstanceOf(ASomei.class);
25+
assertThat(beans.get(2)).isInstanceOf(A2Somei.class);
2426
}
2527

2628
@Test
2729
public void getBeansByPriority_withAnnotation() {
2830

2931
final List<Somei> beans = SystemContext.context().getBeansByPriority(Somei.class, Priority.class);
30-
assertThat(beans).hasSize(2);
32+
assertThat(beans).hasSize(3);
3133

3234
assertThat(beans.get(0)).isInstanceOf(BSomei.class);
3335
assertThat(beans.get(1)).isInstanceOf(ASomei.class);
36+
assertThat(beans.get(2)).isInstanceOf(A2Somei.class);
3437
}
3538

3639
@Test
3740
public void getBeansUnsorted_withPriority() {
3841

3942
final List<Somei> beans = SystemContext.getBeans(Somei.class);
40-
assertThat(beans).hasSize(2);
43+
assertThat(beans).hasSize(3);
4144
// can't assert bean order
4245
}
4346

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.example.coffee.list;
2+
3+
import javax.annotation.Priority;
4+
import javax.inject.Singleton;
5+
6+
@Singleton
7+
@Priority(22)
8+
public class A2Somei implements Somei {
9+
10+
@Override
11+
public String some() {
12+
return "a";
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.example.coffee.priority.base;
2+
3+
import io.avaje.inject.Priority;
4+
5+
import javax.inject.Singleton;
6+
7+
@Singleton
8+
@Priority(42)
9+
public class ABasei implements BaseIface {
10+
11+
@Override
12+
public String other() {
13+
return "a";
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.example.coffee.priority.base;
2+
3+
import io.avaje.inject.Priority;
4+
5+
import javax.inject.Singleton;
6+
7+
@Singleton
8+
@Priority(7)
9+
public class BBasei implements BaseIface {
10+
11+
@Override
12+
public String other() {
13+
return "b";
14+
}
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.example.coffee.priority.base;
2+
3+
public interface BaseIface {
4+
5+
String other();
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.example.coffee.priority.base;
2+
3+
import io.avaje.inject.Priority;
4+
5+
import javax.inject.Singleton;
6+
7+
@Singleton
8+
@Priority(1)
9+
public class CBasei implements BaseIface {
10+
11+
@Override
12+
public String other() {
13+
return "a";
14+
}
15+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.example.coffee.priority.base;
2+
3+
import io.avaje.inject.BeanContext;
4+
import io.avaje.inject.SystemContext;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.List;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
public class PriorityTest {
12+
13+
@Test
14+
void getBeansByPriority() {
15+
final BeanContext context = SystemContext.context();
16+
17+
final List<BaseIface> beans = context.getBeansByPriority(BaseIface.class);
18+
assertExpectedOrder(beans);
19+
}
20+
21+
@Test
22+
void sortByPriority() {
23+
final BeanContext context = SystemContext.context();
24+
25+
final List<BaseIface> beans = context.getBeans(BaseIface.class);
26+
final List<BaseIface> sorted = context.sortByPriority(beans);
27+
28+
assertExpectedOrder(sorted);
29+
}
30+
31+
private void assertExpectedOrder(List<BaseIface> sorted) {
32+
assertThat(sorted.get(0)).isInstanceOf(CBasei.class);
33+
assertThat(sorted.get(1)).isInstanceOf(BBasei.class);
34+
assertThat(sorted.get(2)).isInstanceOf(ABasei.class);
35+
}
36+
}

inject-test/src/test/java/org/example/coffee/priority/AOtheri.java renamed to inject-test/src/test/java/org/example/coffee/priority/custom/AOtheri.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.example.coffee.priority;
1+
package org.example.coffee.priority.custom;
22

33
import javax.inject.Singleton;
44

inject-test/src/test/java/org/example/coffee/priority/BOtheri.java renamed to inject-test/src/test/java/org/example/coffee/priority/custom/BOtheri.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.example.coffee.priority;
1+
package org.example.coffee.priority.custom;
22

33
import javax.inject.Singleton;
44

inject-test/src/test/java/org/example/coffee/priority/COtheri.java renamed to inject-test/src/test/java/org/example/coffee/priority/custom/COtheri.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.example.coffee.priority;
1+
package org.example.coffee.priority.custom;
22

33
import javax.inject.Singleton;
44

inject-test/src/test/java/org/example/coffee/priority/CustomPriority.java renamed to inject-test/src/test/java/org/example/coffee/priority/custom/CustomPriority.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.example.coffee.priority;
1+
package org.example.coffee.priority.custom;
22

33
import java.lang.annotation.Retention;
44
import java.lang.annotation.Target;

inject-test/src/test/java/org/example/coffee/priority/CustomPriorityTest.java renamed to inject-test/src/test/java/org/example/coffee/priority/custom/CustomPriorityTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.example.coffee.priority;
1+
package org.example.coffee.priority.custom;
22

33
import io.avaje.inject.BeanContext;
44
import io.avaje.inject.SystemContext;
@@ -19,7 +19,7 @@ void test() {
1919

2020
assertThat(sorted.get(0)).isInstanceOf(COtheri.class);
2121
assertThat(sorted.get(1)).isInstanceOf(BOtheri.class);
22-
assertThat(sorted.get(1)).isInstanceOf(AOtheri.class);
22+
assertThat(sorted.get(2)).isInstanceOf(AOtheri.class);
2323

2424
}
2525
}

inject-test/src/test/java/org/example/coffee/priority/OtherIface.java renamed to inject-test/src/test/java/org/example/coffee/priority/custom/OtherIface.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.example.coffee.priority;
1+
package org.example.coffee.priority.custom;
22

33
public interface OtherIface {
44

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.avaje.inject;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.Target;
6+
import java.util.List;
7+
8+
import static java.lang.annotation.ElementType.TYPE;
9+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
10+
11+
/**
12+
* The <code>Priority</code> annotation can be applied to classes to indicate
13+
* in what order they should be returned via @{@link SystemContext#getBeansByPriority(Class)}.
14+
* <p>
15+
* Beans can be returned using other Priority annotation such as <code>javax.annotation.Priority</code>
16+
* or any custom priority annotation that has an <code>int value()</code> attribute.
17+
* </p>
18+
*
19+
* @see BeanContext#getBeansByPriority(Class)
20+
* @see BeanContext#getBeansByPriority(Class, Class)
21+
* @see BeanContext#sortByPriority(List, Class)
22+
*/
23+
@Documented
24+
@Retention(RUNTIME)
25+
@Target(TYPE)
26+
public @interface Priority {
27+
int value();
28+
}

inject/src/main/java/io/avaje/inject/spi/DBeanContext.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.avaje.inject.BeanContext;
44
import io.avaje.inject.BeanEntry;
5+
import io.avaje.inject.Priority;
56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
78

@@ -89,7 +90,7 @@ public <T> List<T> getBeans(Class<T> interfaceType) {
8990

9091
@Override
9192
public <T> List<T> getBeansByPriority(Class<T> interfaceType) {
92-
return getBeansByPriority(interfaceType, priorityAnnotation());
93+
return getBeansByPriority(interfaceType, Priority.class);
9394
}
9495

9596
@Override
@@ -100,7 +101,7 @@ public <T> List<T> getBeansByPriority(Class<T> interfaceType, Class<? extends An
100101

101102
@Override
102103
public <T> List<T> sortByPriority(List<T> list) {
103-
return sortByPriority(list, priorityAnnotation());
104+
return sortByPriority(list, Priority.class);
104105
}
105106

106107
@Override
@@ -131,18 +132,6 @@ public <T> List<T> sortByPriority(List<T> list, final Class<? extends Annotation
131132
return sorted;
132133
}
133134

134-
/**
135-
* Return the optional <code>javax.annotation.Priority</code> annotation class or null.
136-
*/
137-
@SuppressWarnings("unchecked")
138-
private Class<? extends Annotation> priorityAnnotation() {
139-
try {
140-
return (Class<? extends Annotation>) Class.forName("javax.annotation.Priority");
141-
} catch (ClassNotFoundException e) {
142-
return null;
143-
}
144-
}
145-
146135
@Override
147136
public List<Object> getBeansWithAnnotation(Class<?> annotation) {
148137

0 commit comments

Comments
 (0)