KT-824 Add explicit conversion in conditions
This commit is contained in:
@@ -275,8 +275,7 @@ public class Converter {
|
||||
new IdentifierImpl(field.getName()), // TODO
|
||||
modifiers,
|
||||
typeToType(field.getType()),
|
||||
expressionToExpression(field.getInitializer()), // TODO: add modifiers
|
||||
createConversionForCallChains(field.getInitializer(), field.getType())
|
||||
createSureCallOnlyForChain(field.getInitializer(), field.getType()) // TODO: add modifiers
|
||||
);
|
||||
}
|
||||
|
||||
@@ -651,9 +650,9 @@ public class Converter {
|
||||
// }
|
||||
|
||||
@NotNull
|
||||
public static String createConversionForCallChains(PsiExpression initializer, PsiType type) {
|
||||
if (initializer != null && initializer instanceof PsiReferenceExpression && ((PsiReferenceExpression) initializer).isQualified())
|
||||
return createConversionForExpression(initializer, type);
|
||||
return "";
|
||||
public static SureCallChainExpression createSureCallOnlyForChain(PsiExpression expression, PsiType type) {
|
||||
String conversion = (expression != null && expression instanceof PsiReferenceExpression && ((PsiReferenceExpression) expression).isQualified()) ?
|
||||
createConversionForExpression(expression, type) : "";
|
||||
return new SureCallChainExpression(expressionToExpression(expression), conversion);
|
||||
}
|
||||
}
|
||||
@@ -14,23 +14,16 @@ import static org.jetbrains.jet.j2k.Converter.getDefaultInitializer;
|
||||
*/
|
||||
public class Field extends Member {
|
||||
final Identifier myIdentifier;
|
||||
@NotNull
|
||||
private final String myConversion;
|
||||
final Type myType;
|
||||
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;
|
||||
myConversion = conversionForCallChains;
|
||||
myModifiers = modifiers;
|
||||
myType = type;
|
||||
myInitializer = initializer;
|
||||
}
|
||||
|
||||
public Field(Identifier identifier, Set<String> modifiers, Type type, Element initializer) {
|
||||
this(identifier, modifiers, type, initializer, "");
|
||||
}
|
||||
|
||||
public Element getInitializer() {
|
||||
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 + myInitializer.toKotlin() + myConversion;
|
||||
EQUAL + SPACE + myInitializer.toKotlin();
|
||||
}
|
||||
}
|
||||
@@ -12,14 +12,12 @@ public class LocalVariable extends Expression {
|
||||
private final Set<String> myModifiersSet;
|
||||
private final Type myType;
|
||||
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;
|
||||
myModifiersSet = modifiersSet;
|
||||
myType = type;
|
||||
myInitializer = initializer;
|
||||
myConversion = conversionForExpression;
|
||||
}
|
||||
|
||||
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() + 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
|
||||
modifiersListToModifiersSet(variable.getModifierList()),
|
||||
typeToType(variable.getType(), Converter.isNotNull(variable.getModifierList())),
|
||||
expressionToExpression(variable.getInitializer()),
|
||||
createConversionForCallChains(variable.getInitializer(), variable.getType())
|
||||
createSureCallOnlyForChain(variable.getInitializer(), variable.getType())
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -136,9 +136,13 @@ public class ExpressionVisitor extends StatementVisitor {
|
||||
@Override
|
||||
public void visitConditionalExpression(@NotNull PsiConditionalExpression expression) {
|
||||
super.visitConditionalExpression(expression);
|
||||
PsiExpression condition = expression.getCondition();
|
||||
Expression e = condition.getType() != null ?
|
||||
createSureCallOnlyForChain(condition, condition.getType()) :
|
||||
expressionToExpression(condition);
|
||||
myResult = new ParenthesizedExpression(
|
||||
new IfStatement(
|
||||
expressionToExpression(expression.getCondition()),
|
||||
e,
|
||||
expressionToExpression(expression.getThenExpression()),
|
||||
expressionToExpression(expression.getElseExpression())
|
||||
)
|
||||
|
||||
@@ -15,8 +15,6 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
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
|
||||
@@ -81,8 +79,12 @@ public class StatementVisitor extends ElementVisitor {
|
||||
@Override
|
||||
public void visitDoWhileStatement(@NotNull PsiDoWhileStatement statement) {
|
||||
super.visitDoWhileStatement(statement);
|
||||
PsiExpression condition = statement.getCondition();
|
||||
Expression expression = condition != null && condition.getType() != null ?
|
||||
createSureCallOnlyForChain(condition, condition.getType()) :
|
||||
expressionToExpression(condition);
|
||||
myResult = new DoWhileStatement(
|
||||
expressionToExpression(statement.getCondition()),
|
||||
expression,
|
||||
statementToStatement(statement.getBody())
|
||||
);
|
||||
}
|
||||
@@ -176,8 +178,12 @@ public class StatementVisitor extends ElementVisitor {
|
||||
@Override
|
||||
public void visitIfStatement(@NotNull PsiIfStatement statement) {
|
||||
super.visitIfStatement(statement);
|
||||
PsiExpression condition = statement.getCondition();
|
||||
Expression expression = condition != null && condition.getType() != null ?
|
||||
createSureCallOnlyForChain(condition, condition.getType()) :
|
||||
expressionToExpression(condition);
|
||||
myResult = new IfStatement(
|
||||
expressionToExpression(statement.getCondition()),
|
||||
expression,
|
||||
statementToStatement(statement.getThenBranch()),
|
||||
statementToStatement(statement.getElseBranch())
|
||||
);
|
||||
@@ -327,8 +333,12 @@ public class StatementVisitor extends ElementVisitor {
|
||||
@Override
|
||||
public void visitWhileStatement(@NotNull PsiWhileStatement statement) {
|
||||
super.visitWhileStatement(statement);
|
||||
PsiExpression condition = statement.getCondition();
|
||||
Expression expression = condition != null && condition.getType() != null ?
|
||||
createSureCallOnlyForChain(condition, condition.getType()) :
|
||||
expressionToExpression(condition);
|
||||
myResult = new WhileStatement(
|
||||
expressionToExpression(statement.getCondition()),
|
||||
expression,
|
||||
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