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) {
|
private List<String> toStringList(List<Element> elements) {
|
||||||
List<String> result = new LinkedList<String>();
|
List<String> result = new LinkedList<String>();
|
||||||
for (String n : AstUtil.nodesToKotlin(elements))
|
for (Element e : elements) {
|
||||||
result.add(
|
if (e instanceof LocalVariable) {
|
||||||
"var" + SPACE + n
|
LocalVariable v = (LocalVariable) e;
|
||||||
);
|
|
||||||
|
final String varKeyword = v.hasModifier(Modifier.FINAL) ? "val" : "var";
|
||||||
|
result.add(
|
||||||
|
varKeyword + SPACE + e.toKotlin()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,20 +2,28 @@ package org.jetbrains.jet.j2k.ast;
|
|||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ignatov
|
* @author ignatov
|
||||||
*/
|
*/
|
||||||
public class LocalVariable extends Expression {
|
public class LocalVariable extends Expression {
|
||||||
private final Identifier myIdentifier;
|
private final Identifier myIdentifier;
|
||||||
|
private HashSet<String> myModifiersSet;
|
||||||
private final Type myType;
|
private final Type myType;
|
||||||
private final Expression myInitializer;
|
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;
|
myIdentifier = identifier;
|
||||||
|
myModifiersSet = modifiersSet;
|
||||||
myType = type;
|
myType = type;
|
||||||
myInitializer = initializer;
|
myInitializer = initializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasModifier(String modifier) {
|
||||||
|
return myModifiersSet.contains(modifier);
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String toKotlin() {
|
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.annotations.NotNull;
|
||||||
import org.jetbrains.jet.j2k.Converter;
|
import org.jetbrains.jet.j2k.Converter;
|
||||||
import org.jetbrains.jet.j2k.ast.*;
|
import org.jetbrains.jet.j2k.ast.*;
|
||||||
|
import org.jetbrains.jet.j2k.ast.Modifier;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
import static org.jetbrains.jet.j2k.Converter.*;
|
import static org.jetbrains.jet.j2k.Converter.*;
|
||||||
|
|
||||||
@@ -22,8 +25,18 @@ public class ElementVisitor extends JavaElementVisitor implements Visitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visitLocalVariable(PsiLocalVariable variable) {
|
public void visitLocalVariable(PsiLocalVariable variable) {
|
||||||
super.visitLocalVariable(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(
|
myResult = new LocalVariable(
|
||||||
new IdentifierImpl(variable.getName()), // TODO
|
new IdentifierImpl(variable.getName()), // TODO
|
||||||
|
modifiersSet,
|
||||||
typeToType(variable.getType()),
|
typeToType(variable.getType()),
|
||||||
expressionToExpression(variable.getInitializer())
|
expressionToExpression(variable.getInitializer())
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package org.jetbrains.jet.j2k.visitors;
|
|||||||
|
|
||||||
import com.intellij.psi.*;
|
import com.intellij.psi.*;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.j2k.Converter;
|
|
||||||
import org.jetbrains.jet.j2k.ast.*;
|
import org.jetbrains.jet.j2k.ast.*;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -36,7 +35,7 @@ public class StatementVisitor extends ElementVisitor implements Visitor {
|
|||||||
public void visitBlockStatement(PsiBlockStatement statement) {
|
public void visitBlockStatement(PsiBlockStatement statement) {
|
||||||
super.visitBlockStatement(statement);
|
super.visitBlockStatement(statement);
|
||||||
myResult = new Block(
|
myResult = new Block(
|
||||||
Converter.statementsToStatementList(statement.getCodeBlock().getStatements()),
|
statementsToStatementList(statement.getCodeBlock().getStatements()),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -67,7 +66,7 @@ public class StatementVisitor extends ElementVisitor implements Visitor {
|
|||||||
public void visitDeclarationStatement(PsiDeclarationStatement statement) {
|
public void visitDeclarationStatement(PsiDeclarationStatement statement) {
|
||||||
super.visitDeclarationStatement(statement);
|
super.visitDeclarationStatement(statement);
|
||||||
myResult = new DeclarationStatement(
|
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