From 1b12f1bd917dd0676ed33d7752ceeca2c1ec9283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=A6=B9=E5=85=89?= Date: Fri, 11 Jan 2019 16:38:58 +0800 Subject: [PATCH 1/5] Optimize code: Fix Constructor to determine illegal logic problems --- .../src/main/java/org/apache/dubbo/common/utils/PojoUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java index e9a9c63201a..6e48cf7745a 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java @@ -515,7 +515,7 @@ private static Object newInstance(Class cls) { } catch (Throwable t) { try { Constructor[] constructors = cls.getDeclaredConstructors(); - if (constructors != null && constructors.length == 0) { + if (constructors == null && constructors.length == 0) { throw new RuntimeException("Illegal constructor: " + cls.getName()); } Constructor constructor = constructors[0]; From 5f4a78d32be1ffe592ac7d769b39eb4e34d1d8ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=A6=B9=E5=85=89?= Date: Fri, 11 Jan 2019 19:00:30 +0800 Subject: [PATCH 2/5] Optimize code: fix error in judgment logic --- .../src/main/java/org/apache/dubbo/common/utils/PojoUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java index 6e48cf7745a..79dac06f448 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java @@ -515,7 +515,7 @@ private static Object newInstance(Class cls) { } catch (Throwable t) { try { Constructor[] constructors = cls.getDeclaredConstructors(); - if (constructors == null && constructors.length == 0) { + if (constructors == null || constructors.length == 0) { throw new RuntimeException("Illegal constructor: " + cls.getName()); } Constructor constructor = constructors[0]; From b8b3aaf2feb4f6083aab850cb8803ffe7a09a229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=A6=B9=E5=85=89?= Date: Mon, 14 Jan 2019 23:46:41 +0800 Subject: [PATCH 3/5] java.lang.Class#getDeclaredConstructors all return not null; --- .../src/main/java/org/apache/dubbo/common/utils/PojoUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java index 79dac06f448..4f5eb39ae7d 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java @@ -515,7 +515,7 @@ private static Object newInstance(Class cls) { } catch (Throwable t) { try { Constructor[] constructors = cls.getDeclaredConstructors(); - if (constructors == null || constructors.length == 0) { + if (constructors.length == 0) { throw new RuntimeException("Illegal constructor: " + cls.getName()); } Constructor constructor = constructors[0]; From 43fde45f966aaf896ec9b5ccd63747e8608c8b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=A6=B9=E5=85=89?= Date: Thu, 17 Jan 2019 20:32:18 +0800 Subject: [PATCH 4/5] Add comments to let everyone understand the changes to the code. --- .../java/org/apache/dubbo/common/utils/PojoUtils.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java index 4f5eb39ae7d..6f3dd2c1133 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java @@ -515,6 +515,14 @@ private static Object newInstance(Class cls) { } catch (Throwable t) { try { Constructor[] constructors = cls.getDeclaredConstructors(); + /** + * From Javadoc java.lang.Class#getDeclaredConstructors + * This method returns an array of Constructor objects reflecting all the constructors + * declared by the class represented by this Class object. + * This method returns an array of length 0, + * if this Class object represents an interface, a primitive type, an array class, or void. + * So I removed the logical judgment of 'constructors == null'. + */ if (constructors.length == 0) { throw new RuntimeException("Illegal constructor: " + cls.getName()); } From d279cfc7e7af65534d6fc097f1ee8d1bb3d66877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E7=A6=B9=E5=85=89?= Date: Fri, 18 Jan 2019 12:33:02 +0800 Subject: [PATCH 5/5] it's less relevant to why we use constructors.length == 0. --- .../src/main/java/org/apache/dubbo/common/utils/PojoUtils.java | 1 - 1 file changed, 1 deletion(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java index 6f3dd2c1133..77fa8e20313 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java @@ -521,7 +521,6 @@ private static Object newInstance(Class cls) { * declared by the class represented by this Class object. * This method returns an array of length 0, * if this Class object represents an interface, a primitive type, an array class, or void. - * So I removed the logical judgment of 'constructors == null'. */ if (constructors.length == 0) { throw new RuntimeException("Illegal constructor: " + cls.getName());