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 Type type = typeToType(method.getReturnType());
final Block body = blockToBlock(method.getBody(), notEmpty); final Block body = blockToBlock(method.getBody(), notEmpty);
final Element params = elementToElement(method.getParameterList()); final Element params = elementToElement(method.getParameterList());
final List<Element> typeParameters = elementsToElementList(method.getTypeParameters());
if (method.isConstructor()) if (method.isConstructor())
return new Constructor( return new Constructor(
identifier, identifier,
type, type,
typeParameters,
params, params,
body body
); );
return new Function( return new Function(
identifier, identifier,
type, type,
typeParameters,
params, params,
body body
); );
@@ -2,12 +2,14 @@ package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List;
/** /**
* @author ignatov * @author ignatov
*/ */
public class Constructor extends Function { public class Constructor extends Function {
public Constructor(Identifier identifier, Type type, Element params, Block block) { public Constructor(Identifier identifier, Type type, List<Element> typeParameters, Element params, Block block) {
super(identifier, type, params, block); super(identifier, type, typeParameters, params, block);
} }
public String primary() { public String primary() {
+12 -3
View File
@@ -1,6 +1,9 @@
package org.jetbrains.jet.j2k.ast; package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.j2k.util.AstUtil;
import java.util.List;
/** /**
* @author ignatov * @author ignatov
@@ -8,20 +11,26 @@ import org.jetbrains.annotations.NotNull;
public class Function extends Node { public class Function extends Node {
private final Identifier myName; private final Identifier myName;
private final Type myType; private final Type myType;
private List<Element> myTypeParameters;
final Element myParams; final Element myParams;
private final Block myBlock; 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; myName = name;
myType = type; myType = type;
myTypeParameters = typeParameters;
myParams = params; myParams = params;
myBlock = block; myBlock = block;
} }
private String typeParametersToKotlin() {
return myTypeParameters.size() > 0 ? "<" + AstUtil.joinNodes(myTypeParameters, COMMA_WITH_SPACE) + ">" : EMPTY;
}
@NotNull @NotNull
@Override @Override
public String toKotlin() { public String toKotlin() {
return "fun" + SPACE + myName.toKotlin() + "(" + myParams.toKotlin() + ")" + SPACE + COLON + SPACE + myType.toKotlin() + SPACE + return "fun" + SPACE + myName.toKotlin() + typeParametersToKotlin() + "(" + myParams.toKotlin() + ")" + SPACE + COLON +
myBlock.toKotlin(); SPACE + myType.toKotlin() + SPACE + myBlock.toKotlin();
} }
} }
@@ -41,4 +41,25 @@ public class FunctionTest extends JetTestCaseBase {
"fun isTrue() : Boolean { return true }" "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 { }"
);
}
} }