access modifiers support for fields
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
package org.jetbrains.jet.j2k.ast;
|
package org.jetbrains.jet.j2k.ast;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.j2k.util.AstUtil;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,10 +23,30 @@ public class Field extends Node {
|
|||||||
myInitializer = initializer;
|
myInitializer = initializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String accessModifier() {
|
||||||
|
for (String m : myModifiers)
|
||||||
|
if (m.equals(Modifier.PUBLIC) || m.equals(Modifier.PROTECTED) || m.equals(Modifier.PRIVATE))
|
||||||
|
return m;
|
||||||
|
return EMPTY; // package local converted to internal, but we use internal by default
|
||||||
|
}
|
||||||
|
|
||||||
|
String modifiersToKotlin() {
|
||||||
|
List<String> modifierList = new LinkedList<String>();
|
||||||
|
|
||||||
|
modifierList.add(accessModifier());
|
||||||
|
|
||||||
|
modifierList.add(myModifiers.contains(Modifier.FINAL) ? "val" : "var");
|
||||||
|
|
||||||
|
if (modifierList.size() > 0)
|
||||||
|
return AstUtil.join(modifierList, SPACE) + SPACE;
|
||||||
|
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String toKotlin() {
|
public String toKotlin() {
|
||||||
String modifier = (myModifiers.contains(Modifier.FINAL) ? "val" : "var") + SPACE;
|
String modifier = modifiersToKotlin();
|
||||||
|
|
||||||
if (myInitializer.toKotlin().isEmpty()) // TODO: remove
|
if (myInitializer.toKotlin().isEmpty()) // TODO: remove
|
||||||
return modifier + myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin();
|
return modifier + myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin();
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ public class EnumTest extends JetTestCaseBase {
|
|||||||
"RED(23)\n" +
|
"RED(23)\n" +
|
||||||
"YELLOW(24)\n" +
|
"YELLOW(24)\n" +
|
||||||
"BLUE(25)\n" +
|
"BLUE(25)\n" +
|
||||||
"var code : Int\n" +
|
"private var code : Int\n" +
|
||||||
"public fun getCode() : Int {\n" +
|
"public fun getCode() : Int {\n" +
|
||||||
"return code\n" +
|
"return code\n" +
|
||||||
"}\n" +
|
"}\n" +
|
||||||
|
|||||||
@@ -27,4 +27,20 @@ public class FieldTest extends JetTestCaseBase {
|
|||||||
"val f : Foo? = Foo(1, 2)"
|
"val f : Foo? = Foo(1, 2)"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testPrivateField() throws Exception {
|
||||||
|
Assert.assertEquals(methodToSingleLineKotlin("private Foo f;"), "private var f : Foo?");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testProtectedField() throws Exception {
|
||||||
|
Assert.assertEquals(methodToSingleLineKotlin("protected Foo f;"), "protected var f : Foo?");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testPublicField() throws Exception {
|
||||||
|
Assert.assertEquals(methodToSingleLineKotlin("public Foo f;"), "public var f : Foo?");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testInternalField() throws Exception {
|
||||||
|
Assert.assertEquals(methodToSingleLineKotlin("Foo f;"), "var f : Foo?");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,9 +36,9 @@ public class TraitTest extends JetTestCaseBase {
|
|||||||
"}"
|
"}"
|
||||||
),
|
),
|
||||||
"trait INode {\n" +
|
"trait INode {\n" +
|
||||||
"val IN : String? = \"in\"\n" +
|
"public val IN : String? = \"in\"\n" +
|
||||||
"val AT : String? = \"@\"\n" +
|
"public val AT : String? = \"@\"\n" +
|
||||||
"val COMMA_WITH_SPACE : String? = (COMMA + SPACE)\n" +
|
"public val COMMA_WITH_SPACE : String? = (COMMA + SPACE)\n" +
|
||||||
"}"
|
"}"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user