Hack that adds more convenient defaults for J2K, minimal test for these defaults

This commit is contained in:
Pavel V. Talanov
2013-10-29 20:02:25 +04:00
parent a649eb52cf
commit e19373cac7
9 changed files with 109 additions and 15 deletions
@@ -24,9 +24,13 @@ public open class CallChainExpression(val expression: Expression, val identifier
public override fun toKotlin(): String {
if (!expression.isEmpty()) {
return expression.toKotlin() + (if (expression.isNullable()) "?." else ".") + identifier.toKotlin()
return expression.toKotlin() + (if (expression.isNullable() && !forceDotCall) "?." else ".") + identifier.toKotlin()
}
return identifier.toKotlin()
}
class object {
public var forceDotCall: Boolean = true
}
}
@@ -18,18 +18,23 @@ package org.jetbrains.jet.j2k.ast
import org.jetbrains.jet.j2k.ast.types.Type
public open class LocalVariable(val identifier: Identifier,
val modifiersSet: Set<Modifier>,
val `type`: Type,
val initializer: Expression) : Expression() {
public class LocalVariable(val identifier: Identifier,
val modifiersSet: Set<Modifier>,
val javaType: Type,
val initializer: Expression) : Expression() {
public open fun hasModifier(modifier: Modifier): Boolean = modifiersSet.contains(modifier)
public fun isImmutable(): Boolean = forceImmutable || modifiersSet.contains(Modifier.FINAL)
public override fun toKotlin(): String {
override fun toKotlin(): String {
if (initializer.isEmpty()) {
return identifier.toKotlin() + " : " + `type`.toKotlin()
return "${identifier.toKotlin()} : ${javaType.toKotlin()}"
}
return identifier.toKotlin() + " : " + `type`.toKotlin() + " = " + initializer.toKotlin()
return "${identifier.toKotlin()} ${if (specifyTypeExplicitly) ": ${javaType.toKotlin()} " else ""}= ${initializer.toKotlin()}"
}
class object {
public var specifyTypeExplicitly: Boolean = false
public var forceImmutable: Boolean = true
}
}
@@ -31,7 +31,7 @@ public open class DeclarationStatement(val elements: List<Element>) : Statement(
}
private fun convertDeclaration(v: LocalVariable): String {
val varKeyword: String? = (if (v.hasModifier(Modifier.FINAL))
val varKeyword: String? = (if (v.isImmutable())
"val"
else
"var")
@@ -25,9 +25,13 @@ public abstract class Type(val nullable: Boolean) : Element() {
}
public open fun isNullableStr(): String? {
return (if (nullable)
return (if (nullable && !forceNotNullTypes)
"?"
else
"")
}
class object {
public var forceNotNullTypes: Boolean = true
}
}
@@ -18,12 +18,15 @@ package org.jetbrains.jet.j2k.test;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.jetbrains.jet.j2k.ast.CallChainExpression;
import org.jetbrains.jet.j2k.ast.LocalVariable;
import org.jetbrains.jet.j2k.ast.types.Type;
import static org.jetbrains.jet.j2k.test.TestPackage.suiteForDirectory;
public class ConverterTestSuite {
public class BasicConverterTestSuite {
private ConverterTestSuite() {
private BasicConverterTestSuite() {
}
public static Test suite() {
@@ -32,6 +35,14 @@ public class ConverterTestSuite {
public Test createTest(String dataPath, String name) {
//noinspection JUnitTestCaseWithNoTests
return new StandaloneJavaToKotlinConverterTest(dataPath, name) {
@Override
protected void runTest() {
CallChainExpression.forceDotCall = false;
LocalVariable.specifyTypeExplicitly = true;
LocalVariable.forceImmutable = false;
Type.forceNotNullTypes = false;
super.runTest();
}
};
}
}));
@@ -0,0 +1,51 @@
/*
* 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.test;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.jetbrains.jet.j2k.ast.CallChainExpression;
import org.jetbrains.jet.j2k.ast.LocalVariable;
import org.jetbrains.jet.j2k.ast.types.Type;
import static org.jetbrains.jet.j2k.test.TestPackage.suiteForDirectory;
public class ConverterTestSuiteForPlugin {
private ConverterTestSuiteForPlugin() {
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(suiteForDirectory("j2k/tests/testData", "/plugin", new NamedTestFactory() {
public Test createTest(String dataPath, String name) {
//noinspection JUnitTestCaseWithNoTests
return new StandaloneJavaToKotlinConverterTest(dataPath, name) {
@Override
protected void runTest() {
CallChainExpression.forceDotCall = true;
LocalVariable.specifyTypeExplicitly = false;
LocalVariable.forceImmutable = true;
Type.forceNotNullTypes = true;
super.runTest();
}
};
}
}));
return suite;
}
}
@@ -94,8 +94,8 @@ public abstract class StandaloneJavaToKotlinConverterTest(val dataPath: String,
}
private fun expressionToKotlin(converter: Converter, code: String?): String {
var result = statementToKotlin(converter, "Object o =" + code + "}")
result = result.replaceFirst("var o : Any\\? =", "")
var result = statementToKotlin(converter, "final Object o =" + code + "}")
result = result.replaceFirst("val o : Any\\? =", "").replaceFirst("val o : Any = ", "")
return prettify(result)
}
@@ -0,0 +1,10 @@
class Example {
void main(Integer param1, int param2) {
Library.getString().isEmpty();
Integer i = 10;
for (Integer a : Arrays.asList(1, 2, 3)) {
}
}
}
@@ -0,0 +1,9 @@
open class Example() {
open fun main(param1 : Int, param2 : Int) : Unit {
Library.getString().isEmpty()
val i = 10
for (a in Arrays.asList(1, 2, 3))
{
}
}
}