in, out, star projection supported as method params

type parameters also supported
This commit is contained in:
Sergey Ignatov
2011-11-01 13:41:05 +04:00
parent 01c0d11ccc
commit 15eafa6f9c
14 changed files with 190 additions and 7 deletions
+15
View File
@@ -189,6 +189,21 @@ public class Converter {
return result;
}
@NotNull
public static List<Type> typesToNotNullableTypeList(PsiType[] types) {
List<Type> result = new LinkedList<Type>(typesToTypeList(types));
for (Type p : result)
p.setNullable(false);
return result;
}
@NotNull
public static Type typeToNotNullableType(@Nullable PsiType type) {
Type result = typeToType(type);
result.setNullable(false);
return result;
}
@NotNull
private static List<Import> importsToImportList(PsiImportStatementBase[] imports) {
List<Import> result = new LinkedList<Import>();
+3 -2
View File
@@ -15,8 +15,9 @@ public class ArrayType extends Type {
@NotNull
@Override
public String toKotlin() {
String nullable = isNullableStr();
if (PRIMITIVE_TYPES.contains(myType.toKotlin().toLowerCase()))
return myType.toKotlin() + "Array" + QUESTION; // TODO
return "Array" + "<" + myType.toKotlin() + ">" + QUESTION;
return myType.toKotlin() + "Array" + nullable; // TODO
return "Array" + "<" + myType.toKotlin() + ">" + nullable;
}
}
+8 -2
View File
@@ -1,20 +1,26 @@
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 ClassType extends Type {
private final Identifier myType;
private List<Type> myParameters;
public ClassType(Identifier type) {
public ClassType(Identifier type, List<Type> parameters) {
myType = type;
myParameters = parameters;
}
@NotNull
@Override
public String toKotlin() {
return myType.toKotlin() + QUESTION;
String params = myParameters.size() == 0 ? EMPTY : "<" + AstUtil.joinNodes(myParameters, COMMA_WITH_SPACE) + ">";
return myType.toKotlin() + params + isNullableStr();
}
}
@@ -0,0 +1,20 @@
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
/**
* @author ignatov
*/
public class InProjectionType extends Type {
private final Type myBound;
public InProjectionType(Type bound) {
myBound = bound;
}
@NotNull
@Override
public String toKotlin() {
return "in" + SPACE + myBound.toKotlin();
}
}
+2
View File
@@ -10,6 +10,8 @@ import java.util.Set;
* @author ignatov
*/
public abstract class Node implements INode {
public static final String STAR = "*";
@NotNull
@Override
public Kind getKind() {
@@ -0,0 +1,20 @@
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
/**
* @author ignatov
*/
public class OutProjectionType extends Type {
private final Type myBound;
public OutProjectionType(Type bound) {
myBound = bound;
}
@NotNull
@Override
public String toKotlin() {
return "out" + SPACE + myBound.toKotlin();
}
}
@@ -0,0 +1,14 @@
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
/**
* @author ignatov
*/
public class StarProjectionType extends Type {
@NotNull
@Override
public String toKotlin() {
return STAR;
}
}
+14
View File
@@ -6,9 +6,23 @@ import org.jetbrains.annotations.NotNull;
* @author ignatov
*/
public abstract class Type extends Element {
protected boolean myNullable = true;
@NotNull
@Override
public Kind getKind() {
return Kind.TYPE;
}
public void setNullable(boolean nullable) {
myNullable = nullable;
}
public boolean isNullable() {
return myNullable;
}
protected String isNullableStr() {
return isNullable() ? QUESTION : EMPTY;
}
}
@@ -2,10 +2,12 @@ package org.jetbrains.jet.j2k.visitors;
import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.j2k.Converter;
import org.jetbrains.jet.j2k.ast.*;
import org.jetbrains.jet.j2k.util.AstUtil;
import static org.jetbrains.jet.j2k.Converter.typeToType;
import static org.jetbrains.jet.j2k.Converter.typesToTypeList;
/**
* @author ignatov
*/
@@ -19,6 +21,7 @@ public class TypeVisitor extends PsiTypeVisitor<Type> implements Visitor {
@Override
public Type visitType(PsiType type) {
System.out.println(type.getClass()); // TODO: remove
return super.visitType(type);
}
@@ -37,13 +40,16 @@ public class TypeVisitor extends PsiTypeVisitor<Type> implements Visitor {
@Override
public Type visitArrayType(PsiArrayType arrayType) {
myResult = new ArrayType(Converter.typeToType(arrayType.getComponentType()));
myResult = new ArrayType(typeToType(arrayType.getComponentType()));
return super.visitArrayType(arrayType);
}
@Override
public Type visitClassType(PsiClassType classType) {
myResult = new ClassType(new IdentifierImpl(classType.getClassName()));
myResult = new ClassType(
new IdentifierImpl(classType.getClassName()),
typesToTypeList(classType.getParameters())
);
return super.visitClassType(classType);
}
@@ -54,6 +60,12 @@ public class TypeVisitor extends PsiTypeVisitor<Type> implements Visitor {
@Override
public Type visitWildcardType(PsiWildcardType wildcardType) {
if (wildcardType.isExtends())
myResult = new OutProjectionType(typeToType(wildcardType.getExtendsBound()));
else if (wildcardType.isSuper())
myResult = new InProjectionType(typeToType(wildcardType.getSuperBound()));
else
myResult = new StarProjectionType();
return super.visitWildcardType(wildcardType);
}
@@ -34,4 +34,11 @@ public class ArrayTypeTest extends JetTestCaseBase {
"var a : LongArray? = array(1, 2, 3)"
);
}
public void testMethodArrayArgs() throws Exception {
Assert.assertEquals(
methodToSingleLineKotlin("void fromArrayToCollection(Foo[] a) {}"),
"fun fromArrayToCollection(a : Array<Foo?>?) : Unit { }"
);
}
}
@@ -0,0 +1,16 @@
package org.jetbrains.jet.j2k.ast;
import junit.framework.Assert;
import org.jetbrains.jet.j2k.JetTestCaseBase;
/**
* @author ignatov
*/
public class InProjectionTypeTest extends JetTestCaseBase {
public void testMethodParams() throws Exception {
Assert.assertEquals(
methodToSingleLineKotlin("void popAll(Collection<? super E> dst) {}"),
"fun popAll(dst : Collection<in E?>?) : Unit { }"
);
}
}
@@ -0,0 +1,16 @@
package org.jetbrains.jet.j2k.ast;
import junit.framework.Assert;
import org.jetbrains.jet.j2k.JetTestCaseBase;
/**
* @author ignatov
*/
public class OutProjectionTypeTest extends JetTestCaseBase {
public void testMethodParams() throws Exception {
Assert.assertEquals(
methodToSingleLineKotlin("void pushAll(Collection<? extends E> src) {}"),
"fun pushAll(src : Collection<out E?>?) : Unit { }"
);
}
}
@@ -0,0 +1,16 @@
package org.jetbrains.jet.j2k.ast;
import junit.framework.Assert;
import org.jetbrains.jet.j2k.JetTestCaseBase;
/**
* @author ignatov
*/
public class StarProjectionTypeTest extends JetTestCaseBase {
public void testMethodParams() throws Exception {
Assert.assertEquals(
methodToSingleLineKotlin("void wtf(Collection<?> w) {}"),
"fun wtf(w : Collection<*>?) : Unit { }"
);
}
}
@@ -0,0 +1,24 @@
package org.jetbrains.jet.j2k.ast;
import junit.framework.Assert;
import org.jetbrains.jet.j2k.JetTestCaseBase;
/**
* @author ignatov
*/
public class TypeParametersTest extends JetTestCaseBase {
public void testGenericParam() throws Exception {
Assert.assertEquals(
statementToKotlin("List<T> l;"),
"var l : List<T?>?"
);
}
public void testManyGenericParams() throws Exception {
Assert.assertEquals(
statementToKotlin("List<T, K, M> l;"),
"var l : List<T?, K?, M?>?"
);
}
}