From 0d7dfc9d3a8b6c1a21687a616a40c454974c2a30 Mon Sep 17 00:00:00 2001 From: RuanSheng Date: Sat, 1 Jul 2023 20:57:42 +0800 Subject: [PATCH] [2022.x] Add SentinelProtectInterceptor AOT hints (#3370) * Sentinel AOT: add SentinelProtectInterceptorHints --- .../hint/SentinelProtectInterceptorHints.java | 44 ++++++++++++++++ .../resources/META-INF/spring/aot.factories | 2 + .../SentinelProtectInterceptorHintsTest.java | 51 +++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/aot/hint/SentinelProtectInterceptorHints.java create mode 100644 spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/resources/META-INF/spring/aot.factories create mode 100644 spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/aot/hint/SentinelProtectInterceptorHintsTest.java diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/aot/hint/SentinelProtectInterceptorHints.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/aot/hint/SentinelProtectInterceptorHints.java new file mode 100644 index 000000000..8488fd319 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/aot/hint/SentinelProtectInterceptorHints.java @@ -0,0 +1,44 @@ +/* + * Copyright 2013-2023 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 com.alibaba.cloud.sentinel.aot.hint; + +import java.lang.reflect.Constructor; + +import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate; +import com.alibaba.cloud.sentinel.custom.SentinelProtectInterceptor; + +import org.springframework.aot.hint.ExecutableMode; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.web.client.RestTemplate; + +/** + * @author ruansheneg + */ +public class SentinelProtectInterceptorHints implements RuntimeHintsRegistrar { + @Override + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { + Constructor constructor; + try { + constructor = SentinelProtectInterceptor.class.getConstructor(SentinelRestTemplate.class, RestTemplate.class); + } + catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + hints.reflection().registerConstructor(constructor, ExecutableMode.INVOKE); + } +} diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/resources/META-INF/spring/aot.factories b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/resources/META-INF/spring/aot.factories new file mode 100644 index 000000000..456c7a9f4 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/main/resources/META-INF/spring/aot.factories @@ -0,0 +1,2 @@ +org.springframework.aot.hint.RuntimeHintsRegistrar=\ +com.alibaba.cloud.sentinel.aot.hint.SentinelProtectInterceptorHints diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/aot/hint/SentinelProtectInterceptorHintsTest.java b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/aot/hint/SentinelProtectInterceptorHintsTest.java new file mode 100644 index 000000000..270b7eae2 --- /dev/null +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/aot/hint/SentinelProtectInterceptorHintsTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2013-2023 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 com.alibaba.cloud.sentinel.aot.hint; + +import java.lang.reflect.Constructor; + +import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate; +import com.alibaba.cloud.sentinel.custom.SentinelProtectInterceptor; +import org.junit.jupiter.api.Test; + +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author ruansheneg + */ +public class SentinelProtectInterceptorHintsTest { + + @Test + public void shouldRegisterHints() { + Constructor constructor; + try { + constructor = SentinelProtectInterceptor.class.getConstructor(SentinelRestTemplate.class, RestTemplate.class); + } + catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + RuntimeHints hints = new RuntimeHints(); + new SentinelProtectInterceptorHints().registerHints(hints, getClass().getClassLoader()); + assertThat(RuntimeHintsPredicates.reflection().onConstructor(constructor)).accepts(hints); + } + +} +