generate !! if the postfix operator is nullable
This commit is contained in:
committed by
Pavel V. Talanov
parent
d4c7b7b2b7
commit
07fcfe3b42
@@ -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?))
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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()),
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
String s = null;
|
||||
if (!s.isEmpty()) { }
|
||||
@@ -0,0 +1,4 @@
|
||||
var s : String? = null
|
||||
if (!s?.isEmpty()!!)
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user