Skip to content

Commit bbb3f11

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

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-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: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
77

8-
import javax.annotation.Priority;
8+
import java.lang.annotation.Annotation;
99
import java.util.ArrayList;
1010
import java.util.Collections;
1111
import java.util.List;
@@ -233,10 +233,21 @@ private static class SortBean<T> implements Comparable<SortBean<T>> {
233233
}
234234

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

0 commit comments

Comments
 (0)