Fixed KT-4437 Converter for java: do not generate assert with lambda for constant message

#KT-4437 Fixed
This commit is contained in:
Valentin Kipyatkov
2014-06-11 13:17:51 +04:00
parent 149ea16f5c
commit ed290178d5
6 changed files with 23 additions and 31 deletions
@@ -1,28 +0,0 @@
/*
* Copyright 2010-2013 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
class AssertStatement(val condition: Expression, val detail: Expression) : Statement() {
override fun toKotlin(): String {
val lazyStringMessage = if (detail != Expression.Empty)
" {" + detail.toKotlin() + "}"
else
""
return "assert(${condition.toKotlin()})$lazyStringMessage"
}
}
@@ -30,8 +30,20 @@ open class StatementVisitor(public val converter: Converter) : JavaElementVisito
protected set
override fun visitAssertStatement(statement: PsiAssertStatement) {
result = AssertStatement(converter.convertExpression(statement.getAssertCondition()),
converter.convertExpression(statement.getAssertDescription()))
val descriptionExpr = statement.getAssertDescription()
val condition = converter.convertExpression(statement.getAssertCondition())
if (descriptionExpr == null) {
result = MethodCallExpression.buildNotNull(null, "assert", listOf(condition))
}
else {
val description = converter.convertExpression(descriptionExpr)
if (descriptionExpr is PsiLiteralExpression) {
result = MethodCallExpression.buildNotNull(null, "assert", listOf(condition, description))
}
else {
result = MethodCallExpression.build(null, "assert", listOf(condition), listOf(), false, LambdaExpression(null, StatementList(listOf(description))))
}
}
}
override fun visitBlockStatement(statement: PsiBlockStatement) {
@@ -248,6 +248,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
doTest("j2k/tests/testData/ast/assertStatement/withStringDetail.java");
}
@TestMetadata("withStringDetail2.java")
public void testWithStringDetail2() throws Exception {
doTest("j2k/tests/testData/ast/assertStatement/withStringDetail2.java");
}
}
@TestMetadata("j2k/tests/testData/ast/assignmentExpression")
@@ -1 +1 @@
assert(true) { "string details" }
assert(true, "string details")
@@ -0,0 +1,2 @@
//statement
assert true : "string details:" + x;
@@ -0,0 +1 @@
assert(true) { "string details:" + x }