where statement for type parameters supported in functions declaration

This commit is contained in:
Sergey Ignatov
2011-11-02 13:46:39 +04:00
parent 341aed521e
commit 5d120e800f
5 changed files with 60 additions and 3 deletions
+22 -1
View File
@@ -3,6 +3,7 @@ package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.j2k.util.AstUtil;
import java.util.LinkedList;
import java.util.List;
/**
@@ -27,10 +28,30 @@ public class Function extends Node {
return myTypeParameters.size() > 0 ? "<" + AstUtil.joinNodes(myTypeParameters, COMMA_WITH_SPACE) + ">" : EMPTY;
}
private boolean hasWhere() {
for (Element t : myTypeParameters)
if (t instanceof TypeParameter && ((TypeParameter)t).hasWhere())
return true;
return false;
}
String typeParameterWhereToKotlin() {
if (hasWhere()) {
List<String> wheres = new LinkedList<String >();
for (Element t : myTypeParameters)
if (t instanceof TypeParameter)
wheres.add(((TypeParameter)t).getWhereToKotlin());
return SPACE + "where" + SPACE + AstUtil.join(wheres, COMMA_WITH_SPACE) + SPACE;
}
return EMPTY;
}
@NotNull
@Override
public String toKotlin() {
return "fun" + SPACE + myName.toKotlin() + typeParametersToKotlin() + "(" + myParams.toKotlin() + ")" + SPACE + COLON +
SPACE + myType.toKotlin() + SPACE + myBlock.toKotlin();
SPACE + myType.toKotlin() + SPACE +
typeParameterWhereToKotlin() +
myBlock.toKotlin();
}
}
@@ -1,20 +1,38 @@
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.j2k.util.AstUtil;
import java.util.List;
/**
* @author ignatov
*/
public class TypeParameter extends Element {
private Identifier myName;
private List<Type> myExtendsTypes;
public TypeParameter(Identifier name) {
public TypeParameter(Identifier name, List<Type> extendsTypes) {
myName = name;
myExtendsTypes = extendsTypes;
}
public boolean hasWhere() {
return myExtendsTypes.size() > 1;
}
@NotNull
public String getWhereToKotlin() {
if (hasWhere())
return myName.toKotlin() + SPACE + COLON + SPACE + myExtendsTypes.get(1).toKotlin();
return EMPTY;
}
@NotNull
@Override
public String toKotlin() {
if (myExtendsTypes.size() > 0)
return myName.toKotlin() + SPACE + COLON + SPACE + myExtendsTypes.get(0).toKotlin();
return myName.toKotlin();
}
}
@@ -48,7 +48,8 @@ public class ElementVisitor extends JavaElementVisitor implements Visitor {
public void visitTypeParameter(PsiTypeParameter classParameter) {
super.visitTypeParameter(classParameter);
myResult = new TypeParameter(
new IdentifierImpl(classParameter.getName())
new IdentifierImpl(classParameter.getName()), // TODO
typesToTypeList(classParameter.getExtendsListTypes())
);
}
@@ -22,6 +22,8 @@ public class TypeVisitor extends PsiTypeVisitor<Type> implements Visitor {
@Override
public Type visitType(PsiType type) {
System.out.println(type.getClass()); // TODO: remove
if (type instanceof PsiIntersectionType)
System.out.println("&");
return super.visitType(type);
}
@@ -21,4 +21,19 @@ public class TypeParametersTest extends JetTestCaseBase {
);
}
public void testWhere() throws Exception {
Assert.assertEquals(
methodToSingleLineKotlin(
"<T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) {}"),
"fun max<T : Object?>(coll : Collection<out T?>?) : T? where T : Comparable<in T?>? { }"
);
}
public void testMethodDoubleParametrizationWithTwoBounds() throws Exception {
Assert.assertEquals(
methodToSingleLineKotlin(
"<T extends Object & Comparable<? super T>, K extends Node & Collection<? super K>> T max(Collection<? extends T> coll) {}"),
"fun max<T : Object?, K : Node?>(coll : Collection<out T?>?) : T? where T : Comparable<in T?>?, K : Collection<in K?>? { }"
);
}
}