TypeCastExpression added
This commit is contained in:
@@ -132,7 +132,7 @@ public class Converter {
|
||||
final StatementVisitor statementVisitor = new StatementVisitor();
|
||||
s.accept(statementVisitor);
|
||||
System.out.println(s.getClass());
|
||||
return statementVisitor.getStatement();
|
||||
return statementVisitor.getResult();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -172,7 +172,7 @@ public class Converter {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Type typeToType(PsiType type) {
|
||||
public static Type typeToType(@Nullable PsiType type) {
|
||||
if (type == null)
|
||||
return new EmptyType(); // TODO
|
||||
TypeVisitor typeVisitor = new TypeVisitor();
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class TypeCastExpression extends Expression {
|
||||
private final Type myType;
|
||||
private final Expression myExpression;
|
||||
|
||||
public TypeCastExpression(Type type, Expression expression) {
|
||||
myType = type;
|
||||
myExpression = expression;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String toKotlin() {
|
||||
return "(" + myExpression.toKotlin() + SPACE + "as" + SPACE + myType.toKotlin() + ")";
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,12 @@ import static org.jetbrains.jet.j2k.Converter.*;
|
||||
public class ElementVisitor extends JavaElementVisitor implements Visitor {
|
||||
private Element myResult = new EmptyElement();
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Element getResult() {
|
||||
return myResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitLocalVariable(PsiLocalVariable variable) {
|
||||
super.visitLocalVariable(variable);
|
||||
@@ -42,9 +48,4 @@ public class ElementVisitor extends JavaElementVisitor implements Visitor {
|
||||
Converter.parametersToParameterList(list.getParameters())
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Element getResult() {
|
||||
return myResult;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,6 +216,14 @@ public class ExpressionVisitor extends StatementVisitor implements Visitor {
|
||||
@Override
|
||||
public void visitTypeCastExpression(PsiTypeCastExpression expression) {
|
||||
super.visitTypeCastExpression(expression);
|
||||
|
||||
final PsiTypeElement castType = expression.getCastType();
|
||||
if (castType != null) {
|
||||
myResult = new TypeCastExpression(
|
||||
typeToType(castType.getType()),
|
||||
expressionToExpression(expression.getOperand())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.jet.j2k.JetTestCaseBase;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class TypeCastExpressionTest extends JetTestCaseBase {
|
||||
public void testStringCast() throws Exception {
|
||||
Assert.assertEquals(
|
||||
expressionToKotlin("(String)t"),
|
||||
"(t as String?)"
|
||||
);
|
||||
}
|
||||
|
||||
public void testIntCast() throws Exception {
|
||||
Assert.assertEquals(
|
||||
expressionToKotlin("(int)t"),
|
||||
"(t as Int)"
|
||||
);
|
||||
}
|
||||
|
||||
public void testFooCast() throws Exception {
|
||||
Assert.assertEquals(
|
||||
expressionToKotlin("(Foo)t"),
|
||||
"(t as Foo?)"
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: add support for generics and uncomment
|
||||
// public void testSimpleGenericCast() throws Exception {
|
||||
// Assert.assertEquals(
|
||||
// expressionToKotlin("(List<Expression>)list"),
|
||||
// "(list as List<Expression?>)"
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// public void testExtendsWildcardCast() throws Exception {
|
||||
// Assert.assertEquals(
|
||||
// expressionToKotlin("(List<? extends String>)list"),
|
||||
// "(list as List<out String?>)"
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// public void testSuperWildcardCast() throws Exception {
|
||||
// Assert.assertEquals(
|
||||
// expressionToKotlin("(List<? super String>)list"),
|
||||
// "(list as List<in String?>)"
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// public void testWildcardCast() throws Exception {
|
||||
// Assert.assertEquals(
|
||||
// expressionToKotlin("(List<?>)list"),
|
||||
// "(list as List<*>)"
|
||||
// );
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user