Skip to content

Commit

Permalink
Fix the primitives for Methref (fixes #777)
Browse files Browse the repository at this point in the history
  • Loading branch information
igr committed Jan 6, 2021
1 parent 4e2d1f1 commit c31c663
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

package jodd.methref;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class InterfaceImplementation<C> {
Expand All @@ -42,18 +40,11 @@ public static <T> InterfaceImplementation<T> of(final Class<T> target) {

@SuppressWarnings("unchecked")
public C createInstanceFor(final Methref m) {
return (C) Proxy.newProxyInstance(target.getClassLoader(), new Class[]{target}, new InvocationHandler() {
final Methref methref = m;
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) {
final String method_name = method.getName();
m.lastName(method_name);
if (method.getReturnType().isPrimitive()) {
return 0;
}
return null;
}
});
return (C) Proxy.newProxyInstance(
target.getClassLoader(),
new Class[]{target},
new InterfaceMethodInvocationHandler(m)
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package jodd.methref;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

@SuppressWarnings("rawtypes")
public class InterfaceMethodInvocationHandler implements InvocationHandler {
private final Methref methref;

public InterfaceMethodInvocationHandler(final Methref methref) {
this.methref = methref;
}

@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) {
final String method_name = method.getName();
methref.lastName(method_name);
if (method.getReturnType().isPrimitive()) {
final Class primitiveReturnType = method.getReturnType();
if (primitiveReturnType.equals(boolean.class)) {
return false;
}
return 0;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package jodd.methref;

import jodd.util.ClassUtil;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;

import static org.junit.jupiter.api.Assertions.assertEquals;

class InterfaceMethodInvocationHandlerTest {
interface Foo {
boolean b();
int i();
double d();
char c();
float f();
}

@Test
void testPrimitives() {
final Methref methref = new Methref(this.getClass());
final InterfaceMethodInvocationHandler imih = new InterfaceMethodInvocationHandler(methref);

Method m = ClassUtil.findMethod(Foo.class, "b");
assertEquals(false, imih.invoke(null, m, null));

m = ClassUtil.findMethod(Foo.class, "i");
assertEquals(0, imih.invoke(null, m, null));

m = ClassUtil.findMethod(Foo.class, "d");
assertEquals(0, imih.invoke(null, m, null));

m = ClassUtil.findMethod(Foo.class, "c");
assertEquals(0, imih.invoke(null, m, null));

m = ClassUtil.findMethod(Foo.class, "f");
assertEquals(0, imih.invoke(null, m, null));
}
}

0 comments on commit c31c663

Please sign in to comment.