You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by "jsbxyyx (via GitHub)" <gi...@apache.org> on 2025/12/16 03:18:09 UTC

[PR] optimize: global transaction filter private method [incubator-seata]

jsbxyyx opened a new pull request, #7867:
URL: https://github.com/apache/incubator-seata/pull/7867

   <!--
       Licensed to the Apache Software Foundation (ASF) under one or more
       contributor license agreements.  See the NOTICE file distributed with
       this work for additional information regarding copyright ownership.
       The ASF licenses this file to You 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.
   -->
   <!-- Please make sure you have read and understood the contributing guidelines -->
   
   - [x] I have read the [CONTRIBUTING.md](https://github.com/apache/incubator-seata/blob/2.x/CONTRIBUTING.md) guidelines.
   - [ ] I have registered the PR [changes](https://github.com/apache/incubator-seata/tree/2.x/changes).
   
   ### Ⅰ. Describe what this PR did
   
   
   ### Ⅱ. Does this pull request fix one issue?
   <!-- If that, add "fixes #xxx" below in the next line, for example, fixes #97. -->
   fixes: #7770 
   
   ### Ⅲ. Why don't you add test cases (unit test/integration test)? 
   
   
   ### Ⅳ. Describe how to verify it
   
   
   ### Ⅴ. Special notes for reviews
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org


Re: [PR] optimize: global transaction support non-private methods [incubator-seata]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #7867:
URL: https://github.com/apache/incubator-seata/pull/7867#discussion_r2625779170


##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParser.java:
##########
@@ -88,8 +89,13 @@ protected boolean existsAnnotation(Class<?>... classes) {
                 if (trxAnno != null) {
                     return true;
                 }
-                Method[] methods = clazz.getMethods();
+                Method[] methods = clazz.getDeclaredMethods();

Review Comment:
   Changing from `getMethods()` to `getDeclaredMethods()` may miss annotations on inherited methods from parent classes. The `getMethods()` method returns all public methods including those inherited from superclasses, while `getDeclaredMethods()` only returns methods declared directly in this class. If a parent class has a method annotated with `@GlobalTransactional` or `@GlobalLock`, it will not be detected when scanning the child class. Consider iterating through the class hierarchy or keeping `getMethods()` while adding the private method filter.



##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParser.java:
##########
@@ -88,8 +89,13 @@ protected boolean existsAnnotation(Class<?>... classes) {
                 if (trxAnno != null) {
                     return true;
                 }
-                Method[] methods = clazz.getMethods();
+                Method[] methods = clazz.getDeclaredMethods();
                 for (Method method : methods) {
+                    // exclude private modifier methods
+                    if (Modifier.isPrivate(method.getModifiers())) {
+                        continue;
+                    }

Review Comment:
   The new behavior of excluding private methods and including non-private methods (protected and package-private) lacks test coverage. Consider adding test cases with method-level `@GlobalTransactional` or `@GlobalLock` annotations on protected, package-private, and private methods to verify that: 1) private methods are correctly excluded, 2) protected and package-private methods are correctly included. Currently all tests only use class-level annotations.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org


Re: [PR] optimize: global transaction support non-private methods [incubator-seata]

Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #7867:
URL: https://github.com/apache/incubator-seata/pull/7867#issuecomment-3658614393

   ## [Codecov](https://app.codecov.io/gh/apache/incubator-seata/pull/7867?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :white_check_mark: All modified and coverable lines are covered by tests.
   :white_check_mark: Project coverage is 71.03%. Comparing base ([`adfbef5`](https://app.codecov.io/gh/apache/incubator-seata/commit/adfbef513caf246f0f300a50a771adafea503af1?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`19a6e91`](https://app.codecov.io/gh/apache/incubator-seata/commit/19a6e91ee5939462ad2e1dca4d2b8c304dafa1b5?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@             Coverage Diff              @@
   ##                2.x    #7867      +/-   ##
   ============================================
   + Coverage     70.99%   71.03%   +0.04%     
     Complexity      797      797              
   ============================================
     Files          1294     1294              
     Lines         49508    49510       +2     
     Branches       5869     5870       +1     
   ============================================
   + Hits          35148    35171      +23     
   + Misses        11445    11429      -16     
   + Partials       2915     2910       -5     
   ```
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/incubator-seata/pull/7867?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [...r/parser/GlobalTransactionalInterceptorParser.java](https://app.codecov.io/gh/apache/incubator-seata/pull/7867?src=pr&el=tree&filepath=integration-tx-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fseata%2Fintegration%2Ftx%2Fapi%2Finterceptor%2Fparser%2FGlobalTransactionalInterceptorParser.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-aW50ZWdyYXRpb24tdHgtYXBpL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9zZWF0YS9pbnRlZ3JhdGlvbi90eC9hcGkvaW50ZXJjZXB0b3IvcGFyc2VyL0dsb2JhbFRyYW5zYWN0aW9uYWxJbnRlcmNlcHRvclBhcnNlci5qYXZh) | `66.66% <100.00%> (+1.80%)` | :arrow_up: |
   
   ... and [7 files with indirect coverage changes](https://app.codecov.io/gh/apache/incubator-seata/pull/7867/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   
   [![Impacted file tree graph](https://app.codecov.io/gh/apache/incubator-seata/pull/7867/graphs/tree.svg?width=650&height=150&src=pr&token=tbmHt2ZfxO&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)](https://app.codecov.io/gh/apache/incubator-seata/pull/7867?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   </details>
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org


Re: [PR] optimize: global transaction support non-private methods [incubator-seata]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #7867:
URL: https://github.com/apache/incubator-seata/pull/7867#discussion_r2633559227


##########
integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/parser/NonPrivateMethodTestClass.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.seata.integration.tx.api.interceptor.parser;
+
+import org.apache.seata.spring.annotation.GlobalTransactional;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class NonPrivateMethodTestClass {
+    private static final Logger LOGGER = LoggerFactory.getLogger(NonPrivateMethodTestClass.class);
+
+    @GlobalTransactional(timeoutMills = 300000)
+    String dftMethod() {

Review Comment:
   The method name `dftMethod` uses an unclear abbreviation. Consider renaming to `defaultMethod` or `packagePrivateMethod` to better communicate that this method has package-private (default) access visibility. This would make the test class more self-documenting and easier to understand.
   ```suggestion
       String defaultMethod() {
   ```



##########
integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParserTest.java:
##########
@@ -38,4 +38,19 @@ void parserInterfaceToProxy() throws Exception {
         // then
         Assertions.assertNotNull(proxyInvocationHandler);
     }
+
+    @Test
+    void shouldExcludePrivateMethodsFromProxy() throws Exception {

Review Comment:
   The test name `shouldExcludePrivateMethodsFromProxy` doesn't fully capture what the test verifies. The test actually verifies that non-private methods (default and protected) ARE included while private methods are excluded. Consider a more descriptive name like `shouldIncludeNonPrivateMethodsAndExcludePrivateMethodsFromProxy` or `shouldProxyOnlyNonPrivateMethods` to better communicate the complete behavior being tested.
   ```suggestion
       void shouldProxyOnlyNonPrivateMethods() throws Exception {
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org


Re: [PR] optimize: global transaction support non-private methods [incubator-seata]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #7867:
URL: https://github.com/apache/incubator-seata/pull/7867#discussion_r2633483600


##########
integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParserTest.java:
##########
@@ -38,4 +38,19 @@ void parserInterfaceToProxy() throws Exception {
         // then
         Assertions.assertNotNull(proxyInvocationHandler);
     }
+
+    @Test
+    void parserInterfaceToProxyNonPrivateMethod() throws Exception {

Review Comment:
   The test method name "parserInterfaceToProxyNonPrivateMethod" could be more descriptive. Consider renaming it to "shouldProxyOnlyNonPrivateAnnotatedMethods" or "shouldExcludePrivateMethodsFromProxy" to better describe what behavior is being tested.
   ```suggestion
       void shouldProxyOnlyNonPrivateAnnotatedMethods() throws Exception {
   ```



##########
integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/parser/MethodImpl.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.seata.integration.tx.api.interceptor.parser;
+
+import org.apache.seata.spring.annotation.GlobalTransactional;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MethodImpl {
+    private static final Logger LOGGER = LoggerFactory.getLogger(MethodImpl.class);

Review Comment:
   The class name "MethodImpl" is ambiguous and doesn't clearly convey its purpose as a test fixture. Consider renaming it to something more descriptive like "NonPrivateMethodTestClass" or "MethodVisibilityTestClass" to better reflect that it's a test helper class used to verify method visibility handling.
   ```suggestion
   public class MethodVisibilityTestClass {
       private static final Logger LOGGER = LoggerFactory.getLogger(MethodVisibilityTestClass.class);
   ```



##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParser.java:
##########
@@ -89,17 +94,31 @@ protected boolean existsAnnotation(Class<?>... classes) {
                     return true;
                 }
                 Method[] methods = clazz.getMethods();
-                for (Method method : methods) {
-                    trxAnno = method.getAnnotation(GlobalTransactional.class);
-                    if (trxAnno != null) {
-                        methodsToProxy.add(method.getName());
-                        result = true;
-                    }
+                Method[] declaredMethods = clazz.getDeclaredMethods();
+                int arrayLength = methods.length + declaredMethods.length;
+                if (arrayLength > 0) {
+                    Method[] allMethods = new Method[arrayLength];
+                    System.arraycopy(methods, 0, allMethods, 0, methods.length);
+                    System.arraycopy(declaredMethods, 0, allMethods, methods.length, declaredMethods.length);
+                    Set<Method> processedMethods = new HashSet<>();
+                    for (Method method : allMethods) {
+                        if (!processedMethods.add(method)) {
+                            continue;
+                        }

Review Comment:
   The duplicate detection logic using a Set of Method objects may not work as intended. The Set.add() method relies on equals() and hashCode() implementations. For Method objects from getMethods() and getDeclaredMethods(), even if they represent the same method, they may be different object instances. This means the duplicate detection might fail to filter out methods that appear in both arrays.
   
   Additionally, the current approach of merging both arrays and then deduplicating is inefficient. Consider iterating through declaredMethods and only processing those that are not already in the methods array (i.e., non-public methods), or use a Set based on method signatures (name + parameter types) instead of Method objects.



##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParser.java:
##########
@@ -89,17 +94,31 @@ protected boolean existsAnnotation(Class<?>... classes) {
                     return true;
                 }
                 Method[] methods = clazz.getMethods();
-                for (Method method : methods) {
-                    trxAnno = method.getAnnotation(GlobalTransactional.class);
-                    if (trxAnno != null) {
-                        methodsToProxy.add(method.getName());
-                        result = true;
-                    }
+                Method[] declaredMethods = clazz.getDeclaredMethods();
+                int arrayLength = methods.length + declaredMethods.length;
+                if (arrayLength > 0) {
+                    Method[] allMethods = new Method[arrayLength];
+                    System.arraycopy(methods, 0, allMethods, 0, methods.length);
+                    System.arraycopy(declaredMethods, 0, allMethods, methods.length, declaredMethods.length);

Review Comment:
   The current implementation creates a new array and copies all methods from both getMethods() and getDeclaredMethods(), which can be memory-intensive and inefficient for classes with many methods. Since getMethods() already returns all public methods (including inherited ones), and the goal is to also process non-public methods from the current class, consider iterating through getMethods() first, then separately iterate through declaredMethods() filtering for non-public methods only. This would avoid the array allocation and copying overhead.



##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParser.java:
##########
@@ -26,13 +26,18 @@
 import org.apache.seata.spring.annotation.GlobalLock;
 import org.apache.seata.spring.annotation.GlobalTransactional;
 import org.apache.seata.tm.api.FailureHandlerHolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.util.HashSet;
 import java.util.Set;
 
 public class GlobalTransactionalInterceptorParser implements InterfaceParser {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalTransactionalInterceptorParser.class);

Review Comment:
   The LOGGER variable is added but never used in this class. Consider removing it if logging is not needed, or add appropriate logging statements if there's a need to log method processing for debugging purposes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org


Re: [PR] optimize: global transaction support non-private methods [incubator-seata]

Posted by "slievrly (via GitHub)" <gi...@apache.org>.
slievrly merged PR #7867:
URL: https://github.com/apache/incubator-seata/pull/7867


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org


Re: [PR] optimize: global transaction support non-private methods [incubator-seata]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #7867:
URL: https://github.com/apache/incubator-seata/pull/7867#discussion_r2633446019


##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParser.java:
##########
@@ -89,17 +94,40 @@ protected boolean existsAnnotation(Class<?>... classes) {
                     return true;
                 }
                 Method[] methods = clazz.getMethods();
-                for (Method method : methods) {
-                    trxAnno = method.getAnnotation(GlobalTransactional.class);
-                    if (trxAnno != null) {
-                        methodsToProxy.add(method.getName());
-                        result = true;
-                    }
+                Method[] declaredMethods = clazz.getDeclaredMethods();
+                int arrayLength = methods.length + declaredMethods.length;
+                if (arrayLength > 0) {
+                    Method[] allMethods = new Method[arrayLength];
+                    System.arraycopy(methods, 0, allMethods, 0, methods.length);
+                    System.arraycopy(declaredMethods, 0, allMethods, methods.length, declaredMethods.length);
+                    Set<Method> processedMethods = new HashSet<>();
+                    for (Method method : allMethods) {
+                        if (!processedMethods.add(method)) {
+                            continue;
+                        }
+                        trxAnno = method.getAnnotation(GlobalTransactional.class);
+                        if (trxAnno != null) {
+                            methodsToProxy.add(method.getName());
+                            if (!Modifier.isPrivate(method.getModifiers())) {
+                                result = true;
+                            } else {
+                                LOGGER.warn(
+                                        "GlobalTransactional annotation found on private method {}, which will be ignored.",
+                                        method.getName());
+                            }
+                        }
 
-                    GlobalLock lockAnno = method.getAnnotation(GlobalLock.class);
-                    if (lockAnno != null) {
-                        methodsToProxy.add(method.getName());
-                        result = true;
+                        GlobalLock lockAnno = method.getAnnotation(GlobalLock.class);
+                        if (lockAnno != null) {
+                            methodsToProxy.add(method.getName());
+                            if (!Modifier.isPrivate(method.getModifiers())) {
+                                result = true;
+                            } else {
+                                LOGGER.warn(
+                                        "GlobalLock annotation found on private method {}, which will be ignored.",
+                                        method.getName());
+                            }

Review Comment:
   Private methods with GlobalLock annotation are still being added to the methodsToProxy set even though they should be ignored. The check on line 123 only affects the result boolean but doesn't prevent adding the method name to methodsToProxy on line 122. This means private methods will be included in the proxy set, which contradicts the intent of filtering them out. Consider moving line 122 inside the non-private check block so that only non-private annotated methods are added to methodsToProxy.



##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParser.java:
##########
@@ -89,17 +94,40 @@ protected boolean existsAnnotation(Class<?>... classes) {
                     return true;
                 }
                 Method[] methods = clazz.getMethods();
-                for (Method method : methods) {
-                    trxAnno = method.getAnnotation(GlobalTransactional.class);
-                    if (trxAnno != null) {
-                        methodsToProxy.add(method.getName());
-                        result = true;
-                    }
+                Method[] declaredMethods = clazz.getDeclaredMethods();
+                int arrayLength = methods.length + declaredMethods.length;
+                if (arrayLength > 0) {
+                    Method[] allMethods = new Method[arrayLength];
+                    System.arraycopy(methods, 0, allMethods, 0, methods.length);
+                    System.arraycopy(declaredMethods, 0, allMethods, methods.length, declaredMethods.length);
+                    Set<Method> processedMethods = new HashSet<>();
+                    for (Method method : allMethods) {
+                        if (!processedMethods.add(method)) {
+                            continue;
+                        }
+                        trxAnno = method.getAnnotation(GlobalTransactional.class);
+                        if (trxAnno != null) {
+                            methodsToProxy.add(method.getName());
+                            if (!Modifier.isPrivate(method.getModifiers())) {
+                                result = true;
+                            } else {
+                                LOGGER.warn(
+                                        "GlobalTransactional annotation found on private method {}, which will be ignored.",
+                                        method.getName());
+                            }

Review Comment:
   Private methods are still being added to the methodsToProxy set even though they should be ignored. The check on line 112 only affects the result boolean but doesn't prevent adding the method name to methodsToProxy on line 111. This means private methods will be included in the proxy set, which contradicts the intent of filtering them out. Consider moving line 111 inside the non-private check block so that only non-private annotated methods are added to methodsToProxy.



##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParser.java:
##########
@@ -89,17 +94,40 @@ protected boolean existsAnnotation(Class<?>... classes) {
                     return true;
                 }
                 Method[] methods = clazz.getMethods();
-                for (Method method : methods) {
-                    trxAnno = method.getAnnotation(GlobalTransactional.class);
-                    if (trxAnno != null) {
-                        methodsToProxy.add(method.getName());
-                        result = true;
-                    }
+                Method[] declaredMethods = clazz.getDeclaredMethods();
+                int arrayLength = methods.length + declaredMethods.length;
+                if (arrayLength > 0) {
+                    Method[] allMethods = new Method[arrayLength];
+                    System.arraycopy(methods, 0, allMethods, 0, methods.length);
+                    System.arraycopy(declaredMethods, 0, allMethods, methods.length, declaredMethods.length);
+                    Set<Method> processedMethods = new HashSet<>();

Review Comment:
   Creating a combined array of methods and declaredMethods using System.arraycopy is inefficient when both arrays could be processed separately in consecutive loops. Consider processing methods array first, then declaredMethods array, using a single Set to track processed methods across both loops. This would avoid the unnecessary array allocation and copying.



##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParser.java:
##########
@@ -89,17 +94,40 @@ protected boolean existsAnnotation(Class<?>... classes) {
                     return true;
                 }
                 Method[] methods = clazz.getMethods();
-                for (Method method : methods) {
-                    trxAnno = method.getAnnotation(GlobalTransactional.class);
-                    if (trxAnno != null) {
-                        methodsToProxy.add(method.getName());
-                        result = true;
-                    }
+                Method[] declaredMethods = clazz.getDeclaredMethods();
+                int arrayLength = methods.length + declaredMethods.length;
+                if (arrayLength > 0) {
+                    Method[] allMethods = new Method[arrayLength];
+                    System.arraycopy(methods, 0, allMethods, 0, methods.length);
+                    System.arraycopy(declaredMethods, 0, allMethods, methods.length, declaredMethods.length);
+                    Set<Method> processedMethods = new HashSet<>();
+                    for (Method method : allMethods) {
+                        if (!processedMethods.add(method)) {
+                            continue;
+                        }
+                        trxAnno = method.getAnnotation(GlobalTransactional.class);
+                        if (trxAnno != null) {
+                            methodsToProxy.add(method.getName());
+                            if (!Modifier.isPrivate(method.getModifiers())) {
+                                result = true;
+                            } else {
+                                LOGGER.warn(
+                                        "GlobalTransactional annotation found on private method {}, which will be ignored.",
+                                        method.getName());
+                            }
+                        }
 
-                    GlobalLock lockAnno = method.getAnnotation(GlobalLock.class);
-                    if (lockAnno != null) {
-                        methodsToProxy.add(method.getName());
-                        result = true;
+                        GlobalLock lockAnno = method.getAnnotation(GlobalLock.class);
+                        if (lockAnno != null) {
+                            methodsToProxy.add(method.getName());
+                            if (!Modifier.isPrivate(method.getModifiers())) {
+                                result = true;
+                            } else {
+                                LOGGER.warn(
+                                        "GlobalLock annotation found on private method {}, which will be ignored.",
+                                        method.getName());
+                            }

Review Comment:
   The logic for checking GlobalTransactional and GlobalLock annotations is duplicated. Both blocks check for the annotation, add the method to methodsToProxy, verify it's not private, and log a warning if it is private. Consider extracting this repeated logic into a helper method that takes the annotation and annotation name as parameters to reduce code duplication and improve maintainability.



##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParser.java:
##########
@@ -26,13 +26,18 @@
 import org.apache.seata.spring.annotation.GlobalLock;
 import org.apache.seata.spring.annotation.GlobalTransactional;
 import org.apache.seata.tm.api.FailureHandlerHolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.util.HashSet;

Review Comment:
   The Arrays import is added but never used in the code. Consider removing this unused import to keep the code clean.



##########
integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParserTest.java:
##########
@@ -38,4 +38,19 @@ void parserInterfaceToProxy() throws Exception {
         // then
         Assertions.assertNotNull(proxyInvocationHandler);
     }
+
+    @Test
+    void parserInterfaceToProxyNonPrivateMethod() throws Exception {
+        MethodImpl method = new MethodImpl();
+
+        GlobalTransactionalInterceptorParser globalTransactionalInterceptorParser =
+                new GlobalTransactionalInterceptorParser();
+
+        ProxyInvocationHandler proxyInvocationHandler = globalTransactionalInterceptorParser.parserInterfaceToProxy(
+                method, method.getClass().getName());
+
+        Assertions.assertNotNull(proxyInvocationHandler);
+
+        Assertions.assertEquals(2, proxyInvocationHandler.getMethodsToProxy().size());

Review Comment:
   The test expects 2 methods in methodsToProxy, but due to a bug in the implementation where private methods are still added to the set (despite being ignored for proxying), this will actually be 3. The MethodImpl class has three annotated methods: dftMethod (package-private), protectedMethod (protected), and privateMethod (private). Once the bug is fixed to exclude private methods from methodsToProxy, this assertion will be correct, but currently it will fail.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org


Re: [PR] optimize: global transaction support non-private methods [incubator-seata]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #7867:
URL: https://github.com/apache/incubator-seata/pull/7867#discussion_r2633308201


##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParser.java:
##########
@@ -89,17 +90,29 @@ protected boolean existsAnnotation(Class<?>... classes) {
                     return true;
                 }
                 Method[] methods = clazz.getMethods();
-                for (Method method : methods) {
-                    trxAnno = method.getAnnotation(GlobalTransactional.class);
-                    if (trxAnno != null) {
-                        methodsToProxy.add(method.getName());
-                        result = true;
-                    }
+                Method[] declaredMethods = clazz.getDeclaredMethods();
+                int arrayLength = methods.length + declaredMethods.length;
+                if (arrayLength > 0) {
+                    Method[] allMethods = new Method[arrayLength];
+                    System.arraycopy(methods, 0, allMethods, 0, methods.length);
+                    System.arraycopy(declaredMethods, 0, allMethods, methods.length, declaredMethods.length);
+                    for (Method method : allMethods) {

Review Comment:
   The approach of combining getMethods() and getDeclaredMethods() results in duplicate entries for public methods. The getMethods() call already returns all public methods including those declared in the class, and getDeclaredMethods() returns all methods declared in the class regardless of access modifier. This means public methods will appear twice in the allMethods array.
   
   Since methodsToProxy is a Set, duplicates won't cause functional issues, but this creates unnecessary iterations and checks. Consider using only getDeclaredMethods() and filtering by modifier instead, or add a check to skip duplicates when processing the combined array.
   ```suggestion
                       System.arraycopy(declaredMethods, 0, allMethods, methods.length, declaredMethods.length);
                       Set<Method> processedMethods = new HashSet<>();
                       for (Method method : allMethods) {
                           // skip methods already processed (avoids duplicates from combining getMethods and getDeclaredMethods)
                           if (!processedMethods.add(method)) {
                               continue;
                           }
   ```



##########
integration-tx-api/src/test/java/org/apache/seata/integration/tx/api/interceptor/parser/MethodImpl.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.seata.integration.tx.api.interceptor.parser;
+
+import org.apache.seata.spring.annotation.GlobalTransactional;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MethodImpl {
+    private static final Logger LOGGER = LoggerFactory.getLogger(MethodImpl.class);
+
+    @GlobalTransactional(timeoutMills = 300000)
+    String dftMethod() {
+        LOGGER.info("default method");
+        return "default method";
+    }
+
+    @GlobalTransactional(timeoutMills = 300000)
+    protected String protectedMethod() {
+        LOGGER.info("protected method");
+        return "protected method";
+    }
+}

Review Comment:
   The test class should include a private method with the GlobalTransactional annotation to verify that private methods are correctly excluded from proxying. Currently, the test only validates that non-private methods (default and protected) are included, but doesn't test the exclusion logic for private methods.



##########
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/interceptor/parser/GlobalTransactionalInterceptorParser.java:
##########
@@ -89,17 +90,29 @@ protected boolean existsAnnotation(Class<?>... classes) {
                     return true;
                 }
                 Method[] methods = clazz.getMethods();
-                for (Method method : methods) {
-                    trxAnno = method.getAnnotation(GlobalTransactional.class);
-                    if (trxAnno != null) {
-                        methodsToProxy.add(method.getName());
-                        result = true;
-                    }
+                Method[] declaredMethods = clazz.getDeclaredMethods();
+                int arrayLength = methods.length + declaredMethods.length;
+                if (arrayLength > 0) {
+                    Method[] allMethods = new Method[arrayLength];
+                    System.arraycopy(methods, 0, allMethods, 0, methods.length);
+                    System.arraycopy(declaredMethods, 0, allMethods, methods.length, declaredMethods.length);

Review Comment:
   The manual array concatenation using System.arraycopy is verbose and error-prone. Consider using a more maintainable approach such as streaming both arrays or using a list-based approach to combine the methods. This would improve code readability and reduce the risk of errors in calculating array lengths.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org