where statement for type parameters supported in class and trait declaration

This commit is contained in:
Sergey Ignatov
2011-11-02 14:06:48 +04:00
parent 5d120e800f
commit 2e6a65c0cd
2 changed files with 74 additions and 1 deletions
+22 -1
View File
@@ -29,6 +29,24 @@ public class Class extends Node {
myFields = fields;
}
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
List<Function> methodsExceptConstructors() {
final LinkedList<Function> result = new LinkedList<Function>();
@@ -55,7 +73,10 @@ public class Class extends Node {
@NotNull
@Override
public String toKotlin() {
return TYPE + SPACE + myName.toKotlin() + typeParametersToKotlin() + implementTypesToKotlin() + SPACE + "{" + N +
return TYPE + SPACE + myName.toKotlin() + typeParametersToKotlin() +
implementTypesToKotlin() +
typeParameterWhereToKotlin() +
SPACE + "{" + N +
AstUtil.joinNodes(myFields, N) + N +
AstUtil.joinNodes(methodsExceptConstructors(), N) + N +
AstUtil.joinNodes(myInnerClasses, N) + N +
@@ -36,4 +36,56 @@ public class TypeParametersTest extends JetTestCaseBase {
"fun max<T : Object?, K : Node?>(coll : Collection<out T?>?) : T? where T : Comparable<in T?>?, K : Collection<in K?>? { }"
);
}
public void testClassDoubleParametrizationWithTwoBoundsWithExtending() throws Exception {
Assert.assertEquals(
classToSingleLineKotlin("final class CC <T extends INode & Comparable<? super T>, K extends Node & Collection<? super K>> extends A {}"),
"class CC<T : INode?, K : Node?> : A where T : Comparable<in T?>?, K : Collection<in K?>? { }"
);
}
public void testTraitDoubleParametrizationWithTwoBoundsWithExtending() throws Exception {
Assert.assertEquals(
classToSingleLineKotlin(
"interface I <T extends INode & Comparable<? super T>, K extends Node & Collection<? super K>> extends II {}"),
"trait I<T : INode?, K : Node?> : II where T : Comparable<in T?>?, K : Collection<in K?>? { }"
);
}
public void testGenericClass() throws Exception {
Assert.assertEquals(
classToSingleLineKotlin("final class Comparable<T> {}"),
"class Comparable<T> { }"
);
}
public void testComplexExampleWithClassExtending() throws Exception {
Assert.assertEquals(
classToSingleLineKotlin(
"interface CommandHandler<T extends Command> {}"),
"trait CommandHandler<T : Command?> { }"
);
}
public void testClassParametrizationWithTwoBounds() throws Exception {
Assert.assertEquals(
classToSingleLineKotlin("final class C<T extends INode & Comparable<? super T>> {}"),
"class C<T : INode?> where T : Comparable<in T?>? { }"
);
}
public void testClassParametrizationWithTwoBoundsWithExtending() throws Exception {
Assert.assertEquals(
classToSingleLineKotlin("final class C<T extends INode & Comparable<? super T>> extends A {}"),
"class C<T : INode?> : A where T : Comparable<in T?>? { }"
);
}
public void testComplexExampleWithClassMultiplyExtending() throws Exception {
Assert.assertEquals(
classToSingleLineKotlin(
"interface CommandHandler<T extends INode, String> {}"),
"trait CommandHandler<T : INode?, String> { }"
);
}
}