diff --git a/src/org/jetbrains/jet/j2k/Converter.kt b/src/org/jetbrains/jet/j2k/Converter.kt index 88b2f6e82e5..22aa988310a 100644 --- a/src/org/jetbrains/jet/j2k/Converter.kt +++ b/src/org/jetbrains/jet/j2k/Converter.kt @@ -414,15 +414,14 @@ public open class Converter() { var expression: Expression = expressionToExpression(argument) val actualType: PsiType? = argument.getType() val isPrimitiveTypeOrNull: Boolean = actualType == null || actualType is PsiPrimitiveType - var isRef: Boolean = ((argument is PsiReferenceExpression && argument.isQualified()) || argument is PsiMethodCallExpression) - if (isPrimitiveTypeOrNull && isRef && expression.isNullable()) { + if (isPrimitiveTypeOrNull && expression.isNullable()) { expression = BangBangExpression(expression) } if (actualType != null) { if (isConversionNeeded(actualType, expectedType) && !(expression is LiteralExpression)) { - var conversion: String? = PRIMITIVE_TYPE_CONVERSIONS?.get(expectedType?.getCanonicalText()) + var conversion: String? = PRIMITIVE_TYPE_CONVERSIONS.get(expectedType?.getCanonicalText()) if (conversion != null) { expression = DummyMethodCallExpression(expression, conversion, (Identifier.EMPTY_IDENTIFIER as IdentifierImpl?)) diff --git a/src/org/jetbrains/jet/j2k/ast/IfStatement.java b/src/org/jetbrains/jet/j2k/ast/IfStatement.java deleted file mode 100644 index 1efa8311a56..00000000000 --- a/src/org/jetbrains/jet/j2k/ast/IfStatement.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2010-2012 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.jet.j2k.ast; - -import org.jetbrains.annotations.NotNull; - -/** - * @author ignatov - */ -public class IfStatement extends Expression { - private final Expression myCondition; - private final Statement myThenStatement; - private final Statement myElseStatement; - - public IfStatement(Expression condition, Statement thenStatement, Statement elseStatement) { - myCondition = condition; - myThenStatement = thenStatement; - myElseStatement = elseStatement; - } - - @NotNull - @Override - public String toKotlin() { - String result = "if" + SPACE + "(" + myCondition.toKotlin() + ")" + N + myThenStatement.toKotlin() + N; - - if (myElseStatement != Statement.EMPTY_STATEMENT) { - return result + - "else" + N + - myElseStatement.toKotlin(); - } - - return result; - } -} diff --git a/src/org/jetbrains/jet/j2k/ast/IfStatement.kt b/src/org/jetbrains/jet/j2k/ast/IfStatement.kt new file mode 100644 index 00000000000..65915e24c49 --- /dev/null +++ b/src/org/jetbrains/jet/j2k/ast/IfStatement.kt @@ -0,0 +1,13 @@ +package org.jetbrains.jet.j2k.ast + + +public open class IfStatement(val condition: Expression, val thenStatement: Statement, val elseStatement: Statement): Expression() { + public override fun toKotlin(): String { + val result: String = "if (" + condition.toKotlin() + ")\n" + thenStatement.toKotlin() + "\n" + if (elseStatement != Statement.EMPTY_STATEMENT) { + return result + "else\n" + elseStatement.toKotlin() + } + + return result + } +} diff --git a/src/org/jetbrains/jet/j2k/ast/PostfixOperator.java b/src/org/jetbrains/jet/j2k/ast/PostfixOperator.java deleted file mode 100644 index 3b6eafdefac..00000000000 --- a/src/org/jetbrains/jet/j2k/ast/PostfixOperator.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2010-2012 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.jet.j2k.ast; - -import org.jetbrains.annotations.NotNull; - -/** - * @author ignatov - */ -public class PostfixOperator extends Expression { - private final String myOp; - private final Expression myExpression; - - public PostfixOperator(String op, Expression expression) { - myOp = op; - myExpression = expression; - } - - @NotNull - @Override - public String toKotlin() { - return myExpression.toKotlin() + myOp; - } -} diff --git a/src/org/jetbrains/jet/j2k/ast/PostfixOperator.kt b/src/org/jetbrains/jet/j2k/ast/PostfixOperator.kt new file mode 100644 index 00000000000..3f71a035c13 --- /dev/null +++ b/src/org/jetbrains/jet/j2k/ast/PostfixOperator.kt @@ -0,0 +1,6 @@ +package org.jetbrains.jet.j2k.ast + + +public open class PostfixOperator(val op: String, val expression: Expression): Expression() { + public override fun toKotlin() = expression.toKotlin() + op +} diff --git a/src/org/jetbrains/jet/j2k/ast/PrefixOperator.java b/src/org/jetbrains/jet/j2k/ast/PrefixOperator.java deleted file mode 100644 index 28b4c92cff9..00000000000 --- a/src/org/jetbrains/jet/j2k/ast/PrefixOperator.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2010-2012 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.jet.j2k.ast; - -import org.jetbrains.annotations.NotNull; - -/** - * @author ignatov - */ -public class PrefixOperator extends Expression { - private final String myOp; - private final Expression myExpression; - - public PrefixOperator(String op, Expression expression) { - myOp = op; - myExpression = expression; - } - - @NotNull - @Override - public String toKotlin() { - return myOp + myExpression.toKotlin(); - } -} diff --git a/src/org/jetbrains/jet/j2k/ast/PrefixOperator.kt b/src/org/jetbrains/jet/j2k/ast/PrefixOperator.kt new file mode 100644 index 00000000000..d8db37ea52b --- /dev/null +++ b/src/org/jetbrains/jet/j2k/ast/PrefixOperator.kt @@ -0,0 +1,7 @@ +package org.jetbrains.jet.j2k.ast + + +public open class PrefixOperator(val op: String, val expression: Expression): Expression() { + public override fun toKotlin() = op + expression.toKotlin() + public override fun isNullable() = expression.isNullable() +} diff --git a/src/org/jetbrains/jet/j2k/visitors/StatementVisitor.java b/src/org/jetbrains/jet/j2k/visitors/StatementVisitor.java index a5988afc5ac..32aeef3a88b 100644 --- a/src/org/jetbrains/jet/j2k/visitors/StatementVisitor.java +++ b/src/org/jetbrains/jet/j2k/visitors/StatementVisitor.java @@ -205,9 +205,7 @@ public class StatementVisitor extends ElementVisitor { super.visitIfStatement(statement); PsiExpression condition = statement.getCondition(); @SuppressWarnings("ConstantConditions") - Expression expression = condition != null && condition.getType() != null ? - this.getConverter().expressionToExpression(condition, condition.getType()) : - getConverter().expressionToExpression(condition); + Expression expression = getConverter().expressionToExpression(condition, PsiType.BOOLEAN); myResult = new IfStatement( expression, getConverter().statementToStatement(statement.getThenBranch()), diff --git a/tests/testData/ast/issues/file/kt-824-isDir.kt b/tests/testData/ast/issues/file/kt-824-isDir.kt index 1879496fd0b..34e7ddca7ec 100644 --- a/tests/testData/ast/issues/file/kt-824-isDir.kt +++ b/tests/testData/ast/issues/file/kt-824-isDir.kt @@ -3,7 +3,7 @@ import java.io.File public open class Test() { class object { public open fun isDir(parent : File?) : Boolean { -if (parent == null || !parent?.exists()) +if (parent == null || !parent?.exists()!!) { return false } diff --git a/tests/testData/ast/prefixOperator/statement/nullableIf.jav b/tests/testData/ast/prefixOperator/statement/nullableIf.jav new file mode 100644 index 00000000000..539180d48fb --- /dev/null +++ b/tests/testData/ast/prefixOperator/statement/nullableIf.jav @@ -0,0 +1,2 @@ +String s = null; +if (!s.isEmpty()) { } diff --git a/tests/testData/ast/prefixOperator/statement/nullableIf.kt b/tests/testData/ast/prefixOperator/statement/nullableIf.kt new file mode 100644 index 00000000000..bd253b95ddf --- /dev/null +++ b/tests/testData/ast/prefixOperator/statement/nullableIf.kt @@ -0,0 +1,4 @@ +var s : String? = null +if (!s?.isEmpty()!!) +{ +} \ No newline at end of file