Skip to content

Commit

Permalink
Use reflection to find javax.annotation.Priority, thus making the jav…
Browse files Browse the repository at this point in the history
…ax.annotation-api dependency optional. (#52)
  • Loading branch information
norrisjeremy authored Oct 26, 2020
1 parent 59dd968 commit c39e105
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
6 changes: 0 additions & 6 deletions inject/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@
<version>1</version>
</dependency>

<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
20 changes: 15 additions & 5 deletions inject/src/main/java/io/avaje/inject/core/DBeanContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Priority;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -233,10 +233,20 @@ private static class SortBean<T> implements Comparable<SortBean<T>> {
}

int initPriority() {
final Priority ann = bean.getClass().getAnnotation(Priority.class);
if (ann != null) {
priorityDefined = true;
return ann.value();
// Avoid adding hard dependency on javax.annotation-api by using reflection to find @Priority
try {
Class<? extends Annotation> type = (Class<? extends Annotation>) Class.forName("javax.annotation.Priority");
Annotation ann = bean.getClass().getAnnotation(type);
if (ann != null) {
int priority = (Integer) type.getMethod("value").invoke(ann);
priorityDefined = true;
return priority;
}
} catch (ClassNotFoundException ignore) {
// @Priority not available, so just use default priority
} catch (ReflectiveOperationException | SecurityException | IllegalArgumentException | ExceptionInInitializerError | ClassCastException e) {
// If this happens, something has gone very wrong since a non-confirming @Priority was found...
throw new UnsupportedOperationException("Problem instantiating @Priority", e);
}
// Default priority as per javax.ws.rs.Priorities.USER
// User-level filter/interceptor priority
Expand Down

0 comments on commit c39e105

Please sign in to comment.