Skip to content

Commit

Permalink
Fixing error with composite StreamType
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashir Estela committed Mar 24, 2024
1 parent 4228bac commit 1ed98c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

public class ReturnType {
Expand Down Expand Up @@ -50,25 +51,30 @@ private void setClassByEventIfExists(Method method) {
this.classByEvent = calculateClassByEvent(
new StreamType[] { method.getDeclaredAnnotation(StreamType.class) });
} else {
var annotations = method.getDeclaredAnnotations();
if (isAnnotationPresent(annotations, StreamType.List.class)) {
var innerStreamTypeList = getInnerAnnotationIfExists(method, StreamType.List.class);
if (innerStreamTypeList.isPresent()) {
this.classByEvent = calculateClassByEvent(
Arrays.stream(annotations)
.map(a -> a.annotationType().getDeclaredAnnotation(StreamType.List.class).value())
.findFirst()
.orElse(null));
} else if (isAnnotationPresent(annotations, StreamType.class)) {
this.classByEvent = calculateClassByEvent(
new StreamType[] { Arrays.stream(annotations)
.map(a -> a.annotationType().getDeclaredAnnotation(StreamType.class))
.findFirst()
.orElse(null) });
innerStreamTypeList.get()
.annotationType()
.getDeclaredAnnotation(StreamType.List.class)
.value());
} else {
var innerStreamType = getInnerAnnotationIfExists(method, StreamType.class);
if (innerStreamType.isPresent()) {
this.classByEvent = calculateClassByEvent(
new StreamType[] { innerStreamType.get()
.annotationType()
.getDeclaredAnnotation(StreamType.class) });
}
}
}
}

private boolean isAnnotationPresent(Annotation[] annotations, Class<? extends Annotation> clazz) {
return Arrays.stream(annotations).anyMatch(a -> a.annotationType().isAnnotationPresent(clazz));
private Optional<? extends Annotation> getInnerAnnotationIfExists(Method method,
Class<? extends Annotation> clazz) {
return Arrays.stream(method.getDeclaredAnnotations())
.filter(a -> a.annotationType().isAnnotationPresent(clazz))
.findFirst();
}

private Map<String, Class<?>> calculateClassByEvent(StreamType[] streamTypeList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.sashirestela.cleverclient.support;

import io.github.sashirestela.cleverclient.Event;
import io.github.sashirestela.cleverclient.annotation.POST;
import org.junit.jupiter.api.Test;

import java.io.InputStream;
Expand Down Expand Up @@ -88,6 +89,7 @@ void shouldReturnMapClassByEventWhenTheMethodIsAnnotatedWithCompositeSingleStrea

static interface TestInterface {

@POST
@CompositeTwo
CompletableFuture<Stream<Event>> asyncStreamEventMethod();

Expand Down

0 comments on commit 1ed98c8

Please sign in to comment.