add support for custom functional interfaces

This commit is contained in:
Renaud Pawlak 2016-01-20 12:05:28 +01:00
parent dac385118e
commit 8c1867846d
3 changed files with 54 additions and 1 deletions

View File

@ -769,7 +769,9 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
report(methodDecl, methodDecl.name, JSweetProblem.WRONG_USE_OF_AMBIENT, methodDecl.name);
}
}
printIdentifier(getTSMethodName(methodDecl));
if(!Util.hasAnnotationType(parent.sym, FunctionalInterface.class.getName())) {
printIdentifier(getTSMethodName(methodDecl));
}
if (methodDecl.typarams != null && !methodDecl.typarams.isEmpty()) {
print("<").printArgList(methodDecl.typarams).print(">");
}
@ -1161,6 +1163,9 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
} else {
if (inv.meth instanceof JCFieldAccess) {
JCExpression selected = ((JCFieldAccess) inv.meth).selected;
if(Util.hasAnnotationType(selected.type.tsym, FunctionalInterface.class.getName())) {
anonymous = true;
}
methSym = Util.findMethodDeclarationInType(context.types, selected.type.tsym, methName, type);
if (methSym != null) {
typeChecker.checkApply(inv, methSym);

View File

@ -26,6 +26,7 @@ import org.junit.Test;
import source.typing.ArraysOfLambdas;
import source.typing.ClassTypeAsFunction;
import source.typing.ClassTypeAsTypeOf;
import source.typing.CustomLambdas;
import source.typing.CustomStringTypes;
import source.typing.InvalidIndexedAccesses;
import source.typing.Lambdas;
@ -192,4 +193,13 @@ public class TypingTests extends AbstractTest {
}
}
@Test
public void testCustomLambdas() {
eval((logHandler, r) -> {
logHandler.assertReportedProblems();
Assert.assertEquals("test1", r.get("lambda"));
} , getSourceFile(CustomLambdas.class));
}
}

View File

@ -0,0 +1,38 @@
/*
* JSweet - http://www.jsweet.org
* Copyright (C) 2015 CINCHEO SAS <renaud.pawlak@cincheo.fr>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package source.typing;
import static jsweet.util.Globals.$export;
@FunctionalInterface
interface MyFunction {
void run(int i, String s);
}
public class CustomLambdas {
void m(MyFunction f) {
f.run(1, "test");
}
public static void main(String[] args) {
new CustomLambdas().m((i, s) -> {
$export("lambda", s + i);
});
}
}