-
-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the primitives for Methref (fixes #777)
- Loading branch information
Showing
3 changed files
with
71 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
jodd-proxetta/src/main/java/jodd/methref/InterfaceMethodInvocationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
jodd-proxetta/src/test/java/jodd/methref/InterfaceMethodInvocationHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |