lindong28 commented on code in PR #235:
URL: https://github.com/apache/flink-ml/pull/235#discussion_r1178571198
##########
flink-ml-iteration/src/main/java/org/apache/flink/iteration/utils/ReflectionUtils.java:
##########
@@ -80,4 +83,55 @@ public static <T> T callMethod(
"Failed to get method" + methodName + " from " +
targetObject, e);
}
}
+
+ /**
+ * The utility method to call method with the specific name and parameters.
+ *
+ * <p>Note that this method is added only for bypassing the existing bug
in Py4j. It doesn't
+ * validate the classes of parameters so it can only deal with the classes
that have only one
+ * method with the specific name.
+ *
+ * <p>TODO Remove this method after the Py4j bug is fixed.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T callMethodByName(
+ Object targetObject, Class<?> declaredClass, String methodName,
Object[] parameters) {
+ List<Method> methods = new ArrayList<>();
+ for (Method m : declaredClass.getMethods()) {
Review Comment:
Is it possible to locate the method by calling `Class#getMethod(String name,
Class<?>... parameterTypes)`?
##########
flink-ml-iteration/src/main/java/org/apache/flink/iteration/utils/ReflectionUtils.java:
##########
@@ -80,4 +83,55 @@ public static <T> T callMethod(
"Failed to get method" + methodName + " from " +
targetObject, e);
}
}
+
+ /**
+ * The utility method to call method with the specific name and parameters.
+ *
+ * <p>Note that this method is added only for bypassing the existing bug
in Py4j. It doesn't
+ * validate the classes of parameters so it can only deal with the classes
that have only one
+ * method with the specific name.
+ *
+ * <p>TODO Remove this method after the Py4j bug is fixed.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T callMethodByName(
+ Object targetObject, Class<?> declaredClass, String methodName,
Object[] parameters) {
+ List<Method> methods = new ArrayList<>();
+ for (Method m : declaredClass.getMethods()) {
+ if (methodName.equals(m.getName())) {
+ m.setAccessible(true);
+ methods.add(m);
+ }
+ }
+ Preconditions.checkState(
+ methods.size() == 1,
+ "Only one method with name %s is permitted to be declared in
%s",
+ methodName,
+ declaredClass);
+ try {
+ return (T) methods.get(0).invoke(targetObject, parameters);
+ } catch (Exception e) {
+ throw new RuntimeException(
+ "Failed to invoke method" + methodName + " from " +
targetObject, e);
+ }
+ }
+
+ /**
+ * The utility method to call method with the specific name.
+ *
+ * <p>TODO Remove this method after the Py4j bug is fixed.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T callMethodByName(
+ Object targetObject, Class<?> declaredClass, String methodName) {
+
+ try {
+ Method method = declaredClass.getMethod(methodName);
+ method.setAccessible(true);
Review Comment:
It is not necessary to use `method.setAccessible(true)` here since
`Class#getMethod(...)` only returns public member method.
##########
flink-ml-iteration/src/main/java/org/apache/flink/iteration/utils/ReflectionUtils.java:
##########
@@ -80,4 +83,55 @@ public static <T> T callMethod(
"Failed to get method" + methodName + " from " +
targetObject, e);
}
}
+
+ /**
+ * The utility method to call method with the specific name and parameters.
+ *
+ * <p>Note that this method is added only for bypassing the existing bug
in Py4j. It doesn't
+ * validate the classes of parameters so it can only deal with the classes
that have only one
+ * method with the specific name.
+ *
+ * <p>TODO Remove this method after the Py4j bug is fixed.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T callMethodByName(
+ Object targetObject, Class<?> declaredClass, String methodName,
Object[] parameters) {
+ List<Method> methods = new ArrayList<>();
+ for (Method m : declaredClass.getMethods()) {
+ if (methodName.equals(m.getName())) {
+ m.setAccessible(true);
+ methods.add(m);
+ }
+ }
+ Preconditions.checkState(
+ methods.size() == 1,
+ "Only one method with name %s is permitted to be declared in
%s",
+ methodName,
+ declaredClass);
+ try {
+ return (T) methods.get(0).invoke(targetObject, parameters);
+ } catch (Exception e) {
+ throw new RuntimeException(
+ "Failed to invoke method" + methodName + " from " +
targetObject, e);
+ }
+ }
+
+ /**
+ * The utility method to call method with the specific name.
+ *
+ * <p>TODO Remove this method after the Py4j bug is fixed.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T callMethodByName(
Review Comment:
Would it be simpler to update the existing `callMethod(...)` to also use the
inherited methods?
##########
flink-ml-iteration/src/main/java/org/apache/flink/iteration/utils/ReflectionUtils.java:
##########
@@ -80,4 +83,55 @@ public static <T> T callMethod(
"Failed to get method" + methodName + " from " +
targetObject, e);
}
}
+
+ /**
+ * The utility method to call method with the specific name and parameters.
+ *
+ * <p>Note that this method is added only for bypassing the existing bug
in Py4j. It doesn't
+ * validate the classes of parameters so it can only deal with the classes
that have only one
+ * method with the specific name.
+ *
+ * <p>TODO Remove this method after the Py4j bug is fixed.
Review Comment:
nits: `TODO` -> `TODO: `
--
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]