From 70d1dd12dda094dda142fd9d415afde17f5c7483 Mon Sep 17 00:00:00 2001 From: Jialin Chen Date: Thu, 15 Oct 2026 11:06:06 +0800 Subject: [PATCH] Make gRPC advice handler methods accessible Allow public exception handler methods declared on package-private advice classes to be invoked from another package. Add regression coverage for package-private advice and a public control. Fixes gh-442 Signed-off-by: Jialin Chen --- .../advice/GrpcAdviceExceptionHandler.java | 3 + .../GrpcAdviceExceptionHandlerTests.java | 24 ++++++++ .../support/AdviceVisibilitySamples.java | 58 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 spring-grpc-core/src/test/java/org/springframework/grpc/server/advice/support/AdviceVisibilitySamples.java diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/advice/GrpcAdviceExceptionHandler.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/advice/GrpcAdviceExceptionHandler.java index fc716232..8dba9cb9 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/advice/GrpcAdviceExceptionHandler.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/advice/GrpcAdviceExceptionHandler.java @@ -27,6 +27,7 @@ import org.jspecify.annotations.Nullable; import org.springframework.util.Assert; +import org.springframework.util.ReflectionUtils; import io.grpc.Metadata; import io.grpc.Status; @@ -38,6 +39,7 @@ * methods in {@link GrpcAdvice @GrpcAdvice} beans. * * @author Oleksandr Shevchenko + * @author Jialin Chen * @see GrpcAdvice * @see GrpcExceptionHandler */ @@ -168,6 +170,7 @@ private Class convertToClass(Parameter parameter) { private Object invokeMappedMethodSafely(Method mappedMethod, Object instanceOfMappedMethod, Object[] instancedParams) throws Throwable { try { + ReflectionUtils.makeAccessible(mappedMethod); return mappedMethod.invoke(instanceOfMappedMethod, instancedParams); } catch (InvocationTargetException ex) { diff --git a/spring-grpc-core/src/test/java/org/springframework/grpc/server/advice/GrpcAdviceExceptionHandlerTests.java b/spring-grpc-core/src/test/java/org/springframework/grpc/server/advice/GrpcAdviceExceptionHandlerTests.java index 1360b2f1..122213f1 100644 --- a/spring-grpc-core/src/test/java/org/springframework/grpc/server/advice/GrpcAdviceExceptionHandlerTests.java +++ b/spring-grpc-core/src/test/java/org/springframework/grpc/server/advice/GrpcAdviceExceptionHandlerTests.java @@ -26,6 +26,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.grpc.server.advice.support.AdviceVisibilitySamples; import io.grpc.Metadata; import io.grpc.Status; @@ -36,9 +37,32 @@ * Tests for {@link GrpcAdviceExceptionHandler}. * * @author Oleksandr Shevchenko + * @author Jialin Chen */ class GrpcAdviceExceptionHandlerTests { + @Test + void handlesExceptionInPublicAdviceFromAnotherPackage() { + assertAdviceHandlesException(AdviceVisibilitySamples.PublicAdvice.class); + } + + @Test + void handlesExceptionInPackagePrivateAdviceFromAnotherPackage() { + assertAdviceHandlesException(AdviceVisibilitySamples.packagePrivateAdviceType()); + } + + private void assertAdviceHandlesException(Class adviceType) { + try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(adviceType, + GrpcAdviceDiscoverer.class, GrpcExceptionHandlerMethodResolver.class, + GrpcAdviceExceptionHandler.class)) { + GrpcAdviceExceptionHandler handler = context.getBean(GrpcAdviceExceptionHandler.class); + StatusException result = handler.handleException(new IllegalArgumentException("bad argument")); + assertThat(result).isNotNull(); + assertThat(result.getStatus().getCode()).isEqualTo(Status.Code.INVALID_ARGUMENT); + assertThat(result.getStatus().getDescription()).isEqualTo("bad argument"); + } + } + @Test void handlesExceptionWithMappedHandler() { try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class)) { diff --git a/spring-grpc-core/src/test/java/org/springframework/grpc/server/advice/support/AdviceVisibilitySamples.java b/spring-grpc-core/src/test/java/org/springframework/grpc/server/advice/support/AdviceVisibilitySamples.java new file mode 100644 index 00000000..4925bc7e --- /dev/null +++ b/spring-grpc-core/src/test/java/org/springframework/grpc/server/advice/support/AdviceVisibilitySamples.java @@ -0,0 +1,58 @@ +/* + * Copyright 2026-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.grpc.server.advice.support; + +import org.springframework.grpc.server.advice.GrpcAdvice; +import org.springframework.grpc.server.advice.GrpcExceptionHandler; + +import io.grpc.Status; + +/** + * Advice fixtures in a different package from the exception handler implementation. + * + * @author Jialin Chen + */ +public final class AdviceVisibilitySamples { + + private AdviceVisibilitySamples() { + } + + public static Class packagePrivateAdviceType() { + return PackagePrivateAdvice.class; + } + + @GrpcAdvice + public static class PublicAdvice { + + @GrpcExceptionHandler + public Status handleIllegalArgument(IllegalArgumentException exception) { + return Status.INVALID_ARGUMENT.withDescription(exception.getMessage()); + } + + } + + @GrpcAdvice + static class PackagePrivateAdvice { + + @GrpcExceptionHandler + public Status handleIllegalArgument(IllegalArgumentException exception) { + return Status.INVALID_ARGUMENT.withDescription(exception.getMessage()); + } + + } + +}