in, out, star projection supported as method params
type parameters also supported
This commit is contained in:
@@ -189,6 +189,21 @@ public class Converter {
|
|||||||
return result;
|
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
|
@NotNull
|
||||||
private static List<Import> importsToImportList(PsiImportStatementBase[] imports) {
|
private static List<Import> importsToImportList(PsiImportStatementBase[] imports) {
|
||||||
List<Import> result = new LinkedList<Import>();
|
List<Import> result = new LinkedList<Import>();
|
||||||
|
|||||||
@@ -15,8 +15,9 @@ public class ArrayType extends Type {
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String toKotlin() {
|
public String toKotlin() {
|
||||||
|
String nullable = isNullableStr();
|
||||||
if (PRIMITIVE_TYPES.contains(myType.toKotlin().toLowerCase()))
|
if (PRIMITIVE_TYPES.contains(myType.toKotlin().toLowerCase()))
|
||||||
return myType.toKotlin() + "Array" + QUESTION; // TODO
|
return myType.toKotlin() + "Array" + nullable; // TODO
|
||||||
return "Array" + "<" + myType.toKotlin() + ">" + QUESTION;
|
return "Array" + "<" + myType.toKotlin() + ">" + nullable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,26 @@
|
|||||||
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
|
||||||
*/
|
*/
|
||||||
public class ClassType extends Type {
|
public class ClassType extends Type {
|
||||||
private final Identifier myType;
|
private final Identifier myType;
|
||||||
|
private List<Type> myParameters;
|
||||||
|
|
||||||
public ClassType(Identifier type) {
|
public ClassType(Identifier type, List<Type> parameters) {
|
||||||
myType = type;
|
myType = type;
|
||||||
|
myParameters = parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String toKotlin() {
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,6 +10,8 @@ import java.util.Set;
|
|||||||
* @author ignatov
|
* @author ignatov
|
||||||
*/
|
*/
|
||||||
public abstract class Node implements INode {
|
public abstract class Node implements INode {
|
||||||
|
public static final String STAR = "*";
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public Kind getKind() {
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,9 +6,23 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
* @author ignatov
|
* @author ignatov
|
||||||
*/
|
*/
|
||||||
public abstract class Type extends Element {
|
public abstract class Type extends Element {
|
||||||
|
protected boolean myNullable = true;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public Kind getKind() {
|
public Kind getKind() {
|
||||||
return Kind.TYPE;
|
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 com.intellij.psi.*;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.j2k.Converter;
|
|
||||||
import org.jetbrains.jet.j2k.ast.*;
|
import org.jetbrains.jet.j2k.ast.*;
|
||||||
import org.jetbrains.jet.j2k.util.AstUtil;
|
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
|
* @author ignatov
|
||||||
*/
|
*/
|
||||||
@@ -19,6 +21,7 @@ public class TypeVisitor extends PsiTypeVisitor<Type> implements Visitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Type visitType(PsiType type) {
|
public Type visitType(PsiType type) {
|
||||||
|
System.out.println(type.getClass()); // TODO: remove
|
||||||
return super.visitType(type);
|
return super.visitType(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,13 +40,16 @@ public class TypeVisitor extends PsiTypeVisitor<Type> implements Visitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Type visitArrayType(PsiArrayType arrayType) {
|
public Type visitArrayType(PsiArrayType arrayType) {
|
||||||
myResult = new ArrayType(Converter.typeToType(arrayType.getComponentType()));
|
myResult = new ArrayType(typeToType(arrayType.getComponentType()));
|
||||||
return super.visitArrayType(arrayType);
|
return super.visitArrayType(arrayType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Type visitClassType(PsiClassType classType) {
|
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);
|
return super.visitClassType(classType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,6 +60,12 @@ public class TypeVisitor extends PsiTypeVisitor<Type> implements Visitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Type visitWildcardType(PsiWildcardType wildcardType) {
|
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);
|
return super.visitWildcardType(wildcardType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,4 +34,11 @@ public class ArrayTypeTest extends JetTestCaseBase {
|
|||||||
"var a : LongArray? = array(1, 2, 3)"
|
"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?>?"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user