Skip to content

Commit 09b9b93

Browse files
authored
Sync with databind/5244 wrt Guice module (#307)
1 parent dd56603 commit 09b9b93

2 files changed

Lines changed: 25 additions & 30 deletions

File tree

guice/src/main/java/tools/jackson/module/guice/GuiceAnnotationIntrospector.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package tools.jackson.module.guice;
22

3+
import java.lang.annotation.Annotation;
4+
import java.util.stream.Stream;
5+
import javax.inject.Qualifier;
6+
37
import com.fasterxml.jackson.annotation.JacksonInject;
48
import tools.jackson.databind.cfg.MapperConfig;
59
import tools.jackson.databind.introspect.*;
610
import com.google.inject.BindingAnnotation;
711
import com.google.inject.Key;
812

9-
import javax.inject.Qualifier;
10-
import java.lang.annotation.Annotation;
11-
import java.util.Arrays;
12-
1313
public class GuiceAnnotationIntrospector extends NopAnnotationIntrospector
1414
{
1515
private static final long serialVersionUID = 1L;
@@ -50,8 +50,7 @@ private Object _findGuiceInjectId(AnnotatedMember m)
5050
// On fields and parameters the @Qualifier annotation and type to
5151
// inject are the member itself, so, nothing to do here...
5252
guiceMember = m;
53-
AnnotationMap anns = ((AnnotatedMember) m).getAllAnnotations();
54-
guiceAnnotation = findBindingAnnotation(anns.annotations());
53+
guiceAnnotation = findBindingAnnotation(m.annotations());
5554
} else if (m instanceof AnnotatedMethod) {
5655
/* For method injection, the @Qualifier and type to inject are
5756
* specified on the parameter. Here, we only consider methods with
@@ -68,7 +67,7 @@ private Object _findGuiceInjectId(AnnotatedMember m)
6867
*/
6968
guiceMember = a.getParameter(0);
7069
final Annotation[] annotations = a.getMember().getParameterAnnotations()[0];
71-
guiceAnnotation = findBindingAnnotation(Arrays.asList(annotations));
70+
guiceAnnotation = findBindingAnnotation(Stream.of(annotations));
7271
} else {
7372
/* Ignore constructors */
7473
return null;
@@ -93,16 +92,14 @@ private Object _findGuiceInjectId(AnnotatedMember m)
9392
* annotation are present on what we're trying to inject.
9493
* Those annotations are only possible on fields or parameters.
9594
*/
96-
private Annotation findBindingAnnotation(Iterable<Annotation> annotations)
95+
private Annotation findBindingAnnotation(Stream<Annotation> annotations)
9796
{
98-
for (Annotation annotation : annotations) {
99-
// Check on guice (BindingAnnotation) & javax (Qualifier) based injections
100-
if (annotation.annotationType().isAnnotationPresent(BindingAnnotation.class) ||
101-
annotation.annotationType().isAnnotationPresent(Qualifier.class))
102-
{
103-
return annotation;
104-
}
105-
}
106-
return null;
97+
return annotations.filter(ann -> {
98+
Class<?> type = ann.annotationType();
99+
return type.isAnnotationPresent(BindingAnnotation.class)
100+
|| type.isAnnotationPresent(Qualifier.class);
101+
})
102+
.findFirst()
103+
.orElse(null);
107104
}
108105
}

guice7/src/main/java/tools/jackson/module/guice7/GuiceAnnotationIntrospector.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import jakarta.inject.Qualifier;
1212
import java.lang.annotation.Annotation;
1313
import java.util.Arrays;
14+
import java.util.stream.Stream;
1415

1516
public class GuiceAnnotationIntrospector extends NopAnnotationIntrospector
1617
{
@@ -53,8 +54,7 @@ private Object _findGuiceInjectId(AnnotatedMember m)
5354
* inject are the member itself, so, nothing to do here...
5455
*/
5556
guiceMember = m;
56-
AnnotationMap anns = ((AnnotatedMember) m).getAllAnnotations();
57-
guiceAnnotation = findBindingAnnotation(anns.annotations());
57+
guiceAnnotation = findBindingAnnotation(m.annotations());
5858
} else if (m instanceof AnnotatedMethod) {
5959
/* For method injection, the @Qualifier and type to inject are
6060
* specified on the parameter. Here, we only consider methods with
@@ -71,7 +71,7 @@ private Object _findGuiceInjectId(AnnotatedMember m)
7171
*/
7272
guiceMember = a.getParameter(0);
7373
final Annotation[] annotations = a.getMember().getParameterAnnotations()[0];
74-
guiceAnnotation = findBindingAnnotation(Arrays.asList(annotations));
74+
guiceAnnotation = findBindingAnnotation(Stream.of(annotations));
7575
} else {
7676
/* Ignore constructors */
7777
return null;
@@ -96,16 +96,14 @@ private Object _findGuiceInjectId(AnnotatedMember m)
9696
* annotation are present on what we're trying to inject.
9797
* Those annotations are only possible on fields or parameters.
9898
*/
99-
private Annotation findBindingAnnotation(Iterable<Annotation> annotations)
99+
private Annotation findBindingAnnotation(Stream<Annotation> annotations)
100100
{
101-
for (Annotation annotation : annotations) {
102-
// Check on guice (BindingAnnotation) & jakarta (Qualifier) based injections
103-
if (annotation.annotationType().isAnnotationPresent(BindingAnnotation.class) ||
104-
annotation.annotationType().isAnnotationPresent(Qualifier.class))
105-
{
106-
return annotation;
107-
}
108-
}
109-
return null;
101+
return annotations.filter(ann -> {
102+
Class<?> type = ann.annotationType();
103+
return type.isAnnotationPresent(BindingAnnotation.class)
104+
|| type.isAnnotationPresent(Qualifier.class);
105+
})
106+
.findFirst()
107+
.orElse(null);
110108
}
111109
}

0 commit comments

Comments
 (0)