Skip to content

Add @Priority and make it the default #73

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 1 commit into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions inject-test/src/test/java/io/avaje/inject/SystemContextTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.avaje.inject;

import org.example.coffee.fruit.Fruit;
import org.example.coffee.list.A2Somei;
import org.example.coffee.list.ASomei;
import org.example.coffee.list.BSomei;
import org.example.coffee.list.Somei;
Expand All @@ -16,28 +17,30 @@ public class SystemContextTest {
@Test
public void getBeansByPriority() {

final List<Somei> beans = SystemContext.getBeansByPriority(Somei.class);
assertThat(beans).hasSize(2);
final List<Somei> beans = SystemContext.context().getBeansByPriority(Somei.class, Priority.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test looks identical to getBeansByPriority_withAnnotation: should one of them be removed or tweaked?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'll merge and sort that out - thanks.

assertThat(beans).hasSize(3);

assertThat(beans.get(0)).isInstanceOf(BSomei.class);
assertThat(beans.get(1)).isInstanceOf(ASomei.class);
assertThat(beans.get(2)).isInstanceOf(A2Somei.class);
}

@Test
public void getBeansByPriority_withAnnotation() {

final List<Somei> beans = SystemContext.context().getBeansByPriority(Somei.class, Priority.class);
assertThat(beans).hasSize(2);
assertThat(beans).hasSize(3);

assertThat(beans.get(0)).isInstanceOf(BSomei.class);
assertThat(beans.get(1)).isInstanceOf(ASomei.class);
assertThat(beans.get(2)).isInstanceOf(A2Somei.class);
}

@Test
public void getBeansUnsorted_withPriority() {

final List<Somei> beans = SystemContext.getBeans(Somei.class);
assertThat(beans).hasSize(2);
assertThat(beans).hasSize(3);
// can't assert bean order
}

Expand Down
14 changes: 14 additions & 0 deletions inject-test/src/test/java/org/example/coffee/list/A2Somei.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.example.coffee.list;

import javax.annotation.Priority;
import javax.inject.Singleton;

@Singleton
@Priority(22)
public class A2Somei implements Somei {

@Override
public String some() {
return "a";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.example.coffee.priority.base;

import io.avaje.inject.Priority;

import javax.inject.Singleton;

@Singleton
@Priority(42)
public class ABasei implements BaseIface {

@Override
public String other() {
return "a";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.example.coffee.priority.base;

import io.avaje.inject.Priority;

import javax.inject.Singleton;

@Singleton
@Priority(7)
public class BBasei implements BaseIface {

@Override
public String other() {
return "b";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.example.coffee.priority.base;

public interface BaseIface {

String other();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.example.coffee.priority.base;

import io.avaje.inject.Priority;

import javax.inject.Singleton;

@Singleton
@Priority(1)
public class CBasei implements BaseIface {

@Override
public String other() {
return "a";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit, but should this be return "c";?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it should, it will be nice to fix it - thanks

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.example.coffee.priority.base;

import io.avaje.inject.BeanContext;
import io.avaje.inject.SystemContext;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class PriorityTest {

@Test
void getBeansByPriority() {
final BeanContext context = SystemContext.context();

final List<BaseIface> beans = context.getBeansByPriority(BaseIface.class);
assertExpectedOrder(beans);
}

@Test
void sortByPriority() {
final BeanContext context = SystemContext.context();

final List<BaseIface> beans = context.getBeans(BaseIface.class);
final List<BaseIface> sorted = context.sortByPriority(beans);

assertExpectedOrder(sorted);
}

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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.example.coffee.priority;
package org.example.coffee.priority.custom;

import javax.inject.Singleton;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.example.coffee.priority;
package org.example.coffee.priority.custom;

import javax.inject.Singleton;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.example.coffee.priority;
package org.example.coffee.priority.custom;

import javax.inject.Singleton;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.example.coffee.priority;
package org.example.coffee.priority.custom;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.example.coffee.priority;
package org.example.coffee.priority.custom;

import io.avaje.inject.BeanContext;
import io.avaje.inject.SystemContext;
Expand All @@ -19,7 +19,7 @@ void test() {

assertThat(sorted.get(0)).isInstanceOf(COtheri.class);
assertThat(sorted.get(1)).isInstanceOf(BOtheri.class);
assertThat(sorted.get(1)).isInstanceOf(AOtheri.class);
assertThat(sorted.get(2)).isInstanceOf(AOtheri.class);

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.example.coffee.priority;
package org.example.coffee.priority.custom;

public interface OtherIface {

Expand Down
28 changes: 28 additions & 0 deletions inject/src/main/java/io/avaje/inject/Priority.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.avaje.inject;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.List;

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 SystemContext#getBeansByPriority(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>
*
* @see BeanContext#getBeansByPriority(Class)
* @see BeanContext#getBeansByPriority(Class, Class)
* @see BeanContext#sortByPriority(List, Class)
*/
@Documented
@Retention(RUNTIME)
@Target(TYPE)
public @interface Priority {
int value();
}
17 changes: 3 additions & 14 deletions inject/src/main/java/io/avaje/inject/spi/DBeanContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.avaje.inject.BeanContext;
import io.avaje.inject.BeanEntry;
import io.avaje.inject.Priority;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

@Override
public <T> List<T> getBeansByPriority(Class<T> interfaceType) {
return getBeansByPriority(interfaceType, priorityAnnotation());
return getBeansByPriority(interfaceType, Priority.class);
}

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

@Override
public <T> List<T> sortByPriority(List<T> list) {
return sortByPriority(list, priorityAnnotation());
return sortByPriority(list, Priority.class);
}

@Override
Expand Down Expand Up @@ -131,18 +132,6 @@ public <T> List<T> sortByPriority(List<T> list, final Class<? extends Annotation
return sorted;
}

/**
* Return the optional <code>javax.annotation.Priority</code> annotation class or null.
*/
@SuppressWarnings("unchecked")
private Class<? extends Annotation> priorityAnnotation() {
try {
return (Class<? extends Annotation>) Class.forName("javax.annotation.Priority");
} catch (ClassNotFoundException e) {
return null;
}
}

@Override
public List<Object> getBeansWithAnnotation(Class<?> annotation) {

Expand Down