statement with final modifier converted to val
This commit is contained in:
@@ -18,10 +18,16 @@ public class DeclarationStatement extends Statement {
|
||||
|
||||
private List<String> toStringList(List<Element> elements) {
|
||||
List<String> result = new LinkedList<String>();
|
||||
for (String n : AstUtil.nodesToKotlin(elements))
|
||||
result.add(
|
||||
"var" + SPACE + n
|
||||
);
|
||||
for (Element e : elements) {
|
||||
if (e instanceof LocalVariable) {
|
||||
LocalVariable v = (LocalVariable) e;
|
||||
|
||||
final String varKeyword = v.hasModifier(Modifier.FINAL) ? "val" : "var";
|
||||
result.add(
|
||||
varKeyword + SPACE + e.toKotlin()
|
||||
);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,20 +2,28 @@ package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class LocalVariable extends Expression {
|
||||
private final Identifier myIdentifier;
|
||||
private HashSet<String> myModifiersSet;
|
||||
private final Type myType;
|
||||
private final Expression myInitializer;
|
||||
|
||||
public LocalVariable(Identifier identifier, Type type, Expression initializer) {
|
||||
public LocalVariable(Identifier identifier, HashSet<String> modifiersSet, Type type, Expression initializer) {
|
||||
myIdentifier = identifier;
|
||||
myModifiersSet = modifiersSet;
|
||||
myType = type;
|
||||
myInitializer = initializer;
|
||||
}
|
||||
|
||||
public boolean hasModifier(String modifier) {
|
||||
return myModifiersSet.contains(modifier);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String toKotlin() {
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public abstract class Modifier {
|
||||
public static String PUBLIC = "public";
|
||||
public static String PROTECTED = "protected";
|
||||
public static String PRIVATE = "private";
|
||||
public static String INTERNAL = "internal";
|
||||
public static String STATIC = "static";
|
||||
public static String ABSTRACT = "abstract";
|
||||
public static String FINAL = "final";
|
||||
}
|
||||
@@ -4,6 +4,9 @@ import com.intellij.psi.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.j2k.Converter;
|
||||
import org.jetbrains.jet.j2k.ast.*;
|
||||
import org.jetbrains.jet.j2k.ast.Modifier;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
import static org.jetbrains.jet.j2k.Converter.*;
|
||||
|
||||
@@ -22,8 +25,18 @@ public class ElementVisitor extends JavaElementVisitor implements Visitor {
|
||||
@Override
|
||||
public void visitLocalVariable(PsiLocalVariable variable) {
|
||||
super.visitLocalVariable(variable);
|
||||
final PsiModifierList modifierList = variable.getModifierList();
|
||||
|
||||
HashSet<String> modifiersSet = new HashSet<String>();
|
||||
|
||||
if (modifierList != null) {
|
||||
if (modifierList.hasModifierProperty("final"))
|
||||
modifiersSet.add(Modifier.FINAL);
|
||||
}
|
||||
|
||||
myResult = new LocalVariable(
|
||||
new IdentifierImpl(variable.getName()), // TODO
|
||||
modifiersSet,
|
||||
typeToType(variable.getType()),
|
||||
expressionToExpression(variable.getInitializer())
|
||||
);
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.jetbrains.jet.j2k.visitors;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.j2k.Converter;
|
||||
import org.jetbrains.jet.j2k.ast.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -36,7 +35,7 @@ public class StatementVisitor extends ElementVisitor implements Visitor {
|
||||
public void visitBlockStatement(PsiBlockStatement statement) {
|
||||
super.visitBlockStatement(statement);
|
||||
myResult = new Block(
|
||||
Converter.statementsToStatementList(statement.getCodeBlock().getStatements()),
|
||||
statementsToStatementList(statement.getCodeBlock().getStatements()),
|
||||
true
|
||||
);
|
||||
}
|
||||
@@ -67,7 +66,7 @@ public class StatementVisitor extends ElementVisitor implements Visitor {
|
||||
public void visitDeclarationStatement(PsiDeclarationStatement statement) {
|
||||
super.visitDeclarationStatement(statement);
|
||||
myResult = new DeclarationStatement(
|
||||
Converter.elementsToElementList(statement.getDeclaredElements())
|
||||
elementsToElementList(statement.getDeclaredElements())
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -30,4 +30,26 @@ public class DeclarationStatementTest extends JetTestCaseBase {
|
||||
);
|
||||
}
|
||||
|
||||
public void testSingleFinalStringDeclaration() throws Exception {
|
||||
Assert.assertEquals(
|
||||
statementToSingleLineKotlin("final String s;"),
|
||||
"val s : String?"
|
||||
);
|
||||
}
|
||||
|
||||
public void testSingleFinalIntDeclaration() throws Exception {
|
||||
Assert.assertEquals(
|
||||
statementToSingleLineKotlin("final int s;"),
|
||||
"val s : Int"
|
||||
);
|
||||
}
|
||||
|
||||
public void testMultiplyFinalIntDeclaration() throws Exception {
|
||||
Assert.assertEquals(
|
||||
statementToKotlin("final int k, l, m;"),
|
||||
"val k : Int\n" +
|
||||
"val l : Int\n" +
|
||||
"val m : Int"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user