Skip to content

Refactor/remove sort by methods #110

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 4 commits into from
May 15, 2021
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.example.coffee.priority.base;

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

import java.util.List;
Expand All @@ -11,20 +10,8 @@
public class PriorityTest {

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

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

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

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

void listByPriority() {
final List<BaseIface> sorted = ApplicationScope.listByPriority(BaseIface.class);
assertExpectedOrder(sorted);
}

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

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

import java.util.List;
Expand All @@ -12,10 +12,9 @@ public class CustomPriorityTest {

@Test
void test() {
final BeanScope context = SystemContext.context();
final BeanScope context = ApplicationScope.scope();

final List<OtherIface> beans = context.getBeans(OtherIface.class);
final List<OtherIface> sorted = context.sortByPriority(beans, CustomPriority.class);
final List<OtherIface> sorted = context.listByPriority(OtherIface.class, CustomPriority.class);

assertThat(sorted.get(0)).isInstanceOf(COtheri.class);
assertThat(sorted.get(1)).isInstanceOf(BOtheri.class);
Expand Down
38 changes: 30 additions & 8 deletions inject/src/main/java/io/avaje/inject/ApplicationScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public class ApplicationScope {

private static final BeanScope rootContext = init();
private static final BeanScope appScope = init();

private static BeanScope init() {
return BeanScope.newBuilder().build();
Expand All @@ -44,7 +44,7 @@ private ApplicationScope() {
* Return the underlying BeanContext.
*/
public static BeanScope scope() {
return rootContext;
return appScope;
}

/**
Expand All @@ -60,15 +60,15 @@ public static BeanScope scope() {
* @param type an interface or bean type
*/
public static <T> T get(Class<T> type) {
return rootContext.getBean(type);
return appScope.get(type);
}

/**
* Return a single bean given the type and name.
*
* <pre>{@code
*
* Heater heater = ApplicationScope.getBean(Heater.class, "electric");
* Heater heater = ApplicationScope.get(Heater.class, "electric");
* heater.heat();
*
* }</pre>
Expand All @@ -77,7 +77,7 @@ public static <T> T get(Class<T> type) {
* @param name the name qualifier of a specific bean
*/
public static <T> T get(Class<T> type, String name) {
return rootContext.getBean(type, name);
return appScope.get(type, name);
}


Expand All @@ -95,7 +95,7 @@ public static <T> T get(Class<T> type, String name) {
* @param interfaceType An interface class.
*/
public static <T> List<T> list(Class<T> interfaceType) {
return rootContext.getBeans(interfaceType);
return appScope.list(interfaceType);
}

/**
Expand All @@ -112,7 +112,29 @@ public static <T> List<T> list(Class<T> interfaceType) {
* @param interfaceType An interface class.
*/
public static <T> List<T> listByPriority(Class<T> interfaceType) {
return rootContext.getBeansByPriority(interfaceType);
return appScope.listByPriority(interfaceType);
}

/**
* Return the list of beans that have an annotation.
*
* <pre>{@code
*
* // e.g. register all controllers with web a framework
* // .. where Controller is an annotation on the beans
*
* List<Object> controllers = ApplicationScope.listByAnnotation(Controller.class);
*
* }</pre>
*
* <p>
* The classic use case for this is registering controllers or routes to
* web frameworks like Sparkjava, Javlin, Rapidoid etc.
*
* @param annotation An annotation class.
*/
public static List<Object> listByAnnotation(Class<?> annotation) {
return appScope.listByAnnotation(annotation);
}

/**
Expand All @@ -121,6 +143,6 @@ public static <T> List<T> listByPriority(Class<T> interfaceType) {
* @return The request scope builder
*/
public static RequestScopeBuilder newRequestScope() {
return rootContext.newRequestScope();
return appScope.newRequestScope();
}
}
43 changes: 13 additions & 30 deletions inject/src/main/java/io/avaje/inject/BeanScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static BeanScopeBuilder newBuilder() {
*
* <pre>{@code
*
* CoffeeMaker coffeeMaker = beanContext.getBean(CoffeeMaker.class);
* CoffeeMaker coffeeMaker = beanScope.get(CoffeeMaker.class);
* coffeeMaker.brew();
*
* }</pre>
Expand All @@ -110,7 +110,7 @@ default <T> T getBean(Class<T> type) {
*
* <pre>{@code
*
* Heater heater = beanContext.getBean(Heater.class, "electric");
* Heater heater = beanScope.get(Heater.class, "electric");
* heater.heat();
*
* }</pre>
Expand All @@ -136,7 +136,7 @@ default <T> T getBean(Class<T> type, String name) {
* // e.g. register all controllers with web a framework
* // .. where Controller is an annotation on the beans
*
* List<Object> controllers = SystemContext.getBeansWithAnnotation(Controller.class);
* List<Object> controllers = ApplicationScope.listByAnnotation(Controller.class);
*
* }</pre>
*
Expand All @@ -146,7 +146,15 @@ default <T> T getBean(Class<T> type, String name) {
*
* @param annotation An annotation class.
*/
List<Object> getBeansWithAnnotation(Class<?> annotation);
List<Object> listByAnnotation(Class<?> annotation);

/**
* Deprecated - migrate to listByAnnotation()
*/
@Deprecated
default List<Object> getBeansWithAnnotation(Class<?> annotation) {
return listByAnnotation(annotation);
}

/**
* Return the list of beans that implement the interface.
Expand All @@ -155,7 +163,7 @@ default <T> T getBean(Class<T> type, String name) {
*
* // e.g. register all routes for a web framework
*
* List<WebRoute> routes = SystemContext.getBeans(WebRoute.class);
* List<WebRoute> routes = ApplicationScope.list(WebRoute.class);
*
* }</pre>
*
Expand Down Expand Up @@ -203,26 +211,6 @@ default <T> List<T> getBeansByPriority(Class<T> interfaceType, Class<? extends A
return listByPriority(interfaceType, priority);
}

/**
* Sort the beans by javax.annotation.Priority annotation.
*
* @param list The beans to sort by priority
* @return A new list of beans sorted by priority
*/
<T> List<T> sortByPriority(List<T> list);

/**
* Sort the beans using the given Priority annotation.
* <p>
* The priority annotation will typically be either <code>javax.annotation.Priority</code>
* or <code>jakarta.annotation.Priority</code>.
*
* @param list The beans to sort by priority
* @param priority The priority annotation used to sort the beans
* @return A new list of beans sorted by priority
*/
<T> List<T> sortByPriority(List<T> list, final Class<? extends Annotation> priority);

/**
* Return a request scoped provided for the specific type and name.
*
Expand All @@ -232,11 +220,6 @@ default <T> List<T> getBeansByPriority(Class<T> interfaceType, Class<? extends A
*/
<T> RequestScopeMatch<T> requestProvider(Class<T> type, String name);

/**
* Start the context firing any <code>@PostConstruct</code> methods.
*/
void start();

/**
* Close the context firing any <code>@PreDestroy</code> methods.
*/
Expand Down
124 changes: 1 addition & 123 deletions inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,7 @@ public BeanScope build() {
for (BeanContextFactory factory : factoryOrder.factories()) {
factory.build(builder);
}

BeanScope beanScope = builder.build();
// entire graph built, fire postConstruct
beanScope.start();
if (shutdownHook) {
return new ShutdownAwareBeanScope(beanScope);
}
return beanScope;
return builder.build(shutdownHook);
}

/**
Expand All @@ -139,121 +132,6 @@ private Class<?> suppliedType(Class<?> suppliedClass) {
}
}

/**
* Internal shutdown hook.
*/
private static class Hook extends Thread {

private final ShutdownAwareBeanScope context;

Hook(ShutdownAwareBeanScope context) {
this.context = context;
}

@Override
public void run() {
context.shutdown();
}
}

/**
* Proxy that handles shutdown hook registration and de-registration.
*/
private static class ShutdownAwareBeanScope implements BeanScope {

private final ReentrantLock lock = new ReentrantLock();
private final BeanScope context;
private final Hook hook;
private boolean shutdown;

ShutdownAwareBeanScope(BeanScope context) {
this.context = context;
this.hook = new Hook(this);
Runtime.getRuntime().addShutdownHook(hook);
}

@Override
public RequestScopeBuilder newRequestScope() {
return context.newRequestScope();
}

@Override
public <T> RequestScopeMatch<T> requestProvider(Class<T> type, String name) {
return context.requestProvider(type, name);
}

@Override
public <T> T get(Class<T> beanClass) {
return context.get(beanClass);
}

@Override
public <T> T get(Class<T> beanClass, String name) {
return context.get(beanClass, name);
}

@Override
public List<Object> getBeansWithAnnotation(Class<?> annotation) {
return context.getBeansWithAnnotation(annotation);
}

@Override
public <T> List<T> list(Class<T> interfaceType) {
return context.list(interfaceType);
}

@Override
public <T> List<T> listByPriority(Class<T> interfaceType) {
return context.listByPriority(interfaceType);
}

@Override
public <T> List<T> listByPriority(Class<T> interfaceType, Class<? extends Annotation> priority) {
return context.listByPriority(interfaceType, priority);
}

@Override
public <T> List<T> sortByPriority(List<T> list) {
return context.sortByPriority(list);
}

@Override
public <T> List<T> sortByPriority(List<T> list, Class<? extends Annotation> priority) {
return context.sortByPriority(list, priority);
}

@Override
public void start() {
context.start();
}

@Override
public void close() {
lock.lock();
try {
if (!shutdown) {
Runtime.getRuntime().removeShutdownHook(hook);
}
context.close();
} finally {
lock.unlock();
}
}

/**
* Close via shutdown hook.
*/
void shutdown() {
lock.lock();
try {
shutdown = true;
close();
} finally {
lock.unlock();
}
}
}

/**
* Helper to order the BeanContextFactory based on dependsOn.
*/
Expand Down
4 changes: 2 additions & 2 deletions inject/src/main/java/io/avaje/inject/SystemContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public static <T> T getBean(Class<T> type, String name) {
}

/**
* Deprecated - removing support for this method.
* Deprecated - migrate to ApplicationScope.listByAnnotation(annotation).
*/
@Deprecated
public static List<Object> getBeansWithAnnotation(Class<?> annotation) {
return ApplicationScope.scope().getBeansWithAnnotation(annotation);
return ApplicationScope.listByAnnotation(annotation);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion inject/src/main/java/io/avaje/inject/spi/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,5 @@ static Builder newBuilder(List<SuppliedBean> suppliedBeans, List<EnrichBean> enr
/**
* Build and return the bean context.
*/
BeanScope build();
BeanScope build(boolean withShutdownHook);
}
Loading