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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]