fields with final modifier converted to val
This commit is contained in:
@@ -6,11 +6,13 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.j2k.ast.*;
|
||||
import org.jetbrains.jet.j2k.ast.Class;
|
||||
import org.jetbrains.jet.j2k.ast.Enum;
|
||||
import org.jetbrains.jet.j2k.ast.Modifier;
|
||||
import org.jetbrains.jet.j2k.visitors.ElementVisitor;
|
||||
import org.jetbrains.jet.j2k.visitors.ExpressionVisitor;
|
||||
import org.jetbrains.jet.j2k.visitors.StatementVisitor;
|
||||
import org.jetbrains.jet.j2k.visitors.TypeVisitor;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -65,14 +67,17 @@ public class Converter {
|
||||
}
|
||||
|
||||
private static Field fieldToField(PsiField field) {
|
||||
HashSet<String> modifiers = getModifiersSet(field.getModifierList());
|
||||
if (field instanceof PsiEnumConstant) // TODO: remove instanceof
|
||||
return new EnumConstant(
|
||||
new IdentifierImpl(field.getName()), // TODO
|
||||
modifiers,
|
||||
typeToType(field.getType()),
|
||||
elementToElement(((PsiEnumConstant) field).getArgumentList())
|
||||
);
|
||||
return new Field(
|
||||
new IdentifierImpl(field.getName()), // TODO
|
||||
modifiers,
|
||||
typeToType(field.getType()),
|
||||
expressionToExpression(field.getInitializer()) // TODO: add modifiers
|
||||
);
|
||||
@@ -253,4 +258,12 @@ public class Converter {
|
||||
return Identifier.EMPTY_IDENTIFIER;
|
||||
return new IdentifierImpl(identifier.getText());
|
||||
}
|
||||
|
||||
public static HashSet<String> getModifiersSet(PsiModifierList modifierList) {
|
||||
HashSet<String> modifiersSet = new HashSet<String>();
|
||||
if (modifierList != null) {
|
||||
if (modifierList.hasModifierProperty("final")) modifiersSet.add(Modifier.FINAL);
|
||||
}
|
||||
return modifiersSet;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,14 @@ package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class EnumConstant extends Field {
|
||||
public EnumConstant(Identifier identifier, Type type, Element params) {
|
||||
super(identifier, type, params);
|
||||
public EnumConstant(Identifier identifier, HashSet<String> modifiers, Type type, Element params) {
|
||||
super(identifier, modifiers, type, params);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -2,16 +2,20 @@ package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class Field extends Node {
|
||||
final Identifier myIdentifier;
|
||||
private HashSet<String> myModifiers;
|
||||
private final Type myType;
|
||||
final Element myInitializer;
|
||||
|
||||
public Field(Identifier identifier, Type type, Element initializer) {
|
||||
public Field(Identifier identifier, HashSet<String> modifiers, Type type, Element initializer) {
|
||||
myIdentifier = identifier;
|
||||
myModifiers = modifiers;
|
||||
myType = type;
|
||||
myInitializer = initializer;
|
||||
}
|
||||
@@ -19,7 +23,7 @@ public class Field extends Node {
|
||||
@NotNull
|
||||
@Override
|
||||
public String toKotlin() {
|
||||
String modifier = "var" + SPACE;
|
||||
String modifier = (myModifiers.contains(Modifier.FINAL) ? "val" : "var") + SPACE;
|
||||
|
||||
if (myInitializer.toKotlin().isEmpty()) // TODO: remove
|
||||
return modifier + myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin();
|
||||
|
||||
@@ -4,7 +4,6 @@ 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;
|
||||
|
||||
@@ -25,18 +24,10 @@ 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,
|
||||
getModifiersSet(variable.getModifierList()),
|
||||
typeToType(variable.getType()),
|
||||
expressionToExpression(variable.getInitializer())
|
||||
);
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.jetbrains.jet.j2k.ast;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.jet.j2k.JetTestCaseBase;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class FieldTest extends JetTestCaseBase {
|
||||
public void testVarWithoutInit() throws Exception {
|
||||
Assert.assertEquals(
|
||||
methodToSingleLineKotlin("Foo f;"),
|
||||
"var f : Foo?"
|
||||
);
|
||||
}
|
||||
|
||||
public void testVarWithInit() throws Exception {
|
||||
Assert.assertEquals(
|
||||
methodToSingleLineKotlin("Foo f = new Foo(1, 2);"),
|
||||
"var f : Foo? = Foo(1, 2)"
|
||||
);
|
||||
}
|
||||
|
||||
public void testValWithInit() throws Exception {
|
||||
Assert.assertEquals(
|
||||
methodToSingleLineKotlin("final Foo f = new Foo(1, 2);"),
|
||||
"val f : Foo? = Foo(1, 2)"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user