Parameter added, constructor for enum supported

This commit is contained in:
Sergey Ignatov
2011-10-27 19:11:06 +04:00
parent 29a353bcc0
commit 5770450b78
12 changed files with 281 additions and 21 deletions
+41 -2
View File
@@ -59,6 +59,12 @@ public class Converter {
}
private static Field fieldToField(PsiField field) {
if (field instanceof PsiEnumConstant) // TODO: remove instanceof
return new EnumConstant(
new IdentifierImpl(field.getName()), // TODO
typeToType(field.getType()),
elementToElement(((PsiEnumConstant) field).getArgumentList())
);
return new Field(
new IdentifierImpl(field.getName()), // TODO
typeToType(field.getType()),
@@ -76,8 +82,25 @@ public class Converter {
}
@NotNull
private static Function methodToFunction(PsiMethod t, boolean notEmpty) {
return new Function(new IdentifierImpl(t.getName()), typeToType(t.getReturnType()), bodyToBlock(t.getBody(), notEmpty));
private static Function methodToFunction(PsiMethod method, boolean notEmpty) {
final IdentifierImpl identifier = new IdentifierImpl(method.getName());
final Type type = typeToType(method.getReturnType());
final Block body = bodyToBlock(method.getBody(), notEmpty);
final Element params = elementToElement(method.getParameterList());
if (method.isConstructor())
return new Constructor(
identifier,
type,
params,
body
);
return new Function(
identifier,
type,
params,
body
);
}
@NotNull
@@ -173,4 +196,20 @@ public class Converter {
return new Import(t.getImportReference().getQualifiedName()); // TODO: use identifier
return new Import("");
}
@NotNull
public static List<Parameter> parametersToParameterList(PsiParameter[] parameters) {
List<Parameter> result = new LinkedList<Parameter>();
for (PsiParameter t : parameters) {
result.add(parameterToParameter(t));
}
return result;
}
private static Parameter parameterToParameter(PsiParameter parameter) {
return new Parameter(
new IdentifierImpl(parameter.getName()), // TODO: remove
typeToType(parameter.getType())
);
}
}
+12 -2
View File
@@ -3,6 +3,7 @@ package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.j2k.util.AstUtil;
import java.util.LinkedList;
import java.util.List;
/**
@@ -22,13 +23,22 @@ public class Class extends Node {
myFields = fields;
}
@NotNull
protected List<Function> methodsExceptConstructors() {
final LinkedList<Function> result = new LinkedList<Function>();
for (Function m : myMethods)
if (m.getKind() != Kind.CONSTRUCTOR)
result.add(m);
return result;
}
@NotNull
@Override
public String toKotlin() {
return TYPE + SPACE + myName.toKotlin() + SPACE + "{" + N +
AstUtil.joinNodes(myInnerClasses, N) + N +
AstUtil.joinNodes(myMethods, N) + N +
AstUtil.joinNodes(myFields, N) + N +
AstUtil.joinNodes(methodsExceptConstructors(), N) + N +
AstUtil.joinNodes(myInnerClasses, N) + N +
"}";
}
}
@@ -0,0 +1,22 @@
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
/**
* @author ignatov
*/
public class Constructor extends Function {
public Constructor(Identifier identifier, Type type, Element params, Block block) {
super(identifier, type, params, block);
}
public String primary() {
return "(" + myParams.toKotlin() + ")";
}
@NotNull
@Override
public Kind getKind() {
return Kind.CONSTRUCTOR;
}
}
+30 -1
View File
@@ -1,5 +1,9 @@
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.j2k.util.AstUtil;
import java.util.List;
/**
@@ -8,6 +12,31 @@ import java.util.List;
public class Enum extends Class {
public Enum(Identifier name, List<Class> innerClasses, List<Function> methods, List<Field> fields) {
super(name, innerClasses, methods, fields);
TYPE = "enum";
}
@Nullable
private Constructor getPrimaryConstructor() {
for (Function m : myMethods)
if (m.getKind() == Kind.CONSTRUCTOR)
return (Constructor) m;
return null;
}
private String primaryConstructorToKotlin() {
Constructor maybeConstructor = getPrimaryConstructor();
if (maybeConstructor != null)
return maybeConstructor.primary();
return EMPTY;
}
@NotNull
@Override
public String toKotlin() {
return "enum" + SPACE + myName.toKotlin() + primaryConstructorToKotlin() + SPACE + "{" + N +
AstUtil.joinNodes(myFields, N) + N +
AstUtil.joinNodes(methodsExceptConstructors(), N) + N +
AstUtil.joinNodes(myInnerClasses, N) + N +
"}";
}
}
@@ -0,0 +1,20 @@
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
/**
* @author ignatov
*/
public class EnumConstant extends Field {
public EnumConstant(Identifier identifier, Type type, Element params) {
super(identifier, type, params);
}
@NotNull
@Override
public String toKotlin() {
if (myInitializer.toKotlin().isEmpty())
return myIdentifier.toKotlin();
return myIdentifier.toKotlin() + "(" + myInitializer.toKotlin() + ")";
}
}
+4 -4
View File
@@ -6,11 +6,11 @@ import org.jetbrains.annotations.NotNull;
* @author ignatov
*/
public class Field extends Node {
private Identifier myIdentifier;
private Type myType;
private Expression myInitializer;
protected Identifier myIdentifier;
protected Type myType;
protected Element myInitializer;
public Field(Identifier identifier, Type type, Expression initializer) {
public Field(Identifier identifier, Type type, Element initializer) {
myIdentifier = identifier;
myType = type;
myInitializer = initializer;
+4 -2
View File
@@ -8,18 +8,20 @@ import org.jetbrains.annotations.NotNull;
public class Function extends Node {
private Identifier myName;
private Type myType;
protected Element myParams;
private Block myBlock;
public Function(Identifier name, Type type, Block block) {
public Function(Identifier name, Type type, Element params, Block block) {
myName = name;
myType = type;
myParams = params;
myBlock = block;
}
@NotNull
@Override
public String toKotlin() {
return "fun" + SPACE + myName.toKotlin() + "(" + ")" + SPACE + COLON + SPACE + myType.toKotlin() + SPACE +
return "fun" + SPACE + myName.toKotlin() + "(" + myParams.toKotlin() + ")" + SPACE + COLON + SPACE + myType.toKotlin() + SPACE +
myBlock.toKotlin();
}
}
+1 -1
View File
@@ -13,6 +13,6 @@ public interface INode {
public Kind getKind();
public enum Kind {
UNDEFINED, TYPE,
UNDEFINED, TYPE, CONSTRUCTOR,
}
}
@@ -0,0 +1,22 @@
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
/**
* @author ignatov
*/
public class Parameter extends Expression {
private IdentifierImpl myIdentifier;
private Type myType;
public Parameter(IdentifierImpl identifier, Type type) {
myIdentifier = identifier;
myType = type;
}
@NotNull
@Override
public String toKotlin() {
return myIdentifier.toKotlin() + SPACE + COLON + SPACE + myType.toKotlin();
}
}
@@ -0,0 +1,23 @@
package org.jetbrains.jet.j2k.ast;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.j2k.util.AstUtil;
import java.util.List;
/**
* @author ignatov
*/
public class ParameterList extends Expression {
private List<Parameter> myParameters;
public ParameterList(List<Parameter> parameters) {
myParameters = parameters;
}
@NotNull
@Override
public String toKotlin() {
return AstUtil.joinNodes(myParameters, COMMA_WITH_SPACE);
}
}
@@ -3,7 +3,9 @@ package org.jetbrains.jet.j2k.visitors;
import com.intellij.psi.JavaElementVisitor;
import com.intellij.psi.PsiExpressionList;
import com.intellij.psi.PsiLocalVariable;
import com.intellij.psi.PsiParameterList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.j2k.Converter;
import org.jetbrains.jet.j2k.ast.*;
import static org.jetbrains.jet.j2k.Converter.*;
@@ -31,7 +33,14 @@ public class ElementVisitor extends JavaElementVisitor implements Visitor {
expressionsToExpressionList(list.getExpressions()),
typesToTypeList(list.getExpressionTypes())
);
}
@Override
public void visitParameterList(PsiParameterList list) {
super.visitParameterList(list);
myResult = new ParameterList(
Converter.parametersToParameterList(list.getParameters())
);
}
@NotNull
+93 -9
View File
@@ -14,15 +14,99 @@ public class EnumTest extends JetTestCaseBase {
);
}
// public void testTypeSafeEnum() throws Exception {
public void testTypeSafeEnum() throws Exception {
Assert.assertEquals(
classToKotlin("enum Coin { PENNY, NICKEL, DIME, QUARTER; }"),
"enum Coin {\n" +
"PENNY\n" +
"NICKEL\n" +
"DIME\n" +
"QUARTER\n" +
"}"
);
}
//
public void testOverrideToString() throws Exception {
Assert.assertEquals(
classToKotlin(
"enum Color {" +
" WHITE, BLACK, RED, YELLOW, BLUE;" +
"@Override String toString() {" +
" return \"COLOR\";" +
"}" +
"}"),
"enum Color {\n" +
"WHITE\n" +
"BLACK\n" +
"RED\n" +
"YELLOW\n" +
"BLUE\n" +
"fun toString() : String? {\n" +
"return \"COLOR\"\n" +
"}\n" +
"}"
);
}
public void testFields() throws Exception {
Assert.assertEquals(
classToKotlin(
"enum Color {\n" +
" WHITE(21), BLACK(22), RED(23), YELLOW(24), BLUE(25);\n" +
"\n" +
" private int code;\n" +
"\n" +
" private Color(int c) {\n" +
" code = c;\n" +
" }\n" +
"\n" +
" public int getCode() {\n" +
" return code;\n" +
" }"),
"enum Color(c : Int) {\n" +
"WHITE(21)\n" +
"BLACK(22)\n" +
"RED(23)\n" +
"YELLOW(24)\n" +
"BLUE(25)\n" +
"var code : Int\n" +
"fun getCode() : Int {\n" +
"return code\n" +
"}\n" +
"}"
);
}
// public void testTwoConstructors() throws Exception {
// Assert.assertEquals(
// classToKotlin("enum Coin { PENNY, NICKEL, DIME, QUARTER; }"),
// "enum Coin {\n" +
// "PENNY\n" +
// "NICKEL\n" +
// "DIME\n" +
// "QUARTER\n" +
// "}"
// classToKotlin(
// "enum MultEnum {\n" +
// " GREMLIN(\"UTILITY\"),\n" +
// " MORT(30);\n" +
// " \n" +
// " MultEnum(String s) {\n" +
// " }\n" +
// " \n" +
// " MultEnum(int dmg) {\n" +
// " }"),
// "" // TODO: will fail
// );
// }
}
//
// public void testInterfaceImplementation() throws Exception {
// Assert.assertEquals(
// classToKotlin(
// "enum Color implements Runnable {\n" +
// " WHITE, BLACK, RED, YELLOW, BLUE;\n" +
// "\n" +
// " public void run() {\n" +
// " System.out.println(\"name()=\" + name() +\n" +
// " \", toString()=\" + toString());\n" +
// " }\n" +
// "}"),
// ""
// );
// }
}