Skip to content

Commit

Permalink
Merge pull request #35876 from alesj/i35634
Browse files Browse the repository at this point in the history
More hierarchical classes are not picked up as global interceptors
  • Loading branch information
cescoffier authored Sep 13, 2023
2 parents 08961a5 + d2b39f1 commit 3bfd85a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,41 @@ static GrpcInterceptors gatherInterceptors(IndexView index, DotName interceptorI
private static Set<DotName> allGlobalInterceptors(IndexView index, DotName interceptorInterface) {
Set<DotName> result = new HashSet<>();
for (AnnotationInstance instance : index.getAnnotations(GLOBAL_INTERCEPTOR)) {
ClassInfo classInfo = classInfo(index, instance.target());
if (isAssignableFrom(index, classInfo, interceptorInterface)) {
result.add(classInfo.name());
DotName className = className(instance.target());
if (isAssignableFrom(index, className, interceptorInterface)) {
result.add(className);
}
}
return result;
}

private static ClassInfo classInfo(IndexView index, AnnotationTarget target) {
private static DotName className(AnnotationTarget target) {
if (target.kind() == CLASS) {
return target.asClass();
return target.asClass().name();
} else if (target.kind() == METHOD) {
return index.getClassByName(target.asMethod().returnType().name());
return target.asMethod().returnType().name();
}
return null;
}

private static boolean isAssignableFrom(IndexView index, ClassInfo classInfo, DotName interceptorInterface) {
if (classInfo == null) {
private static boolean isAssignableFrom(IndexView index, DotName className, DotName interceptorInterface) {
if (className == null) {
return false;
}
if (classInfo.interfaceNames().contains(interceptorInterface)) {
return true;
}
if (classInfo.superName() == null) {
return false;

ClassInfo classInfo = index.getClassByName(className);
List<DotName> interfaceNames = classInfo.interfaceNames();
for (DotName in : interfaceNames) {
if (in.equals(interceptorInterface)) {
return true;
}
boolean result = isAssignableFrom(index, in, interceptorInterface);
if (result) {
return true;
}
}
return isAssignableFrom(index, index.getClassByName(classInfo.superName()), interceptorInterface);

return isAssignableFrom(index, classInfo.superName(), interceptorInterface);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.grpc.examples.interceptors;

import jakarta.enterprise.context.ApplicationScoped;

import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.quarkus.grpc.GlobalInterceptor;

@ApplicationScoped
@GlobalInterceptor
public class PriorityImplInterceptor implements PriorityInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
ServerCallHandler<ReqT, RespT> next) {
HelloWorldEndpoint.invoked.add(getClass().getName());
return next.startCall(call, headers);
}

@Override
public int getPriority() {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.grpc.examples.interceptors;

import jakarta.enterprise.inject.spi.Prioritized;

import io.grpc.ServerInterceptor;

public interface PriorityInterceptor extends ServerInterceptor, Prioritized {
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void testHelloWorldServiceUsingBlockingStub() {
assertThat(invoked).containsExactlyInAnyOrder(
"io.quarkus.grpc.examples.interceptors.ClientInterceptors$TypeTarget",
"io.quarkus.grpc.examples.interceptors.ClientInterceptors$MethodTarget",
"io.quarkus.grpc.examples.interceptors.PriorityImplInterceptor",
"io.quarkus.grpc.examples.interceptors.ServerInterceptors$TypeTarget",
"io.quarkus.grpc.examples.interceptors.ServerInterceptors$MethodTarget");

Expand Down

0 comments on commit 3bfd85a

Please sign in to comment.