Skip to content

Commit b4a3f61

Browse files
committed
Use reflection to find javax.annotation.Priority, thus making the javax.annotation-api dependency optional.
1 parent 59dd968 commit b4a3f61

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

inject/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@
2929
<version>1</version>
3030
</dependency>
3131

32-
<dependency>
33-
<groupId>javax.annotation</groupId>
34-
<artifactId>javax.annotation-api</artifactId>
35-
<version>1.3.2</version>
36-
</dependency>
37-
3832
<dependency>
3933
<groupId>org.slf4j</groupId>
4034
<artifactId>slf4j-api</artifactId>

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77

8-
import javax.annotation.Priority;
8+
import java.lang.annotation.Annotation;
9+
import java.lang.reflect.InvocationTargetException;
910
import java.util.ArrayList;
1011
import java.util.Collections;
1112
import java.util.List;
@@ -233,10 +234,21 @@ private static class SortBean<T> implements Comparable<SortBean<T>> {
233234
}
234235

235236
int initPriority() {
236-
final Priority ann = bean.getClass().getAnnotation(Priority.class);
237-
if (ann != null) {
238-
priorityDefined = true;
239-
return ann.value();
237+
// Avoid adding hard dependency on annotation-api by using reflection
238+
for (Annotation ann : bean.getClass().getAnnotations()) {
239+
Class<? extends Annotation> type = ann.annotationType();
240+
String name = type.getName();
241+
if (!"javax.annotation.Priority".equals(name)) {
242+
continue;
243+
}
244+
try {
245+
int priority = (Integer) type.getMethod("value").invoke(ann);
246+
priorityDefined = true;
247+
return priority;
248+
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ExceptionInInitializerError | ClassCastException e) {
249+
// If this happens, something has gone very wrong...
250+
throw new UnsupportedOperationException(e);
251+
}
240252
}
241253
// Default priority as per javax.ws.rs.Priorities.USER
242254
// User-level filter/interceptor priority

0 commit comments

Comments
 (0)