Skip to content

Commit 5786de1

Browse files
committed
Tidy up listByAnnotation() methods
1 parent 53fa6a8 commit 5786de1

File tree

6 files changed

+47
-18
lines changed

6 files changed

+47
-18
lines changed

inject/src/main/java/io/avaje/inject/ApplicationScope.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
public class ApplicationScope {
3232

33-
private static final BeanScope rootContext = init();
33+
private static final BeanScope appScope = init();
3434

3535
private static BeanScope init() {
3636
return BeanScope.newBuilder().build();
@@ -44,7 +44,7 @@ private ApplicationScope() {
4444
* Return the underlying BeanContext.
4545
*/
4646
public static BeanScope scope() {
47-
return rootContext;
47+
return appScope;
4848
}
4949

5050
/**
@@ -60,7 +60,7 @@ public static BeanScope scope() {
6060
* @param type an interface or bean type
6161
*/
6262
public static <T> T get(Class<T> type) {
63-
return rootContext.get(type);
63+
return appScope.get(type);
6464
}
6565

6666
/**
@@ -77,7 +77,7 @@ public static <T> T get(Class<T> type) {
7777
* @param name the name qualifier of a specific bean
7878
*/
7979
public static <T> T get(Class<T> type, String name) {
80-
return rootContext.get(type, name);
80+
return appScope.get(type, name);
8181
}
8282

8383

@@ -95,7 +95,7 @@ public static <T> T get(Class<T> type, String name) {
9595
* @param interfaceType An interface class.
9696
*/
9797
public static <T> List<T> list(Class<T> interfaceType) {
98-
return rootContext.list(interfaceType);
98+
return appScope.list(interfaceType);
9999
}
100100

101101
/**
@@ -112,7 +112,29 @@ public static <T> List<T> list(Class<T> interfaceType) {
112112
* @param interfaceType An interface class.
113113
*/
114114
public static <T> List<T> listByPriority(Class<T> interfaceType) {
115-
return rootContext.listByPriority(interfaceType);
115+
return appScope.listByPriority(interfaceType);
116+
}
117+
118+
/**
119+
* Return the list of beans that have an annotation.
120+
*
121+
* <pre>{@code
122+
*
123+
* // e.g. register all controllers with web a framework
124+
* // .. where Controller is an annotation on the beans
125+
*
126+
* List<Object> controllers = ApplicationScope.listByAnnotation(Controller.class);
127+
*
128+
* }</pre>
129+
*
130+
* <p>
131+
* The classic use case for this is registering controllers or routes to
132+
* web frameworks like Sparkjava, Javlin, Rapidoid etc.
133+
*
134+
* @param annotation An annotation class.
135+
*/
136+
public static List<Object> listByAnnotation(Class<?> annotation) {
137+
return appScope.listByAnnotation(annotation);
116138
}
117139

118140
/**
@@ -121,6 +143,6 @@ public static <T> List<T> listByPriority(Class<T> interfaceType) {
121143
* @return The request scope builder
122144
*/
123145
public static RequestScopeBuilder newRequestScope() {
124-
return rootContext.newRequestScope();
146+
return appScope.newRequestScope();
125147
}
126148
}

inject/src/main/java/io/avaje/inject/BeanScope.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ default <T> T getBean(Class<T> type, String name) {
136136
* // e.g. register all controllers with web a framework
137137
* // .. where Controller is an annotation on the beans
138138
*
139-
* List<Object> controllers = SystemContext.getBeansWithAnnotation(Controller.class);
139+
* List<Object> controllers = ApplicationScope.listByAnnotation(Controller.class);
140140
*
141141
* }</pre>
142142
*
@@ -146,7 +146,15 @@ default <T> T getBean(Class<T> type, String name) {
146146
*
147147
* @param annotation An annotation class.
148148
*/
149-
List<Object> getBeansWithAnnotation(Class<?> annotation);
149+
List<Object> listByAnnotation(Class<?> annotation);
150+
151+
/**
152+
* Deprecated - migrate to listByAnnotation()
153+
*/
154+
@Deprecated
155+
default List<Object> getBeansWithAnnotation(Class<?> annotation) {
156+
return listByAnnotation(annotation);
157+
}
150158

151159
/**
152160
* Return the list of beans that implement the interface.
@@ -155,7 +163,7 @@ default <T> T getBean(Class<T> type, String name) {
155163
*
156164
* // e.g. register all routes for a web framework
157165
*
158-
* List<WebRoute> routes = SystemContext.getBeans(WebRoute.class);
166+
* List<WebRoute> routes = ApplicationScope.list(WebRoute.class);
159167
*
160168
* }</pre>
161169
*

inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ public <T> T get(Class<T> beanClass, String name) {
193193
}
194194

195195
@Override
196-
public List<Object> getBeansWithAnnotation(Class<?> annotation) {
197-
return context.getBeansWithAnnotation(annotation);
196+
public List<Object> listByAnnotation(Class<?> annotation) {
197+
return context.listByAnnotation(annotation);
198198
}
199199

200200
@Override

inject/src/main/java/io/avaje/inject/SystemContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public static <T> T getBean(Class<T> type, String name) {
5151
}
5252

5353
/**
54-
* Deprecated - removing support for this method.
54+
* Deprecated - migrate to ApplicationScope.listByAnnotation(annotation).
5555
*/
5656
@Deprecated
5757
public static List<Object> getBeansWithAnnotation(Class<?> annotation) {
58-
return ApplicationScope.scope().getBeansWithAnnotation(annotation);
58+
return ApplicationScope.listByAnnotation(annotation);
5959
}
6060

6161
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private <T> List<T> sortByPriority(List<T> list, final Class<? extends Annotati
9292
}
9393

9494
@Override
95-
public List<Object> getBeansWithAnnotation(Class<?> annotation) {
95+
public List<Object> listByAnnotation(Class<?> annotation) {
9696
return beans.all(annotation);
9797
}
9898

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,12 @@ public <T> Provider<T> getProvider(Class<T> type, String name) {
9191

9292
@Override
9393
public <T> List<T> getList(Class<T> interfaceType) {
94-
return beanScope.getBeans(interfaceType);
94+
return beanScope.list(interfaceType);
9595
}
9696

9797
@Override
9898
public <T> Set<T> getSet(Class<T> interfaceType) {
99-
//TODO Refactor this
100-
return new LinkedHashSet<>(beanScope.getBeans(interfaceType));
99+
return new LinkedHashSet<>(beanScope.list(interfaceType));
101100
}
102101

103102
@Override

0 commit comments

Comments
 (0)