access modifiers support
This commit is contained in:
@@ -67,7 +67,7 @@ public class Converter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Field fieldToField(PsiField field) {
|
private static Field fieldToField(PsiField field) {
|
||||||
HashSet<String> modifiers = getModifiersSet(field.getModifierList());
|
HashSet<String> modifiers = modifiersListToModifiersSet(field.getModifierList());
|
||||||
if (field instanceof PsiEnumConstant) // TODO: remove instanceof
|
if (field instanceof PsiEnumConstant) // TODO: remove instanceof
|
||||||
return new EnumConstant(
|
return new EnumConstant(
|
||||||
new IdentifierImpl(field.getName()), // TODO
|
new IdentifierImpl(field.getName()), // TODO
|
||||||
@@ -95,6 +95,7 @@ public class Converter {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private static Function methodToFunction(PsiMethod method, boolean notEmpty) {
|
private static Function methodToFunction(PsiMethod method, boolean notEmpty) {
|
||||||
final IdentifierImpl identifier = new IdentifierImpl(method.getName());
|
final IdentifierImpl identifier = new IdentifierImpl(method.getName());
|
||||||
|
final HashSet<String> modifiers = modifiersListToModifiersSet(method.getModifierList());
|
||||||
final Type type = typeToType(method.getReturnType());
|
final Type type = typeToType(method.getReturnType());
|
||||||
final Block body = blockToBlock(method.getBody(), notEmpty);
|
final Block body = blockToBlock(method.getBody(), notEmpty);
|
||||||
final Element params = elementToElement(method.getParameterList());
|
final Element params = elementToElement(method.getParameterList());
|
||||||
@@ -103,6 +104,7 @@ public class Converter {
|
|||||||
if (method.isConstructor())
|
if (method.isConstructor())
|
||||||
return new Constructor(
|
return new Constructor(
|
||||||
identifier,
|
identifier,
|
||||||
|
modifiers,
|
||||||
type,
|
type,
|
||||||
typeParameters,
|
typeParameters,
|
||||||
params,
|
params,
|
||||||
@@ -110,6 +112,7 @@ public class Converter {
|
|||||||
);
|
);
|
||||||
return new Function(
|
return new Function(
|
||||||
identifier,
|
identifier,
|
||||||
|
modifiers,
|
||||||
type,
|
type,
|
||||||
typeParameters,
|
typeParameters,
|
||||||
params,
|
params,
|
||||||
@@ -259,10 +262,14 @@ public class Converter {
|
|||||||
return new IdentifierImpl(identifier.getText());
|
return new IdentifierImpl(identifier.getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HashSet<String> getModifiersSet(PsiModifierList modifierList) {
|
public static HashSet<String> modifiersListToModifiersSet(PsiModifierList modifierList) {
|
||||||
HashSet<String> modifiersSet = new HashSet<String>();
|
HashSet<String> modifiersSet = new HashSet<String>();
|
||||||
if (modifierList != null) {
|
if (modifierList != null) {
|
||||||
if (modifierList.hasModifierProperty("final")) modifiersSet.add(Modifier.FINAL);
|
if (modifierList.hasModifierProperty(PsiModifier.FINAL)) modifiersSet.add(Modifier.FINAL);
|
||||||
|
if (modifierList.hasModifierProperty(PsiModifier.PUBLIC)) modifiersSet.add(Modifier.PUBLIC);
|
||||||
|
if (modifierList.hasModifierProperty(PsiModifier.PROTECTED)) modifiersSet.add(Modifier.PROTECTED);
|
||||||
|
if (modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) modifiersSet.add(Modifier.INTERNAL);
|
||||||
|
if (modifierList.hasModifierProperty(PsiModifier.PRIVATE)) modifiersSet.add(Modifier.PRIVATE);
|
||||||
}
|
}
|
||||||
return modifiersSet;
|
return modifiersSet;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,13 +3,14 @@ package org.jetbrains.jet.j2k.ast;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ignatov
|
* @author ignatov
|
||||||
*/
|
*/
|
||||||
public class Constructor extends Function {
|
public class Constructor extends Function {
|
||||||
public Constructor(Identifier identifier, Type type, List<Element> typeParameters, Element params, Block block) {
|
public Constructor(Identifier identifier, Set<String> modifiers, Type type, List<Element> typeParameters, Element params, Block block) {
|
||||||
super(identifier, type, typeParameters, params, block);
|
super(identifier, modifiers, type, typeParameters, params, block);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String primary() {
|
public String primary() {
|
||||||
|
|||||||
@@ -5,19 +5,22 @@ import org.jetbrains.jet.j2k.util.AstUtil;
|
|||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ignatov
|
* @author ignatov
|
||||||
*/
|
*/
|
||||||
public class Function extends Node {
|
public class Function extends Node {
|
||||||
private final Identifier myName;
|
private final Identifier myName;
|
||||||
|
private Set<String> myModifiers;
|
||||||
private final Type myType;
|
private final Type myType;
|
||||||
private final List<Element> myTypeParameters;
|
private final List<Element> myTypeParameters;
|
||||||
final Element myParams;
|
final Element myParams;
|
||||||
private final Block myBlock;
|
private final Block myBlock;
|
||||||
|
|
||||||
public Function(Identifier name, Type type, List<Element> typeParameters, Element params, Block block) {
|
public Function(Identifier name, Set<String> modifiers, Type type, List<Element> typeParameters, Element params, Block block) {
|
||||||
myName = name;
|
myName = name;
|
||||||
|
myModifiers = modifiers;
|
||||||
myType = type;
|
myType = type;
|
||||||
myTypeParameters = typeParameters;
|
myTypeParameters = typeParameters;
|
||||||
myParams = params;
|
myParams = params;
|
||||||
@@ -35,7 +38,14 @@ public class Function extends Node {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
String typeParameterWhereToKotlin() {
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
private String typeParameterWhereToKotlin() {
|
||||||
if (hasWhere()) {
|
if (hasWhere()) {
|
||||||
List<String> wheres = new LinkedList<String>();
|
List<String> wheres = new LinkedList<String>();
|
||||||
for (Element t : myTypeParameters)
|
for (Element t : myTypeParameters)
|
||||||
@@ -46,10 +56,21 @@ public class Function extends Node {
|
|||||||
return EMPTY;
|
return EMPTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String modifiersToKotlin() {
|
||||||
|
List<String> modifierList = new LinkedList<String>();
|
||||||
|
|
||||||
|
modifierList.add(accessModifier());
|
||||||
|
|
||||||
|
if (modifierList.size() > 0)
|
||||||
|
return AstUtil.join(modifierList, SPACE) + SPACE;
|
||||||
|
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String toKotlin() {
|
public String toKotlin() {
|
||||||
return "fun" + SPACE + myName.toKotlin() + typeParametersToKotlin() + "(" + myParams.toKotlin() + ")" + SPACE + COLON +
|
return modifiersToKotlin() + "fun" + SPACE + myName.toKotlin() + typeParametersToKotlin() + "(" + myParams.toKotlin() + ")" + SPACE + COLON +
|
||||||
SPACE + myType.toKotlin() + SPACE +
|
SPACE + myType.toKotlin() + SPACE +
|
||||||
typeParameterWhereToKotlin() +
|
typeParameterWhereToKotlin() +
|
||||||
myBlock.toKotlin();
|
myBlock.toKotlin();
|
||||||
|
|||||||
@@ -11,4 +11,5 @@ public abstract class Modifier {
|
|||||||
public static String STATIC = "static";
|
public static String STATIC = "static";
|
||||||
public static String ABSTRACT = "abstract";
|
public static String ABSTRACT = "abstract";
|
||||||
public static String FINAL = "final";
|
public static String FINAL = "final";
|
||||||
|
public static String OPEN = "open";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ 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 java.util.HashSet;
|
|
||||||
|
|
||||||
import static org.jetbrains.jet.j2k.Converter.*;
|
import static org.jetbrains.jet.j2k.Converter.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,7 +25,7 @@ public class ElementVisitor extends JavaElementVisitor implements Visitor {
|
|||||||
|
|
||||||
myResult = new LocalVariable(
|
myResult = new LocalVariable(
|
||||||
new IdentifierImpl(variable.getName()), // TODO
|
new IdentifierImpl(variable.getName()), // TODO
|
||||||
getModifiersSet(variable.getModifierList()),
|
modifiersListToModifiersSet(variable.getModifierList()),
|
||||||
typeToType(variable.getType()),
|
typeToType(variable.getType()),
|
||||||
expressionToExpression(variable.getInitializer())
|
expressionToExpression(variable.getInitializer())
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ public class EnumTest extends JetTestCaseBase {
|
|||||||
"\n" +
|
"\n" +
|
||||||
" private int code;\n" +
|
" private int code;\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
" private Color(int c) {\n" +
|
" private Color(int c) {\n" + // TODO: private constructor, WTF?
|
||||||
" code = c;\n" +
|
" code = c;\n" +
|
||||||
" }\n" +
|
" }\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
@@ -70,7 +70,7 @@ public class EnumTest extends JetTestCaseBase {
|
|||||||
"YELLOW(24)\n" +
|
"YELLOW(24)\n" +
|
||||||
"BLUE(25)\n" +
|
"BLUE(25)\n" +
|
||||||
"var code : Int\n" +
|
"var code : Int\n" +
|
||||||
"fun getCode() : Int {\n" +
|
"public fun getCode() : Int {\n" +
|
||||||
"return code\n" +
|
"return code\n" +
|
||||||
"}\n" +
|
"}\n" +
|
||||||
"}"
|
"}"
|
||||||
|
|||||||
@@ -62,4 +62,20 @@ public class FunctionTest extends JetTestCaseBase {
|
|||||||
"fun putUVW<U, V, W>(u : U?, v : V?, w : W?) : Unit { }"
|
"fun putUVW<U, V, W>(u : U?, v : V?, w : W?) : Unit { }"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testInternal() throws Exception {
|
||||||
|
Assert.assertEquals(methodToSingleLineKotlin("void test() {}"), "fun test() : Unit { }");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testPrivate() throws Exception {
|
||||||
|
Assert.assertEquals(methodToSingleLineKotlin("private void test() {}"), "private fun test() : Unit { }");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testProtected() throws Exception {
|
||||||
|
Assert.assertEquals(methodToSingleLineKotlin("protected void test() {}"), "protected fun test() : Unit { }");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testPublic() throws Exception {
|
||||||
|
Assert.assertEquals(methodToSingleLineKotlin("public void test() {}"), "public fun test() : Unit { }");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -22,8 +22,8 @@ public class TraitTest extends JetTestCaseBase {
|
|||||||
"}"
|
"}"
|
||||||
),
|
),
|
||||||
"trait INode {\n" +
|
"trait INode {\n" +
|
||||||
"fun getTag() : Tag? \n" +
|
"public fun getTag() : Tag? \n" +
|
||||||
"fun toKotlin() : String? \n" +
|
"public fun toKotlin() : String? \n" +
|
||||||
"}");
|
"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,9 +36,9 @@ public class TraitTest extends JetTestCaseBase {
|
|||||||
"}"
|
"}"
|
||||||
),
|
),
|
||||||
"trait INode {\n" +
|
"trait INode {\n" +
|
||||||
"var IN : String? = \"in\"\n" +
|
"val IN : String? = \"in\"\n" +
|
||||||
"var AT : String? = \"@\"\n" +
|
"val AT : String? = \"@\"\n" +
|
||||||
"var COMMA_WITH_SPACE : String? = (COMMA + SPACE)\n" +
|
"val COMMA_WITH_SPACE : String? = (COMMA + SPACE)\n" +
|
||||||
"}"
|
"}"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user