Skip to content

Commit

Permalink
Fixing bug in filtering methods based on the parameter. Primitive typ…
Browse files Browse the repository at this point in the history
…es can be converted to the wrapper class and used to invoke the method.
  • Loading branch information
toastertaster committed Feb 9, 2024
1 parent f64ed20 commit 07f919f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.astonbitecode.j4rs.api.value.JsonValueFactory;
import org.astonbitecode.j4rs.errors.InvocationException;
import org.astonbitecode.j4rs.rust.RustPointer;
import org.astonbitecode.j4rs.utils.Utils;

import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
Expand Down Expand Up @@ -274,10 +275,21 @@ Method findMethodInHierarchy(Class clazz, String methodName, Class[] argTypes) t
matchedParams.add(((Class<?>)t).isAssignableFrom(argTypes[i]));
} else if (typ instanceof GenericArrayType) {
// TODO: Improve by checking the actual types of the arrays?
matchedParams.add(argTypes[i].isArray());
boolean value = argTypes[i].isArray();
// Useful for debugging
//if (!value) {
// System.out.println("Method " + m.getName() + " not matched for array type at param index " + i + " which is " + argTypes[i].toString());
//}
matchedParams.add(value);
} else if (typ instanceof Class) {
// In case of TypeVariable, the arg matches via the equals method
matchedParams.add(((Class<?>) typ).isAssignableFrom(argTypes[i]));
boolean value = Utils.toWrapper((Class<?>) typ).isAssignableFrom(argTypes[i]);

// Useful for debugging
//if (!value) {
// System.out.println("Method " + m.getName() + " not matched for Class type at param index " + i + " which is invoked with argType=" + argTypes[i].toString() + " with method definition type " + typ.toString());
//}
matchedParams.add(value);
} else if (typ instanceof WildcardType) {
// Find the type and match. Eg. ? <?> ? extends String etc
WildcardType wildcardType = (WildcardType) typ;
Expand Down Expand Up @@ -311,12 +323,20 @@ Method findMethodInHierarchy(Class clazz, String methodName, Class[] argTypes) t
return matchedParams.stream().allMatch(Boolean::booleanValue);
}).collect(Collectors.toList());
if (!found.isEmpty()) {
// Useful for debugging
//if (found.size() > 1) {
// StringBuilder options = new StringBuilder();
// for (Method m: found) {
// options.append(m.toString()).append(", ");
// }
// System.out.println("Found " + found.size() + " matching methods and only using the first. Options are: " + options);
//}
return found.get(0);
} else {
Class<?> superclass = clazz.getSuperclass();
if (superclass == null) {
throw new NoSuchMethodException(
"Method " + methodName + " was not found in " + this.clazz.getName() + " or its ancestors.");
"j4rs could not resolve Method " + methodName + " was not found in " + this.clazz.getName() + " or its ancestors.");
}
return findMethodInHierarchy(superclass, methodName, argTypes);
}
Expand Down
27 changes: 27 additions & 0 deletions java/src/main/java/org/astonbitecode/j4rs/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,31 @@ public static Class<?> forNameEnhanced(final String className) throws ClassNotFo
public static Class<?> forNameBasedOnArgs(final GeneratedArg[] params) {
return Arrays.stream(params).map(arg -> arg.getClazz()).reduce((a, b) -> a).orElse(Void.class);
}

// Converts primitive types to their wrapper class. Useful in matching method parameters.
public static Class<?> toWrapper(Class<?> clazz) {
if (!clazz.isPrimitive())
return clazz;

if (clazz == Integer.TYPE)
return Integer.class;
if (clazz == Long.TYPE)
return Long.class;
if (clazz == Boolean.TYPE)
return Boolean.class;
if (clazz == Byte.TYPE)
return Byte.class;
if (clazz == Character.TYPE)
return Character.class;
if (clazz == Float.TYPE)
return Float.class;
if (clazz == Double.TYPE)
return Double.class;
if (clazz == Short.TYPE)
return Short.class;
if (clazz == Void.TYPE)
return Void.class;

return clazz;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.astonbitecode.j4rs.api.Instance;
import org.astonbitecode.j4rs.api.dtos.InvocationArg;
import org.astonbitecode.j4rs.api.dtos.InvocationArgGenerator;
import org.astonbitecode.j4rs.api.instantiation.NativeInstantiationImpl;
import org.astonbitecode.j4rs.errors.InvocationException;
import org.astonbitecode.j4rs.tests.MyTest;
Expand Down Expand Up @@ -156,6 +157,15 @@ public void invokeMethodInHierarchy() {
assert (i.equals(33));
}

@Test
public void invokeMethodInHierarchyParamCheck() {
Instance ni = new JsonInvocationImpl(new GrandchildDummy(), GrandchildDummy.class);
InvocationArg arg = new InvocationArg(new JsonInvocationImpl(3, Integer.class));
Instance res = ni.invoke("checkParam", arg);
Integer i = (Integer) res.getObject();
assert (i.equals(3));
}

@Test
public void invokeMethodInInterface() {
Instance ni = new JsonInvocationImpl(new GrandchildDummy(), GrandchildDummy.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public void doSomething() {
System.out.println("I am doing something...");
}

public int checkParam(int i) { return i; }

public DummyMapInterface<String, Object> getMap() {
return new DummyMapImpl();
}
Expand Down

0 comments on commit 07f919f

Please sign in to comment.