Java to Kotlin converter: explicit locals type option is used for for variable too
This commit is contained in:
@@ -88,9 +88,9 @@ class DoWhileStatement(val condition: Expression, val body: Element, singleLine:
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: explicit type (if option)
|
||||
class ForeachStatement(
|
||||
val variableName: Identifier,
|
||||
val explicitVariableType: Type?,
|
||||
val collection: Expression,
|
||||
val body: Element,
|
||||
singleLine: Boolean
|
||||
@@ -99,7 +99,11 @@ class ForeachStatement(
|
||||
private val br = if (singleLine) " " else "\n"
|
||||
|
||||
override fun generateCode(builder: CodeBuilder) {
|
||||
builder append "for (" append variableName append " in " append collection append ")" append br append body
|
||||
builder append "for (" append variableName
|
||||
if (explicitVariableType != null) {
|
||||
builder append ":" append explicitVariableType
|
||||
}
|
||||
builder append " in " append collection append ")" append br append body
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -122,7 +122,8 @@ open class StatementVisitor(public val converter: Converter) : JavaElementVisito
|
||||
&& update.getChildren().size == 1
|
||||
&& update.getChildren().single().isPlusPlusExpression()) {
|
||||
val range = forIterationRange(start, upperBound, operationTokenType).assignNoPrototype()
|
||||
result = ForeachStatement(loopVar.declarationIdentifier(), range, convertStatementOrBlock(body), statement.isInSingleLine())
|
||||
val explicitType = if (converter.settings.specifyLocalVariableTypeByDefault) PrimitiveType(Identifier("Int").assignNoPrototype()).assignNoPrototype() else null
|
||||
result = ForeachStatement(loopVar.declarationIdentifier(), explicitType, range, convertStatementOrBlock(body), statement.isInSingleLine())
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -212,7 +213,9 @@ open class StatementVisitor(public val converter: Converter) : JavaElementVisito
|
||||
override fun visitForeachStatement(statement: PsiForeachStatement) {
|
||||
val iteratorExpr = converter.convertExpression(statement.getIteratedValue())
|
||||
val iterator = if (iteratorExpr.isNullable) BangBangExpression(iteratorExpr).assignNoPrototype() else iteratorExpr
|
||||
result = ForeachStatement(statement.getIterationParameter().declarationIdentifier(),
|
||||
val iterationParameter = statement.getIterationParameter()
|
||||
result = ForeachStatement(iterationParameter.declarationIdentifier(),
|
||||
if (converter.settings.specifyLocalVariableTypeByDefault) converter.typeConverter.convertVariableType(iterationParameter) else null,
|
||||
iterator,
|
||||
convertStatementOrBlock(statement.getBody()),
|
||||
statement.isInSingleLine())
|
||||
|
||||
@@ -2564,6 +2564,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
|
||||
doTest("j2k/tests/testData/ast/settings/specifyLocalVariableTypeByDefault.java");
|
||||
}
|
||||
|
||||
@TestMetadata("specifyLocalVariableTypeByDefaultInFor.java")
|
||||
public void testSpecifyLocalVariableTypeByDefaultInFor() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/settings/specifyLocalVariableTypeByDefaultInFor.java");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("j2k/tests/testData/ast/starProjectionType")
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
//method
|
||||
// !specifyLocalVariableTypeByDefault: true
|
||||
public void foo(List<String> list) {
|
||||
int[] array = new int[10];
|
||||
for (int i = 0; i < 10; i++){
|
||||
array[i] = i;
|
||||
}
|
||||
|
||||
for(String s : list) System.out.print(s);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// !specifyLocalVariableTypeByDefault: true
|
||||
public fun foo(list: List<String>) {
|
||||
val array: IntArray = IntArray(10)
|
||||
for (i: Int in 0..10 - 1) {
|
||||
array[i] = i
|
||||
}
|
||||
|
||||
for (s: String in list) System.out.print(s)
|
||||
}
|
||||
Reference in New Issue
Block a user