KT-824 Add explicit conversion in conditions
This commit is contained in:
@@ -275,8 +275,7 @@ public class Converter {
|
|||||||
new IdentifierImpl(field.getName()), // TODO
|
new IdentifierImpl(field.getName()), // TODO
|
||||||
modifiers,
|
modifiers,
|
||||||
typeToType(field.getType()),
|
typeToType(field.getType()),
|
||||||
expressionToExpression(field.getInitializer()), // TODO: add modifiers
|
createSureCallOnlyForChain(field.getInitializer(), field.getType()) // TODO: add modifiers
|
||||||
createConversionForCallChains(field.getInitializer(), field.getType())
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -651,9 +650,9 @@ public class Converter {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static String createConversionForCallChains(PsiExpression initializer, PsiType type) {
|
public static SureCallChainExpression createSureCallOnlyForChain(PsiExpression expression, PsiType type) {
|
||||||
if (initializer != null && initializer instanceof PsiReferenceExpression && ((PsiReferenceExpression) initializer).isQualified())
|
String conversion = (expression != null && expression instanceof PsiReferenceExpression && ((PsiReferenceExpression) expression).isQualified()) ?
|
||||||
return createConversionForExpression(initializer, type);
|
createConversionForExpression(expression, type) : "";
|
||||||
return "";
|
return new SureCallChainExpression(expressionToExpression(expression), conversion);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,23 +14,16 @@ import static org.jetbrains.jet.j2k.Converter.getDefaultInitializer;
|
|||||||
*/
|
*/
|
||||||
public class Field extends Member {
|
public class Field extends Member {
|
||||||
final Identifier myIdentifier;
|
final Identifier myIdentifier;
|
||||||
@NotNull
|
|
||||||
private final String myConversion;
|
|
||||||
final Type myType;
|
final Type myType;
|
||||||
final Element myInitializer;
|
final Element myInitializer;
|
||||||
|
|
||||||
public Field(Identifier identifier, Set<String> modifiers, Type type, Element initializer, @NotNull String conversionForCallChains) {
|
public Field(Identifier identifier, Set<String> modifiers, Type type, Element initializer) {
|
||||||
myIdentifier = identifier;
|
myIdentifier = identifier;
|
||||||
myConversion = conversionForCallChains;
|
|
||||||
myModifiers = modifiers;
|
myModifiers = modifiers;
|
||||||
myType = type;
|
myType = type;
|
||||||
myInitializer = initializer;
|
myInitializer = initializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Field(Identifier identifier, Set<String> modifiers, Type type, Element initializer) {
|
|
||||||
this(identifier, modifiers, type, initializer, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
public Element getInitializer() {
|
public Element getInitializer() {
|
||||||
return myInitializer;
|
return myInitializer;
|
||||||
}
|
}
|
||||||
@@ -78,6 +71,6 @@ public class Field extends Member {
|
|||||||
return modifier + myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin() + SPACE + EQUAL + SPACE + getDefaultInitializer(this);
|
return modifier + myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin() + SPACE + EQUAL + SPACE + getDefaultInitializer(this);
|
||||||
|
|
||||||
return modifier + myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin() + SPACE +
|
return modifier + myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin() + SPACE +
|
||||||
EQUAL + SPACE + myInitializer.toKotlin() + myConversion;
|
EQUAL + SPACE + myInitializer.toKotlin();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,14 +12,12 @@ public class LocalVariable extends Expression {
|
|||||||
private final Set<String> myModifiersSet;
|
private final Set<String> myModifiersSet;
|
||||||
private final Type myType;
|
private final Type myType;
|
||||||
private final Expression myInitializer;
|
private final Expression myInitializer;
|
||||||
private final String myConversion;
|
|
||||||
|
|
||||||
public LocalVariable(Identifier identifier, Set<String> modifiersSet, Type type, Expression initializer, @NotNull String conversionForExpression) {
|
public LocalVariable(Identifier identifier, Set<String> modifiersSet, Type type, Expression initializer) {
|
||||||
myIdentifier = identifier;
|
myIdentifier = identifier;
|
||||||
myModifiersSet = modifiersSet;
|
myModifiersSet = modifiersSet;
|
||||||
myType = type;
|
myType = type;
|
||||||
myInitializer = initializer;
|
myInitializer = initializer;
|
||||||
myConversion = conversionForExpression;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasModifier(String modifier) {
|
public boolean hasModifier(String modifier) {
|
||||||
@@ -33,6 +31,6 @@ public class LocalVariable extends Expression {
|
|||||||
return myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin();
|
return myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin();
|
||||||
|
|
||||||
return myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin() + SPACE +
|
return myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin() + SPACE +
|
||||||
EQUAL + SPACE + myInitializer.toKotlin() + myConversion;
|
EQUAL + SPACE + myInitializer.toKotlin();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package org.jetbrains.jet.j2k.ast;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author ignatov
|
||||||
|
*/
|
||||||
|
public class SureCallChainExpression extends Expression {
|
||||||
|
private final Expression myExpression;
|
||||||
|
private final String myConversion;
|
||||||
|
|
||||||
|
public SureCallChainExpression(Expression expression, String conversion) {
|
||||||
|
myExpression = expression;
|
||||||
|
myConversion = conversion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return myExpression.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public String toKotlin() {
|
||||||
|
return myExpression.toKotlin() + myConversion;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,8 +30,7 @@ public class ElementVisitor extends JavaElementVisitor {
|
|||||||
new IdentifierImpl(variable.getName()), // TODO
|
new IdentifierImpl(variable.getName()), // TODO
|
||||||
modifiersListToModifiersSet(variable.getModifierList()),
|
modifiersListToModifiersSet(variable.getModifierList()),
|
||||||
typeToType(variable.getType(), Converter.isNotNull(variable.getModifierList())),
|
typeToType(variable.getType(), Converter.isNotNull(variable.getModifierList())),
|
||||||
expressionToExpression(variable.getInitializer()),
|
createSureCallOnlyForChain(variable.getInitializer(), variable.getType())
|
||||||
createConversionForCallChains(variable.getInitializer(), variable.getType())
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -136,9 +136,13 @@ public class ExpressionVisitor extends StatementVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visitConditionalExpression(@NotNull PsiConditionalExpression expression) {
|
public void visitConditionalExpression(@NotNull PsiConditionalExpression expression) {
|
||||||
super.visitConditionalExpression(expression);
|
super.visitConditionalExpression(expression);
|
||||||
|
PsiExpression condition = expression.getCondition();
|
||||||
|
Expression e = condition.getType() != null ?
|
||||||
|
createSureCallOnlyForChain(condition, condition.getType()) :
|
||||||
|
expressionToExpression(condition);
|
||||||
myResult = new ParenthesizedExpression(
|
myResult = new ParenthesizedExpression(
|
||||||
new IfStatement(
|
new IfStatement(
|
||||||
expressionToExpression(expression.getCondition()),
|
e,
|
||||||
expressionToExpression(expression.getThenExpression()),
|
expressionToExpression(expression.getThenExpression()),
|
||||||
expressionToExpression(expression.getElseExpression())
|
expressionToExpression(expression.getElseExpression())
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -15,8 +15,6 @@ import java.util.LinkedList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.jetbrains.jet.j2k.Converter.*;
|
import static org.jetbrains.jet.j2k.Converter.*;
|
||||||
import static org.jetbrains.jet.j2k.Converter.getPrimitiveTypeConversion;
|
|
||||||
import static org.jetbrains.jet.j2k.Converter.isConversionNeeded;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ignatov
|
* @author ignatov
|
||||||
@@ -81,8 +79,12 @@ public class StatementVisitor extends ElementVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visitDoWhileStatement(@NotNull PsiDoWhileStatement statement) {
|
public void visitDoWhileStatement(@NotNull PsiDoWhileStatement statement) {
|
||||||
super.visitDoWhileStatement(statement);
|
super.visitDoWhileStatement(statement);
|
||||||
|
PsiExpression condition = statement.getCondition();
|
||||||
|
Expression expression = condition != null && condition.getType() != null ?
|
||||||
|
createSureCallOnlyForChain(condition, condition.getType()) :
|
||||||
|
expressionToExpression(condition);
|
||||||
myResult = new DoWhileStatement(
|
myResult = new DoWhileStatement(
|
||||||
expressionToExpression(statement.getCondition()),
|
expression,
|
||||||
statementToStatement(statement.getBody())
|
statementToStatement(statement.getBody())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -176,8 +178,12 @@ public class StatementVisitor extends ElementVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visitIfStatement(@NotNull PsiIfStatement statement) {
|
public void visitIfStatement(@NotNull PsiIfStatement statement) {
|
||||||
super.visitIfStatement(statement);
|
super.visitIfStatement(statement);
|
||||||
|
PsiExpression condition = statement.getCondition();
|
||||||
|
Expression expression = condition != null && condition.getType() != null ?
|
||||||
|
createSureCallOnlyForChain(condition, condition.getType()) :
|
||||||
|
expressionToExpression(condition);
|
||||||
myResult = new IfStatement(
|
myResult = new IfStatement(
|
||||||
expressionToExpression(statement.getCondition()),
|
expression,
|
||||||
statementToStatement(statement.getThenBranch()),
|
statementToStatement(statement.getThenBranch()),
|
||||||
statementToStatement(statement.getElseBranch())
|
statementToStatement(statement.getElseBranch())
|
||||||
);
|
);
|
||||||
@@ -327,8 +333,12 @@ public class StatementVisitor extends ElementVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visitWhileStatement(@NotNull PsiWhileStatement statement) {
|
public void visitWhileStatement(@NotNull PsiWhileStatement statement) {
|
||||||
super.visitWhileStatement(statement);
|
super.visitWhileStatement(statement);
|
||||||
|
PsiExpression condition = statement.getCondition();
|
||||||
|
Expression expression = condition != null && condition.getType() != null ?
|
||||||
|
createSureCallOnlyForChain(condition, condition.getType()) :
|
||||||
|
expressionToExpression(condition);
|
||||||
myResult = new WhileStatement(
|
myResult = new WhileStatement(
|
||||||
expressionToExpression(statement.getCondition()),
|
expression,
|
||||||
statementToStatement(statement.getBody())
|
statementToStatement(statement.getBody())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package demo;
|
||||||
|
|
||||||
|
class Container {
|
||||||
|
boolean myBoolean = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
class One {
|
||||||
|
static Container myContainer = new Container();
|
||||||
|
}
|
||||||
|
|
||||||
|
class Test {
|
||||||
|
void test() {
|
||||||
|
if (One.myContainer.myBoolean)
|
||||||
|
System.out.println("Ok");
|
||||||
|
|
||||||
|
String s = One.myContainer.myBoolean ? "YES" : "NO";
|
||||||
|
|
||||||
|
while (One.myContainer.myBoolean)
|
||||||
|
System.out.println("Ok");
|
||||||
|
|
||||||
|
do {
|
||||||
|
System.out.println("Ok");
|
||||||
|
} while (One.myContainer.myBoolean)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
namespace demo
|
||||||
|
open class Container() {
|
||||||
|
var myBoolean : Boolean = true
|
||||||
|
}
|
||||||
|
open class One() {
|
||||||
|
class object {
|
||||||
|
var myContainer : Container? = Container()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
open class Test() {
|
||||||
|
open fun test() : Unit {
|
||||||
|
if (One.myContainer?.myBoolean.sure())
|
||||||
|
System.out?.println("Ok")
|
||||||
|
var s : String? = (if (One.myContainer?.myBoolean.sure())
|
||||||
|
"YES"
|
||||||
|
else
|
||||||
|
"NO")
|
||||||
|
while (One.myContainer?.myBoolean.sure())
|
||||||
|
System.out?.println("Ok")
|
||||||
|
do
|
||||||
|
{
|
||||||
|
System.out?.println("Ok")
|
||||||
|
}
|
||||||
|
while (One.myContainer?.myBoolean.sure())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user