This is an automated email from the ASF dual-hosted git repository.
xtsong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
The following commit(s) were added to refs/heads/master by this push:
new 9cc649898d0 [FLINK-35148][core] Improve InstantiationUtil for checking
nullary public constructor
9cc649898d0 is described below
commit 9cc649898d018ca87fa83ae9831ca9bee856f70b
Author: Mingliang Liu <[email protected]>
AuthorDate: Wed Apr 17 15:28:36 2024 -0700
[FLINK-35148][core] Improve InstantiationUtil for checking nullary public
constructor
---
.../main/java/org/apache/flink/util/InstantiationUtil.java | 12 +++---------
.../java/org/apache/flink/util/InstantiationUtilTest.java | 8 ++++++++
2 files changed, 11 insertions(+), 9 deletions(-)
diff --git
a/flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java
b/flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java
index 4f0a8b66f83..8207e6379a2 100644
--- a/flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java
+++ b/flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java
@@ -41,9 +41,9 @@ import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.OutputStream;
import java.io.Serializable;
-import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -391,14 +391,8 @@ public final class InstantiationUtil {
* @return True, if the class has a public nullary constructor, false if
not.
*/
public static boolean hasPublicNullaryConstructor(Class<?> clazz) {
- Constructor<?>[] constructors = clazz.getConstructors();
- for (Constructor<?> constructor : constructors) {
- if (constructor.getParameterCount() == 0
- && Modifier.isPublic(constructor.getModifiers())) {
- return true;
- }
- }
- return false;
+ return Arrays.stream(clazz.getConstructors())
+ .anyMatch(constructor -> constructor.getParameterCount() == 0);
}
/**
diff --git
a/flink-core/src/test/java/org/apache/flink/util/InstantiationUtilTest.java
b/flink-core/src/test/java/org/apache/flink/util/InstantiationUtilTest.java
index b9543181d6c..1d4150d9269 100644
--- a/flink-core/src/test/java/org/apache/flink/util/InstantiationUtilTest.java
+++ b/flink-core/src/test/java/org/apache/flink/util/InstantiationUtilTest.java
@@ -115,6 +115,14 @@ public class InstantiationUtilTest extends TestLogger {
assertTrue(InstantiationUtil.hasPublicNullaryConstructor(StringValue.class));
}
+ /**
+ * Test that {@link InstantiationUtil} class per se does not have a
nullary public constructor.
+ */
+ @Test
+ public void testHasNullaryConstructorFalse() {
+
assertFalse(InstantiationUtil.hasPublicNullaryConstructor(InstantiationUtil.class));
+ }
+
@Test
public void testClassIsProper() {
assertTrue(InstantiationUtil.isProperClass(StringValue.class));