Skip to content

Commit 07876ad

Browse files
committed
#52 Refactor moving the Class.forName() on priority annotation to earlier
1 parent c39e105 commit 07876ad

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,15 @@ public <T> List<T> getBeansByPriority(Class<T> interfaceType) {
9595

9696
@Override
9797
public <T> List<T> sortByPriority(List<T> list) {
98+
final Class<? extends Annotation> priorityAnnotation = priorityAnnotation();
99+
if (priorityAnnotation == null) {
100+
// priority annotation not on the classpath
101+
return list;
102+
}
98103
boolean priorityUsed = false;
99104
List<SortBean<T>> tempList = new ArrayList<>(list.size());
100105
for (T bean : list) {
101-
SortBean<T> sortBean = new SortBean<>(bean);
106+
SortBean<T> sortBean = new SortBean<>(bean, priorityAnnotation);
102107
tempList.add(sortBean);
103108
if (!priorityUsed && sortBean.priorityDefined) {
104109
priorityUsed = true;
@@ -117,6 +122,18 @@ public <T> List<T> sortByPriority(List<T> list) {
117122
return sorted;
118123
}
119124

125+
/**
126+
* Return the optional <code>javax.annotation.Priority</code> annotation class or null.
127+
*/
128+
@SuppressWarnings("unchecked")
129+
private Class<? extends Annotation> priorityAnnotation() {
130+
try {
131+
return (Class<? extends Annotation>) Class.forName("javax.annotation.Priority");
132+
} catch (ClassNotFoundException e) {
133+
return null;
134+
}
135+
}
136+
120137
@Override
121138
public List<Object> getBeansWithAnnotation(Class<?> annotation) {
122139

@@ -227,24 +244,21 @@ private static class SortBean<T> implements Comparable<SortBean<T>> {
227244

228245
private final int priority;
229246

230-
SortBean(T bean) {
247+
SortBean(T bean, Class<? extends Annotation> priorityAnnotation) {
231248
this.bean = bean;
232-
this.priority = initPriority();
249+
this.priority = initPriority(priorityAnnotation);
233250
}
234251

235-
int initPriority() {
236-
// Avoid adding hard dependency on javax.annotation-api by using reflection to find @Priority
252+
int initPriority(Class<? extends Annotation> priorityAnnotation) {
253+
// Avoid adding hard dependency on javax.annotation-api by using reflection
237254
try {
238-
Class<? extends Annotation> type = (Class<? extends Annotation>) Class.forName("javax.annotation.Priority");
239-
Annotation ann = bean.getClass().getAnnotation(type);
255+
Annotation ann = bean.getClass().getAnnotation(priorityAnnotation);
240256
if (ann != null) {
241-
int priority = (Integer) type.getMethod("value").invoke(ann);
257+
int priority = (Integer) priorityAnnotation.getMethod("value").invoke(ann);
242258
priorityDefined = true;
243259
return priority;
244260
}
245-
} catch (ClassNotFoundException ignore) {
246-
// @Priority not available, so just use default priority
247-
} catch (ReflectiveOperationException | SecurityException | IllegalArgumentException | ExceptionInInitializerError | ClassCastException e) {
261+
} catch (Exception e) {
248262
// If this happens, something has gone very wrong since a non-confirming @Priority was found...
249263
throw new UnsupportedOperationException("Problem instantiating @Priority", e);
250264
}

0 commit comments

Comments
 (0)