From ac2bb45a2d5b5e15f3840ef5099efd6ece8f2e10 Mon Sep 17 00:00:00 2001 From: TLPC <470193496@qq.com> Date: Thu, 12 Oct 2023 10:08:41 +0800 Subject: [PATCH] =?UTF-8?q?AgentCore=E7=9A=84=E5=9F=BA=E7=A1=80=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=E6=B5=8B=E8=AF=95=EF=BC=9A=E6=8F=92=E4=BB=B6=E9=83=A8?= =?UTF-8?q?=E5=88=86=E3=80=81=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=E9=83=A8?= =?UTF-8?q?=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agentcore/test/request/RequestTest.java | 24 +++++++ .../application/common/TestAnnotationA.java | 33 +++++++++ .../application/common/TestAnnotationB.java | 33 +++++++++ .../application/common/TestSuperTypeA.java | 26 +++++++ .../application/common/TestSuperTypeB.java | 26 +++++++ .../test/application/router/RouterPath.java | 20 ++++++ .../classmatch/TestAnnotationDeclarer.java | 42 ++++++++++++ .../classmatch/TestAnnotationsDeclarer.java | 43 ++++++++++++ .../classmatch/TestInfixDeclarer.java | 42 ++++++++++++ .../classmatch/TestPrefixDeclarer.java | 42 ++++++++++++ .../classmatch/TestSuffixDeclarer.java | 42 ++++++++++++ .../classmatch/TestSuperTypeDeclarer.java | 42 ++++++++++++ .../classmatch/TestSuperTypesDeclarer.java | 43 ++++++++++++ .../TestExecuteContextDeclarer.java | 47 +++++++++++++ .../TestMethodMatcherDeclarer.java | 68 +++++++++++++++++++ .../retransform/TestReTransformDeclarer.java | 55 +++++++++++++++ .../common/SetEnhanceFlagInterceptor.java | 39 +++++++++++ .../enhancement/SetArgumentsInterceptor.java | 39 +++++++++++ .../enhancement/SetFieldInterceptor.java | 40 +++++++++++ .../enhancement/SkipInterceptor.java | 39 +++++++++++ .../retransform/ActiveCountInterceptor.java | 40 +++++++++++ .../GetAllStackTracesInterceptor.java | 44 ++++++++++++ .../NoParamsConstructorInterceptor.java | 44 ++++++++++++ .../retransform/SetNameInterceptor.java | 44 ++++++++++++ ....core.plugin.agent.declarer.PluginDeclarer | 12 +++- 25 files changed, 968 insertions(+), 1 deletion(-) create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestAnnotationA.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestAnnotationB.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestSuperTypeA.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestSuperTypeB.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestAnnotationDeclarer.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestAnnotationsDeclarer.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestInfixDeclarer.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestPrefixDeclarer.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestSuffixDeclarer.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestSuperTypeDeclarer.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestSuperTypesDeclarer.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/enhancement/TestExecuteContextDeclarer.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/methodmatch/TestMethodMatcherDeclarer.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/retransform/TestReTransformDeclarer.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/common/SetEnhanceFlagInterceptor.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/enhancement/SetArgumentsInterceptor.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/enhancement/SetFieldInterceptor.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/enhancement/SkipInterceptor.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/ActiveCountInterceptor.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/GetAllStackTracesInterceptor.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/NoParamsConstructorInterceptor.java create mode 100644 sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/SetNameInterceptor.java diff --git a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/RequestTest.java b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/RequestTest.java index 7be01ea3e0..9fa467eaa5 100644 --- a/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/RequestTest.java +++ b/sermant-integration-tests/agentcore-test/agentcore-integration-test/src/test/java/com/huaweicloud/agentcore/test/request/RequestTest.java @@ -116,4 +116,28 @@ public void testBackend() throws IOException { public void testCoreAndPluginConfigLoad() throws IOException { RequestUtils.testRequest("http://127.0.0.1:8915/testCoreAndPluginConfigLoad"); } + + @Test + @EnabledIfSystemProperty(named = "agentcore.test.type", matches = "COMMON_ENHANCE") + public void testClassMatch() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testClassMatch"); + } + + @Test + @EnabledIfSystemProperty(named = "agentcore.test.type", matches = "COMMON_ENHANCE") + public void testMethodMatch() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testMethodMatch"); + } + + @Test + @EnabledIfSystemProperty(named = "agentcore.test.type", matches = "COMMON_ENHANCE") + public void testEnhancement() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testEnhancement"); + } + + @Test + @EnabledIfSystemProperty(named = "agentcore.test.type", matches = "COMMON_ENHANCE") + public void testReTransform() throws IOException { + RequestUtils.testRequest("http://127.0.0.1:8915/testReTransform"); + } } diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestAnnotationA.java b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestAnnotationA.java new file mode 100644 index 0000000000..3a75f753e1 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestAnnotationA.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.test.application.common; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 示例注解,用于作为注解拦截的示例 + * + * @author luanwenfei + * @since 2023-09-08 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +public @interface TestAnnotationA { +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestAnnotationB.java b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestAnnotationB.java new file mode 100644 index 0000000000..46349228bf --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestAnnotationB.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.test.application.common; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 实例注解,用于作为注解拦截的示例 + * + * @author luanwenfei + * @since 2023-09-08 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +public @interface TestAnnotationB { +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestSuperTypeA.java b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestSuperTypeA.java new file mode 100644 index 0000000000..ea154619fa --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestSuperTypeA.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.test.application.common; + +/** + * TestSuperTypeA + * + * @author luanwenfei + * @since 2023-09-08 + */ +public interface TestSuperTypeA { +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestSuperTypeB.java b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestSuperTypeB.java new file mode 100644 index 0000000000..be4b57d9ba --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/common/TestSuperTypeB.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.test.application.common; + +/** + * TestSuperTypeB + * + * @author luanwenfei + * @since 2023-09-08 + */ +public interface TestSuperTypeB { +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/router/RouterPath.java b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/router/RouterPath.java index 673b2bde98..0d090f6e08 100644 --- a/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/router/RouterPath.java +++ b/sermant-integration-tests/agentcore-test/agentcore-test-application/src/main/java/com/huaweicloud/agentcore/test/application/router/RouterPath.java @@ -68,6 +68,26 @@ public class RouterPath { */ public static final String REQUEST_PATH_CORE_AND_PLUGIN_CONFIG_LOAD = "/testCoreAndPluginConfigLoad"; + /** + * 测试类匹配 + */ + public static final String REQUEST_PATH_CLASS_MATCH = "/testClassMatch"; + + /** + * 测试方法匹配 + */ + public static final String REQUEST_PATH_METHOD_MATCH = "/testMethodMatch"; + + /** + * 测试增强功能 + */ + public static final String REQUEST_PATH_ENHANCEMENT = "/testEnhancement"; + + /** + * 测试对类的重转换能力 + */ + public static final String REQUEST_PATH_RE_TRANSFORM = "/testReTransform"; + private RouterPath() { } } diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestAnnotationDeclarer.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestAnnotationDeclarer.java new file mode 100644 index 0000000000..479ff987ff --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestAnnotationDeclarer.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.declarer.classmatch; + +import com.huaweicloud.agentcore.tests.plugin.interceptor.common.SetEnhanceFlagInterceptor; +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; + +/** + * 测试通过单个注解匹配类 + * + * @author luanwenfei + * @since 2023-09-07 + */ +public class TestAnnotationDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.isAnnotatedWith("com.huaweicloud.agentcore.test.application.common.TestAnnotationA"); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + InterceptDeclarer.build(MethodMatcher.isStaticMethod(), new SetEnhanceFlagInterceptor())}; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestAnnotationsDeclarer.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestAnnotationsDeclarer.java new file mode 100644 index 0000000000..c39e40d16e --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestAnnotationsDeclarer.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.declarer.classmatch; + +import com.huaweicloud.agentcore.tests.plugin.interceptor.common.SetEnhanceFlagInterceptor; +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; + +/** + * 测试注解匹配类 + * + * @author luanwenfei + * @since 2023-09-07 + */ +public class TestAnnotationsDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.isAnnotatedWith("com.huaweicloud.agentcore.test.application.common.TestAnnotationA", + "com.huaweicloud.agentcore.test.application.common.TestAnnotationB"); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + InterceptDeclarer.build(MethodMatcher.isStaticMethod(), new SetEnhanceFlagInterceptor())}; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestInfixDeclarer.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestInfixDeclarer.java new file mode 100644 index 0000000000..f41e8faaab --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestInfixDeclarer.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.declarer.classmatch; + +import com.huaweicloud.agentcore.tests.plugin.interceptor.common.SetEnhanceFlagInterceptor; +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; + +/** + * 测试通过中缀匹配类 + * + * @author luanwenfei + * @since 2023-09-07 + */ +public class TestInfixDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.nameInfixedWith("Infix"); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + InterceptDeclarer.build(MethodMatcher.isStaticMethod(), new SetEnhanceFlagInterceptor())}; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestPrefixDeclarer.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestPrefixDeclarer.java new file mode 100644 index 0000000000..f3a4254253 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestPrefixDeclarer.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.declarer.classmatch; + +import com.huaweicloud.agentcore.tests.plugin.interceptor.common.SetEnhanceFlagInterceptor; +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; + +/** + * 测试通过前缀匹配类 + * + * @author luanwenfei + * @since 2023-09-07 + */ +public class TestPrefixDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.namePrefixedWith("com.huaweicloud.agentcore.test.application.tests.classmatch.Prefix"); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + InterceptDeclarer.build(MethodMatcher.isStaticMethod(), new SetEnhanceFlagInterceptor())}; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestSuffixDeclarer.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestSuffixDeclarer.java new file mode 100644 index 0000000000..e1c631a55b --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestSuffixDeclarer.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.declarer.classmatch; + +import com.huaweicloud.agentcore.tests.plugin.interceptor.common.SetEnhanceFlagInterceptor; +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; + +/** + * 测试通过后缀匹配类 + * + * @author luanwenfei + * @since 2023-09-07 + */ +public class TestSuffixDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.nameSuffixedWith("Suffix"); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + InterceptDeclarer.build(MethodMatcher.isStaticMethod(), new SetEnhanceFlagInterceptor())}; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestSuperTypeDeclarer.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestSuperTypeDeclarer.java new file mode 100644 index 0000000000..af64ee087f --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestSuperTypeDeclarer.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.declarer.classmatch; + +import com.huaweicloud.agentcore.tests.plugin.interceptor.common.SetEnhanceFlagInterceptor; +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; + +/** + * 测试通过单个父类匹配类 + * + * @author luanwenfei + * @since 2023-09-07 + */ +public class TestSuperTypeDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.isExtendedFrom("com.huaweicloud.agentcore.test.application.common.TestSuperTypeA"); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + InterceptDeclarer.build(MethodMatcher.isStaticMethod(), new SetEnhanceFlagInterceptor())}; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestSuperTypesDeclarer.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestSuperTypesDeclarer.java new file mode 100644 index 0000000000..b75d154e4e --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/classmatch/TestSuperTypesDeclarer.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.declarer.classmatch; + +import com.huaweicloud.agentcore.tests.plugin.interceptor.common.SetEnhanceFlagInterceptor; +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; + +/** + * 测试超类匹配类 + * + * @author luanwenfei + * @since 2023-09-07 + */ +public class TestSuperTypesDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.isExtendedFrom("com.huaweicloud.agentcore.test.application.common.TestSuperTypeA", + "com.huaweicloud.agentcore.test.application.common.TestSuperTypeB"); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + InterceptDeclarer.build(MethodMatcher.isStaticMethod(), new SetEnhanceFlagInterceptor())}; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/enhancement/TestExecuteContextDeclarer.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/enhancement/TestExecuteContextDeclarer.java new file mode 100644 index 0000000000..a8dbd85225 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/enhancement/TestExecuteContextDeclarer.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.declarer.enhancement; + +import com.huaweicloud.agentcore.tests.plugin.interceptor.enhancement.SetArgumentsInterceptor; +import com.huaweicloud.agentcore.tests.plugin.interceptor.enhancement.SetFieldInterceptor; +import com.huaweicloud.agentcore.tests.plugin.interceptor.enhancement.SkipInterceptor; +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; + +/** + * 测试ExecuteContext增强能力 + * + * @author luanwenfei + * @since 2023-09-07 + */ +public class TestExecuteContextDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.nameEquals("com.huaweicloud.agentcore.test.application.tests.enhancement.EnhancementTest"); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + InterceptDeclarer.build(MethodMatcher.nameEquals("testSkipFunction"), new SkipInterceptor()), + InterceptDeclarer.build(MethodMatcher.nameEquals("testSetFiledFunction"), new SetFieldInterceptor()), + InterceptDeclarer.build(MethodMatcher.nameEquals("testSetArguments"), new SetArgumentsInterceptor()) + }; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/methodmatch/TestMethodMatcherDeclarer.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/methodmatch/TestMethodMatcherDeclarer.java new file mode 100644 index 0000000000..779e7e6237 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/methodmatch/TestMethodMatcherDeclarer.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.declarer.methodmatch; + +import com.huaweicloud.agentcore.tests.plugin.interceptor.common.SetEnhanceFlagInterceptor; +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; + +/** + * 测试方法匹配 + * + * @author luanwenfei + * @since 2023-09-07 + */ +public class TestMethodMatcherDeclarer extends AbstractPluginDeclarer { + /** + * 测试方法参数个数为3时 + */ + private static final int TEST_METHOD_PARAMS_COUNT = 3; + + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.nameEquals( + "com.huaweicloud.agentcore.test.application.tests.methodmatch.MethodMatchersTest"); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + InterceptDeclarer.build(MethodMatcher.isConstructor(), new SetEnhanceFlagInterceptor()), + InterceptDeclarer.build(MethodMatcher.isStaticMethod(), new SetEnhanceFlagInterceptor()), + InterceptDeclarer.build(MethodMatcher.nameEquals("exactNameMethod"), new SetEnhanceFlagInterceptor()), + InterceptDeclarer.build(MethodMatcher.nameEquals("exactNameMethod"), new SetEnhanceFlagInterceptor()), + InterceptDeclarer.build(MethodMatcher.namePrefixedWith("prefix"), new SetEnhanceFlagInterceptor()), + InterceptDeclarer.build(MethodMatcher.nameInfixedWith("Infix"), new SetEnhanceFlagInterceptor()), + InterceptDeclarer.build(MethodMatcher.nameSuffixedWith("Suffix"), new SetEnhanceFlagInterceptor()), + InterceptDeclarer.build(MethodMatcher.resultTypeEquals(boolean.class), new SetEnhanceFlagInterceptor()), + InterceptDeclarer.build(MethodMatcher.paramCountEquals(TEST_METHOD_PARAMS_COUNT), + new SetEnhanceFlagInterceptor()), + InterceptDeclarer.build(MethodMatcher.paramTypesEqual(boolean.class, boolean.class), + new SetEnhanceFlagInterceptor()), + InterceptDeclarer.build( + MethodMatcher.isAnnotatedWith( + "com.huaweicloud.agentcore.test.application.common.TestAnnotationA"), + new SetEnhanceFlagInterceptor()), + InterceptDeclarer.build( + MethodMatcher.isAnnotatedWith( + "com.huaweicloud.agentcore.test.application.common.TestAnnotationA", + "com.huaweicloud.agentcore.test.application.common.TestAnnotationB"), + new SetEnhanceFlagInterceptor())}; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/retransform/TestReTransformDeclarer.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/retransform/TestReTransformDeclarer.java new file mode 100644 index 0000000000..8f05673929 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/declarer/retransform/TestReTransformDeclarer.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.declarer.retransform; + +import com.huaweicloud.agentcore.tests.plugin.interceptor.retransform.ActiveCountInterceptor; +import com.huaweicloud.agentcore.tests.plugin.interceptor.retransform.GetAllStackTracesInterceptor; +import com.huaweicloud.agentcore.tests.plugin.interceptor.retransform.NoParamsConstructorInterceptor; +import com.huaweicloud.agentcore.tests.plugin.interceptor.retransform.SetNameInterceptor; +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; +import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; + +/** + * 测试对类的重转换能力 + * + * @author luanwenfei + * @since 2023-09-07 + */ +public class TestReTransformDeclarer extends AbstractPluginDeclarer { + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.nameEquals("java.lang.Thread"); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + // 测试静态方法 + InterceptDeclarer.build(MethodMatcher.nameEquals("getAllStackTraces"), + new GetAllStackTracesInterceptor()), + // 测试无参构造方法 + InterceptDeclarer.build(MethodMatcher.isConstructor().and(MethodMatcher.paramCountEquals(0)), + new NoParamsConstructorInterceptor()), + // 测试实例方法 + InterceptDeclarer.build(MethodMatcher.nameEquals("setName"), new SetNameInterceptor()), + // 测试静态方法skip + InterceptDeclarer.build(MethodMatcher.nameEquals("activeCount"), new ActiveCountInterceptor()), + }; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/common/SetEnhanceFlagInterceptor.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/common/SetEnhanceFlagInterceptor.java new file mode 100644 index 0000000000..de9b1108c1 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/common/SetEnhanceFlagInterceptor.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.interceptor.common; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; + +/** + * 告知应用被成功增强 + * + * @author luanwenfei + * @since 2023-09-07 + */ +public class SetEnhanceFlagInterceptor extends AbstractInterceptor { + @Override + public ExecuteContext before(ExecuteContext context) throws Exception { + context.getArguments()[0] = true; + return context; + } + + @Override + public ExecuteContext after(ExecuteContext context) throws Exception { + return context; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/enhancement/SetArgumentsInterceptor.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/enhancement/SetArgumentsInterceptor.java new file mode 100644 index 0000000000..51686df253 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/enhancement/SetArgumentsInterceptor.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.interceptor.enhancement; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; + +/** + * 测试设置入参功能 + * + * @author tangle + * @since 2023-09-07 + */ +public class SetArgumentsInterceptor extends AbstractInterceptor { + @Override + public ExecuteContext before(ExecuteContext context) { + context.getArguments()[0] = "argSetBySermant"; + return context; + } + + @Override + public ExecuteContext after(ExecuteContext context) { + return context; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/enhancement/SetFieldInterceptor.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/enhancement/SetFieldInterceptor.java new file mode 100644 index 0000000000..9758704e77 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/enhancement/SetFieldInterceptor.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.interceptor.enhancement; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; + +/** + * 测试设置属性值功能 + * + * @author tangle + * @since 2023-09-07 + */ +public class SetFieldInterceptor extends AbstractInterceptor { + @Override + public ExecuteContext before(ExecuteContext context) { + context.setStaticFieldValue("staticField", "staticFieldSetBySermant"); + context.setMemberFieldValue("memberField", "memberFieldSetBySermant"); + return context; + } + + @Override + public ExecuteContext after(ExecuteContext context) { + return context; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/enhancement/SkipInterceptor.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/enhancement/SkipInterceptor.java new file mode 100644 index 0000000000..40d9dd5516 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/enhancement/SkipInterceptor.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.interceptor.enhancement; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; + +/** + * 测试skip功能 + * + * @author tangle + * @since 2023-09-07 + */ +public class SkipInterceptor extends AbstractInterceptor { + @Override + public ExecuteContext before(ExecuteContext context) { + context.skip(true); + return context; + } + + @Override + public ExecuteContext after(ExecuteContext context) { + return context; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/ActiveCountInterceptor.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/ActiveCountInterceptor.java new file mode 100644 index 0000000000..91593fd2de --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/ActiveCountInterceptor.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.interceptor.retransform; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; + +/** + * 测试静态方法的skip + * + * @author tangle + * @since 2023-09-07 + */ +public class ActiveCountInterceptor extends AbstractInterceptor { + private static final int SKIP_COUNT = 999; + + @Override + public ExecuteContext before(ExecuteContext context) throws Exception { + return context.skip(SKIP_COUNT); + } + + @Override + public ExecuteContext after(ExecuteContext context) throws Exception { + return context; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/GetAllStackTracesInterceptor.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/GetAllStackTracesInterceptor.java new file mode 100644 index 0000000000..84f4f124c2 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/GetAllStackTracesInterceptor.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.interceptor.retransform; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; + +import java.lang.reflect.Method; + +/** + * 测试静态方法的拦截 + * + * @author tangle + * @since 2023-09-07 + */ +public class GetAllStackTracesInterceptor extends AbstractInterceptor { + @Override + public ExecuteContext before(ExecuteContext context) throws Exception { + Class targetClass = Class.forName("com.huaweicloud.agentcore.test.application.tests.bootstrap" + + ".BootstrapTest"); + Method targetMethod = targetClass.getMethod("setStaticFlag", boolean.class); + targetMethod.invoke(null, true); + return context; + } + + @Override + public ExecuteContext after(ExecuteContext context) throws Exception { + return context; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/NoParamsConstructorInterceptor.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/NoParamsConstructorInterceptor.java new file mode 100644 index 0000000000..0862de580a --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/NoParamsConstructorInterceptor.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.interceptor.retransform; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; + +import java.lang.reflect.Method; + +/** + * 测试无参构造函数的拦截 + * + * @author tangle + * @since 2023-09-07 + */ +public class NoParamsConstructorInterceptor extends AbstractInterceptor { + @Override + public ExecuteContext before(ExecuteContext context) throws Exception { + Class targetClass = Class.forName("com.huaweicloud.agentcore.test.application.tests.bootstrap" + + ".BootstrapTest"); + Method targetMethod = targetClass.getMethod("setConstructorFlag", boolean.class); + targetMethod.invoke(null, true); + return context; + } + + @Override + public ExecuteContext after(ExecuteContext context) throws Exception { + return context; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/SetNameInterceptor.java b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/SetNameInterceptor.java new file mode 100644 index 0000000000..e2aca198d8 --- /dev/null +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/java/com/huaweicloud/agentcore/tests/plugin/interceptor/retransform/SetNameInterceptor.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * 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 + * + * http://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.huaweicloud.agentcore.tests.plugin.interceptor.retransform; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; + +/** + * 测试实例方法的拦截 + * + * @author tangle + * @since 2023-09-07 + */ +public class SetNameInterceptor extends AbstractInterceptor { + /** + * 修改Thread.setName()的入参为“modifyName” + */ + private static final String NAME = "modifyName"; + + @Override + public ExecuteContext before(ExecuteContext context) throws Exception { + context.getArguments()[0] = NAME; + return context; + } + + @Override + public ExecuteContext after(ExecuteContext context) throws Exception { + return context; + } +} diff --git a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer index 8d7c63a894..7aabe60585 100644 --- a/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer +++ b/sermant-integration-tests/agentcore-test/agentcore-test-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer @@ -1,2 +1,12 @@ com.huaweicloud.agentcore.tests.plugin.declarer.dynamicconfig.TestDynamicConfigDeclarer -com.huaweicloud.agentcore.tests.plugin.declarer.configload.TestConfigLoadDeclarer \ No newline at end of file +com.huaweicloud.agentcore.tests.plugin.declarer.configload.TestConfigLoadDeclarer +com.huaweicloud.agentcore.tests.plugin.declarer.retransform.TestReTransformDeclarer +com.huaweicloud.agentcore.tests.plugin.declarer.classmatch.TestAnnotationDeclarer +com.huaweicloud.agentcore.tests.plugin.declarer.classmatch.TestAnnotationsDeclarer +com.huaweicloud.agentcore.tests.plugin.declarer.classmatch.TestSuperTypeDeclarer +com.huaweicloud.agentcore.tests.plugin.declarer.classmatch.TestSuperTypesDeclarer +com.huaweicloud.agentcore.tests.plugin.declarer.classmatch.TestInfixDeclarer +com.huaweicloud.agentcore.tests.plugin.declarer.classmatch.TestPrefixDeclarer +com.huaweicloud.agentcore.tests.plugin.declarer.classmatch.TestSuffixDeclarer +com.huaweicloud.agentcore.tests.plugin.declarer.methodmatch.TestMethodMatcherDeclarer +com.huaweicloud.agentcore.tests.plugin.declarer.enhancement.TestExecuteContextDeclarer \ No newline at end of file