Skip to content

Commit

Permalink
Fix findMethod returns wrong method when method has been overloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
milkyway0308 committed Aug 17, 2023
1 parent 8f73684 commit 41fff7a
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/main/java/org/bytedeco/gradle/javacpp/BuildPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Properties;
import java.util.Set;
import org.bytedeco.javacpp.Loader;
Expand Down Expand Up @@ -93,9 +94,13 @@ boolean isLibraryPath(String path) {
}

private <T> void setProperty(String originalMethod, String propertyField, Object target, T value) {
Method method = findMethod(target.getClass(), originalMethod);
Method method = findMethod(target.getClass(), originalMethod, value.getClass());
if (method != null) {
invoke(method, target);
try {
invoke(method, target, value);
} catch (Exception e) {
throw new RuntimeException("Cannot set property " + method.getName() + " in " + target.getClass(), e);
}
} else {
Method propertyGetter = findMethod(target.getClass(), propertyField);
if (propertyGetter == null) {
Expand Down Expand Up @@ -127,11 +132,19 @@ private Method findMethod(Class<?> cls, String methodName) {
return null;
}

private Method findMethod(Class<?> cls, String methodName, Class<?>... parameterTypes) {
try {
return cls.getMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
return null;
}
}

private <T> T invoke(Method method, Object target, Object... parameter) {
try {
return (T) method.invoke(target, parameter);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException("Method " + method.getName() + ", types " ,e);
}
}

Expand Down Expand Up @@ -178,8 +191,8 @@ private <T> T invoke(Method method, Object target, Object... parameter) {
task.setSource(main.getJava());
task.setClasspath(main.getCompileClasspath());
setProperty(
"getDestinationDirectory",
"setDestinationDir",
"getDestinationDirectory",
task,
getProperty(
"getOutputDir",
Expand Down

0 comments on commit 41fff7a

Please sign in to comment.