generic parameters for functions added

This commit is contained in:
Sergey Ignatov
2011-11-01 15:54:18 +04:00
parent 8f97c76c07
commit 69b9100890
4 changed files with 40 additions and 5 deletions
+3
View File
@@ -87,17 +87,20 @@ public class Converter {
final Type type = typeToType(method.getReturnType());
final Block body = blockToBlock(method.getBody(), notEmpty);
final Element params = elementToElement(method.getParameterList());
final List<Element> typeParameters = elementsToElementList(method.getTypeParameters());
if (method.isConstructor())
return new Constructor(
identifier,
type,
typeParameters,
params,
body
);
return new Function(
identifier,
type,
typeParameters,
params,
body
);
@@ -2,12 +2,14 @@ package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* @author ignatov
*/
public class Constructor extends Function {
public Constructor(Identifier identifier, Type type, Element params, Block block) {
super(identifier, type, params, block);
public Constructor(Identifier identifier, Type type, List<Element> typeParameters, Element params, Block block) {
super(identifier, type, typeParameters, params, block);
}
public String primary() {
+12 -3
View File
@@ -1,6 +1,9 @@
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.j2k.util.AstUtil;
import java.util.List;
/**
* @author ignatov
@@ -8,20 +11,26 @@ import org.jetbrains.annotations.NotNull;
public class Function extends Node {
private final Identifier myName;
private final Type myType;
private List<Element> myTypeParameters;
final Element myParams;
private final Block myBlock;
public Function(Identifier name, Type type, Element params, Block block) {
public Function(Identifier name, Type type, List<Element> typeParameters, Element params, Block block) {
myName = name;
myType = type;
myTypeParameters = typeParameters;
myParams = params;
myBlock = block;
}
private String typeParametersToKotlin() {
return myTypeParameters.size() > 0 ? "<" + AstUtil.joinNodes(myTypeParameters, COMMA_WITH_SPACE) + ">" : EMPTY;
}
@NotNull
@Override
public String toKotlin() {
return "fun" + SPACE + myName.toKotlin() + "(" + myParams.toKotlin() + ")" + SPACE + COLON + SPACE + myType.toKotlin() + SPACE +
myBlock.toKotlin();
return "fun" + SPACE + myName.toKotlin() + typeParametersToKotlin() + "(" + myParams.toKotlin() + ")" + SPACE + COLON +
SPACE + myType.toKotlin() + SPACE + myBlock.toKotlin();
}
}
@@ -41,4 +41,25 @@ public class FunctionTest extends JetTestCaseBase {
"fun isTrue() : Boolean { return true }"
);
}
public void testClassGenericParam() throws Exception {
Assert.assertEquals(
methodToSingleLineKotlin("T getT() {}"),
"fun getT() : T? { }"
);
}
public void testOwnGenericParam() throws Exception {
Assert.assertEquals(
methodToSingleLineKotlin("<U> void putU(U u) {}"),
"fun putU<U>(u : U?) : Unit { }"
);
}
public void testOwnSeveralGenericParams() throws Exception {
Assert.assertEquals(
methodToSingleLineKotlin("<U, V, W> void putUVW(U u, V v, W w) {}"),
"fun putUVW<U, V, W>(u : U?, v : V?, w : W?) : Unit { }"
);
}
}