This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new cc421ba [Optimization] Remove double loop in dubbo-common (#7493)
cc421ba is described below
commit cc421bac428ab656f2b48937db76c3e1fce04d90
Author: fzhyzamt <[email protected]>
AuthorDate: Thu Apr 1 08:08:30 2021 -0500
[Optimization] Remove double loop in dubbo-common (#7493)
* Remove double loop
* change override to overload
* set HashMap initial size
* rename test method to testOverloadMethod
---
.../org/apache/dubbo/common/bytecode/Wrapper.java | 14 +++---
.../apache/dubbo/common/bytecode/WrapperTest.java | 52 +++++++++++++++++++++-
2 files changed, 58 insertions(+), 8 deletions(-)
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Wrapper.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Wrapper.java
index 61924ce..e8fb687 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Wrapper.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/bytecode/Wrapper.java
@@ -154,6 +154,12 @@ public abstract class Wrapper {
// get all public method.
boolean hasMethod = hasMethods(methods);
if (hasMethod) {
+ Map<String, Integer> sameNameMethodCount = new HashMap<>((int)
(methods.length / 0.75f) + 1);
+ for (Method m : methods) {
+ sameNameMethodCount.compute(m.getName(),
+ (key, oldValue) -> oldValue == null ? 1 : oldValue +
1);
+ }
+
c3.append(" try{");
for (Method m : methods) {
//ignore Object's method.
@@ -166,13 +172,7 @@ public abstract class Wrapper {
int len = m.getParameterTypes().length;
c3.append(" && ").append(" $3.length == ").append(len);
- boolean overload = false;
- for (Method m2 : methods) {
- if (m != m2 && m.getName().equals(m2.getName())) {
- overload = true;
- break;
- }
- }
+ boolean overload = sameNameMethodCount.get(m.getName()) > 1;
if (overload) {
if (len > 0) {
for (int l = 0; l < len; l++) {
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/WrapperTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/WrapperTest.java
index d3c4d90..cc246f8 100644
---
a/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/WrapperTest.java
+++
b/dubbo-common/src/test/java/org/apache/dubbo/common/bytecode/WrapperTest.java
@@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
public class WrapperTest {
@@ -114,6 +115,26 @@ public class WrapperTest {
}
@Test
+ public void testOverloadMethod() throws Exception {
+ Wrapper w = Wrapper.getWrapper(I2.class);
+ assertEquals(2, w.getMethodNames().length);
+
+ Impl2 impl = new Impl2();
+
+ w.invokeMethod(impl, "setFloat", new Class[]{float.class}, new
Object[]{1F});
+ assertEquals(1F, impl.getFloat1());
+ assertNull(impl.getFloat2());
+
+ w.invokeMethod(impl, "setFloat", new Class[]{Float.class}, new
Object[]{2f});
+ assertEquals(1F, impl.getFloat1());
+ assertEquals(2F, impl.getFloat2());
+
+ w.invokeMethod(impl, "setFloat", new Class[]{Float.class}, new
Object[]{null});
+ assertEquals(1F, impl.getFloat1());
+ assertNull(impl.getFloat2());
+ }
+
+ @Test
public void test_getDeclaredMethodNames_ContainExtendsParentMethods()
throws Exception {
assertArrayEquals(new String[]{"hello",},
Wrapper.getWrapper(Parent1.class).getMethodNames());
@@ -141,6 +162,12 @@ public class WrapperTest {
void setFloat(float f);
}
+ public interface I2 {
+ void setFloat(float f);
+
+ void setFloat(Float f);
+ }
+
public interface EmptyService {
}
@@ -191,6 +218,29 @@ public class WrapperTest {
}
}
+ public static class Impl2 implements I2 {
+ private float float1;
+ private Float float2;
+
+ @Override
+ public void setFloat(float f) {
+ this.float1 = f;
+ }
+
+ @Override
+ public void setFloat(Float f) {
+ this.float2 = f;
+ }
+
+ public float getFloat1() {
+ return float1;
+ }
+
+ public Float getFloat2() {
+ return float2;
+ }
+ }
+
public static class EmptyServiceImpl implements EmptyService {
}
-}
\ No newline at end of file
+}