Support for default and static methods in Functors

This commit is contained in:
marciodel 2017-05-29 10:08:40 -03:00 committed by Alexey Andreev
parent eba3fa3ac4
commit 5ecfb3620c
2 changed files with 41 additions and 2 deletions

View File

@ -991,7 +991,9 @@ class JSClassProcessor {
}
private boolean isProperFunctor(ClassReader type) {
return type.hasModifier(ElementModifier.INTERFACE) && type.getMethods().size() == 1;
return type.hasModifier(ElementModifier.INTERFACE)
&& type.getMethods().stream().filter(it->it.hasModifier(ElementModifier.ABSTRACT))
.toArray().length == 1;
}
private Variable wrapFunctor(CallLocation location, Variable var, ClassReader type) {
@ -999,7 +1001,8 @@ class JSClassProcessor {
diagnostics.error(location, "Wrong functor: {{c0}}", type.getName());
return var;
}
String name = type.getMethods().iterator().next().getName();
String name = type.getMethods().stream()
.filter(it->it.hasModifier(ElementModifier.ABSTRACT)).findFirst().get().getName();
Variable functor = program.createVariable();
Variable nameVar = addStringWrap(addString(name, location.getSourceLocation()), location.getSourceLocation());
InvokeInstruction insn = new InvokeInstruction();

View File

@ -41,6 +41,24 @@ public class FunctorTest {
assertSame(firstRef, secondRef);
}
@Test
public void functorWithDefaultMethodPassed(){
JSFunctionWithDefaultMethod javaFunction = (s) -> s+" returned";
String returned = javaFunction.defaultMethod();
assertEquals(returned, "Content returned");
}
@Test
public void functorWithStaticMethodPassed(){
JSFunctionWithStaticMethod javaFunction = (s) -> s+" returned";
String returned = javaFunction.apply(JSFunctionWithStaticMethod.staticMethod());
assertEquals(returned, "Content returned");
}
@Test
public void propertyWithNonAlphabeticFirstChar() {
WithProperties wp = getWithPropertiesInstance();
@ -63,6 +81,24 @@ public class FunctorTest {
int apply(int a, int b);
}
@JSFunctor
interface JSFunctionWithDefaultMethod extends JSObject {
String apply(String a);
default String defaultMethod(){
return apply("Content");
}
}
@JSFunctor
interface JSFunctionWithStaticMethod extends JSObject {
String apply(String a);
public static String staticMethod(){
return "Content";
}
}
interface WithProperties extends JSObject {
@JSProperty
String get_foo();