mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 15:29:22 +00:00
add support for custom functional interfaces
This commit is contained in:
parent
dac385118e
commit
8c1867846d
@ -769,7 +769,9 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
report(methodDecl, methodDecl.name, JSweetProblem.WRONG_USE_OF_AMBIENT, methodDecl.name);
|
report(methodDecl, methodDecl.name, JSweetProblem.WRONG_USE_OF_AMBIENT, methodDecl.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(!Util.hasAnnotationType(parent.sym, FunctionalInterface.class.getName())) {
|
||||||
printIdentifier(getTSMethodName(methodDecl));
|
printIdentifier(getTSMethodName(methodDecl));
|
||||||
|
}
|
||||||
if (methodDecl.typarams != null && !methodDecl.typarams.isEmpty()) {
|
if (methodDecl.typarams != null && !methodDecl.typarams.isEmpty()) {
|
||||||
print("<").printArgList(methodDecl.typarams).print(">");
|
print("<").printArgList(methodDecl.typarams).print(">");
|
||||||
}
|
}
|
||||||
@ -1161,6 +1163,9 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
} else {
|
} else {
|
||||||
if (inv.meth instanceof JCFieldAccess) {
|
if (inv.meth instanceof JCFieldAccess) {
|
||||||
JCExpression selected = ((JCFieldAccess) inv.meth).selected;
|
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);
|
methSym = Util.findMethodDeclarationInType(context.types, selected.type.tsym, methName, type);
|
||||||
if (methSym != null) {
|
if (methSym != null) {
|
||||||
typeChecker.checkApply(inv, methSym);
|
typeChecker.checkApply(inv, methSym);
|
||||||
|
|||||||
@ -26,6 +26,7 @@ import org.junit.Test;
|
|||||||
import source.typing.ArraysOfLambdas;
|
import source.typing.ArraysOfLambdas;
|
||||||
import source.typing.ClassTypeAsFunction;
|
import source.typing.ClassTypeAsFunction;
|
||||||
import source.typing.ClassTypeAsTypeOf;
|
import source.typing.ClassTypeAsTypeOf;
|
||||||
|
import source.typing.CustomLambdas;
|
||||||
import source.typing.CustomStringTypes;
|
import source.typing.CustomStringTypes;
|
||||||
import source.typing.InvalidIndexedAccesses;
|
import source.typing.InvalidIndexedAccesses;
|
||||||
import source.typing.Lambdas;
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
38
src/test/java/source/typing/CustomLambdas.java
Normal file
38
src/test/java/source/typing/CustomLambdas.java
Normal 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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user