Skip to content

Commit

Permalink
[FLINK-35148][core] Improve InstantiationUtil for checking nullary pu…
Browse files Browse the repository at this point in the history
…blic constructor
  • Loading branch information
liuml07 authored and xintongsong committed Apr 24, 2024
1 parent 712d980 commit 9cc6498
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
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;
Expand Down Expand Up @@ -391,14 +391,8 @@ public static <T> T instantiate(Class<T> clazz) {
* @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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ public void testHasNullaryConstructor() {
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));
Expand Down

0 comments on commit 9cc6498

Please sign in to comment.