Skip to content

Commit

Permalink
feat(core): only detect listeners written by a developer
Browse files Browse the repository at this point in the history
(Bridge) methods generated by the compiler (java) or for example synthetic kotlin suspend methods should be skipped.

These are placed next to the method written by the developer, have a cryptic method postfix and would otherwise show up as duplicate in the Springwolf documentation.

Note: All bridge methods are also synthetic methods.
  • Loading branch information
timonback committed May 27, 2024
1 parent 7a2b5cf commit d9d3c2d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Stream<Map.Entry<String, ChannelObject>> scan(Class<?> clazz) {
methodAnnotationClass.getName());

return Arrays.stream(clazz.getDeclaredMethods())
.filter(method -> !method.isBridge())
.filter(AnnotationScannerUtil::isMethodInSourceCode)
.filter(method -> AnnotationScannerUtil.findAnnotation(methodAnnotationClass, method) != null)
.map(this::mapMethodToChannel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected Stream<MethodAndAnnotation<A>> getAnnotatedMethods(Class<?> type) {
log.debug("Scanning class \"{}\" for @\"{}\" annotated methods", type.getName(), annotationClass.getName());

return Arrays.stream(ReflectionUtils.getAllDeclaredMethods(type))
.filter(method -> !method.isBridge())
.filter(AnnotationScannerUtil::isMethodInSourceCode)
.filter(method -> AnnotationScannerUtil.findAnnotation(annotationClass, method) != null)
.peek(method -> log.debug("Mapping method \"{}\" to channels", method.getName()))
.flatMap(method -> AnnotationScannerUtil.findAnnotations(annotationClass, method).stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.github.springwolf.core.asyncapi.scanners.operations.annotations.SpringAnnotationClassLevelOperationsScanner;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -58,8 +57,8 @@ protected Set<Method> getAnnotatedMethods(Class<?> clazz) {
methodAnnotationClass.getName());

return Arrays.stream(clazz.getDeclaredMethods())
.filter(method -> !method.isBridge())
.filter(method -> AnnotationUtils.findAnnotation(method, methodAnnotationClass) != null)
.filter(AnnotationScannerUtil::isMethodInSourceCode)
.filter(method -> AnnotationScannerUtil.findAnnotation(methodAnnotationClass, method) != null)
.collect(toSet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Set;

public class AnnotationScannerUtil {

private AnnotationScannerUtil() {}

/**
* Check that a method was written by a developer and not generated by the compiler.
*/
public static boolean isMethodInSourceCode(Method method) {
return !method.isSynthetic();
}

public static <T extends Annotation> T findAnnotationOrThrow(Class<T> annotationClass, AnnotatedElement element) {
T annotation = findAnnotation(annotationClass, element);
if (annotation == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Stream<Map.Entry<String, Operation>> scan(Class<?> clazz) {
methodAnnotationClass.getName());

return Arrays.stream(clazz.getDeclaredMethods())
.filter(method -> !method.isBridge())
.filter(AnnotationScannerUtil::isMethodInSourceCode)
.filter(method -> AnnotationScannerUtil.findAnnotation(methodAnnotationClass, method) != null)
.map(this::mapMethodToOperation);
}
Expand Down

0 comments on commit d9d3c2d

Please sign in to comment.