access modifiers support

This commit is contained in:
Sergey Ignatov
2011-11-03 14:04:33 +04:00
parent bbd282e57b
commit b727c18852
8 changed files with 62 additions and 18 deletions
+10 -3
View File
@@ -67,7 +67,7 @@ public class Converter {
}
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
return new EnumConstant(
new IdentifierImpl(field.getName()), // TODO
@@ -95,6 +95,7 @@ public class Converter {
@NotNull
private static Function methodToFunction(PsiMethod method, boolean notEmpty) {
final IdentifierImpl identifier = new IdentifierImpl(method.getName());
final HashSet<String> modifiers = modifiersListToModifiersSet(method.getModifierList());
final Type type = typeToType(method.getReturnType());
final Block body = blockToBlock(method.getBody(), notEmpty);
final Element params = elementToElement(method.getParameterList());
@@ -103,6 +104,7 @@ public class Converter {
if (method.isConstructor())
return new Constructor(
identifier,
modifiers,
type,
typeParameters,
params,
@@ -110,6 +112,7 @@ public class Converter {
);
return new Function(
identifier,
modifiers,
type,
typeParameters,
params,
@@ -259,10 +262,14 @@ public class Converter {
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>();
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;
}
@@ -3,13 +3,14 @@ package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Set;
/**
* @author ignatov
*/
public class Constructor extends Function {
public Constructor(Identifier identifier, Type type, List<Element> typeParameters, Element params, Block block) {
super(identifier, type, typeParameters, params, block);
public Constructor(Identifier identifier, Set<String> modifiers, Type type, List<Element> typeParameters, Element params, Block block) {
super(identifier, modifiers, type, typeParameters, params, block);
}
public String primary() {
+24 -3
View File
@@ -5,19 +5,22 @@ import org.jetbrains.jet.j2k.util.AstUtil;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
* @author ignatov
*/
public class Function extends Node {
private final Identifier myName;
private Set<String> myModifiers;
private final Type myType;
private final List<Element> myTypeParameters;
final Element myParams;
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;
myModifiers = modifiers;
myType = type;
myTypeParameters = typeParameters;
myParams = params;
@@ -35,7 +38,14 @@ public class Function extends Node {
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()) {
List<String> wheres = new LinkedList<String>();
for (Element t : myTypeParameters)
@@ -46,10 +56,21 @@ public class Function extends Node {
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
@Override
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 +
typeParameterWhereToKotlin() +
myBlock.toKotlin();
@@ -11,4 +11,5 @@ public abstract class Modifier {
public static String STATIC = "static";
public static String ABSTRACT = "abstract";
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.ast.*;
import java.util.HashSet;
import static org.jetbrains.jet.j2k.Converter.*;
/**
@@ -27,7 +25,7 @@ public class ElementVisitor extends JavaElementVisitor implements Visitor {
myResult = new LocalVariable(
new IdentifierImpl(variable.getName()), // TODO
getModifiersSet(variable.getModifierList()),
modifiersListToModifiersSet(variable.getModifierList()),
typeToType(variable.getType()),
expressionToExpression(variable.getInitializer())
);
+2 -2
View File
@@ -56,7 +56,7 @@ public class EnumTest extends JetTestCaseBase {
"\n" +
" private int code;\n" +
"\n" +
" private Color(int c) {\n" +
" private Color(int c) {\n" + // TODO: private constructor, WTF?
" code = c;\n" +
" }\n" +
"\n" +
@@ -70,7 +70,7 @@ public class EnumTest extends JetTestCaseBase {
"YELLOW(24)\n" +
"BLUE(25)\n" +
"var code : Int\n" +
"fun getCode() : Int {\n" +
"public fun getCode() : Int {\n" +
"return code\n" +
"}\n" +
"}"
@@ -62,4 +62,20 @@ public class FunctionTest extends JetTestCaseBase {
"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" +
"fun getTag() : Tag? \n" +
"fun toKotlin() : String? \n" +
"public fun getTag() : Tag? \n" +
"public fun toKotlin() : String? \n" +
"}");
}
@@ -36,9 +36,9 @@ public class TraitTest extends JetTestCaseBase {
"}"
),
"trait INode {\n" +
"var IN : String? = \"in\"\n" +
"var AT : String? = \"@\"\n" +
"var COMMA_WITH_SPACE : String? = (COMMA + SPACE)\n" +
"val IN : String? = \"in\"\n" +
"val AT : String? = \"@\"\n" +
"val COMMA_WITH_SPACE : String? = (COMMA + SPACE)\n" +
"}"
);
}