This commit is contained in:
Sergey Ignatov
2011-11-15 16:19:23 +04:00
parent 437fe1b6b4
commit a80f0eecd4
30 changed files with 56 additions and 87 deletions
+9 -9
View File
@@ -36,7 +36,7 @@ public class Converter {
// TODO: replace by Block, use class.getChild() method // TODO: replace by Block, use class.getChild() method
return new AnonymousClass( return new AnonymousClass(
classesToClassList(anonymousClass.getAllInnerClasses()), classesToClassList(anonymousClass.getAllInnerClasses()),
methodsToFunctionList(anonymousClass.getMethods(), true), methodsToFunctionList(anonymousClass.getMethods()),
fieldsToFieldList(anonymousClass.getAllFields()) fieldsToFieldList(anonymousClass.getAllFields())
); );
} }
@@ -73,10 +73,10 @@ public class Converter {
return s + "(" + AstUtil.join(result, ", ") + ")"; return s + "(" + AstUtil.join(result, ", ") + ")";
} }
public static Class classToClass(PsiClass psiClass) { private static Class classToClass(PsiClass psiClass) {
final Set<String> modifiers = modifiersListToModifiersSet(psiClass.getModifierList()); final Set<String> modifiers = modifiersListToModifiersSet(psiClass.getModifierList());
final List<Class> innerClasses = classesToClassList(psiClass.getAllInnerClasses()); final List<Class> innerClasses = classesToClassList(psiClass.getAllInnerClasses());
final List<Function> methods = methodsToFunctionList(psiClass.getMethods(), true); final List<Function> methods = methodsToFunctionList(psiClass.getMethods());
final List<Field> fields = fieldsToFieldList(psiClass.getFields()); final List<Field> fields = fieldsToFieldList(psiClass.getFields());
final List<Element> typeParameters = elementsToElementList(psiClass.getTypeParameters()); final List<Element> typeParameters = elementsToElementList(psiClass.getTypeParameters());
final List<Type> implementsTypes = typesToNotNullableTypeList(psiClass.getImplementsListTypes()); final List<Type> implementsTypes = typesToNotNullableTypeList(psiClass.getImplementsListTypes());
@@ -113,7 +113,7 @@ public class Converter {
for (Statement s : f.getBlock().getStatements()) { for (Statement s : f.getBlock().getStatements()) {
boolean isRemoved = false; boolean isRemoved = false;
if (s.getKind() == INode.Kind.ASSINGNMENT_EXPRESSION) { if (s.getKind() == INode.Kind.ASSIGNMENT_EXPRESSION) {
final AssignmentExpression assignmentExpression = (AssignmentExpression) s; final AssignmentExpression assignmentExpression = (AssignmentExpression) s;
if (assignmentExpression.getLeft().getKind() == INode.Kind.CALL_CHAIN) { if (assignmentExpression.getLeft().getKind() == INode.Kind.CALL_CHAIN) {
for (Field fo : finalOrWithEmptyInitializer) { for (Field fo : finalOrWithEmptyInitializer) {
@@ -146,7 +146,7 @@ public class Converter {
new Constructor( new Constructor(
Identifier.EMPTY_IDENTIFIER, Identifier.EMPTY_IDENTIFIER,
Collections.<String>emptySet(), Collections.<String>emptySet(),
new ClassType(name, false), new ClassType(name),
Collections.<Element>emptyList(), Collections.<Element>emptyList(),
new ParameterList(createParametersFromFields(finalOrWithEmptyInitializer)), new ParameterList(createParametersFromFields(finalOrWithEmptyInitializer)),
new Block(createInitStatementsFromFields(finalOrWithEmptyInitializer)), new Block(createInitStatementsFromFields(finalOrWithEmptyInitializer)),
@@ -224,14 +224,14 @@ public class Converter {
} }
@NotNull @NotNull
private static List<Function> methodsToFunctionList(PsiMethod[] methods, boolean notEmpty) { private static List<Function> methodsToFunctionList(PsiMethod[] methods) {
List<Function> result = new LinkedList<Function>(); List<Function> result = new LinkedList<Function>();
for (PsiMethod t : methods) result.add(methodToFunction(t, notEmpty)); for (PsiMethod t : methods) result.add(methodToFunction(t, true));
return result; return result;
} }
@Nullable @Nullable
public static PsiMethod getPrimaryConstructorForThisCase(PsiClass psiClass) { private static PsiMethod getPrimaryConstructorForThisCase(PsiClass psiClass) {
ThisVisitor tv = new ThisVisitor(); ThisVisitor tv = new ThisVisitor();
psiClass.accept(tv); psiClass.accept(tv);
return tv.getPrimaryConstructor(); return tv.getPrimaryConstructor();
@@ -371,7 +371,7 @@ public class Converter {
@NotNull @NotNull
private static List<Type> typesToNotNullableTypeList(PsiType[] types) { private static List<Type> typesToNotNullableTypeList(PsiType[] types) {
List<Type> result = new LinkedList<Type>(typesToTypeList(types)); List<Type> result = new LinkedList<Type>(typesToTypeList(types));
for (Type p : result) p.setNullable(false); for (Type p : result) p.convertToNotNull();
return result; return result;
} }
@@ -33,6 +33,6 @@ public class AssignmentExpression extends Expression {
@NotNull @NotNull
@Override @Override
public Kind getKind() { public Kind getKind() {
return Kind.ASSINGNMENT_EXPRESSION; return Kind.ASSIGNMENT_EXPRESSION;
} }
} }
@@ -6,8 +6,8 @@ import org.jetbrains.annotations.NotNull;
* @author ignatov * @author ignatov
*/ */
public class CallChainExpression extends Expression { public class CallChainExpression extends Expression {
private Expression myExpression; private final Expression myExpression;
private Expression myIdentifier; private final Expression myIdentifier;
public Expression getIdentifier() { public Expression getIdentifier() {
return myIdentifier; return myIdentifier;
+1 -1
View File
@@ -103,7 +103,7 @@ public class Class extends Member {
result.add(new Function( result.add(new Function(
new IdentifierImpl("init"), new IdentifierImpl("init"),
modifiers, modifiers,
new ClassType(myName, false), new ClassType(myName),
m.getTypeParameters(), m.getTypeParameters(),
m.getParams(), m.getParams(),
block block
+3 -3
View File
@@ -18,10 +18,10 @@ public class ClassType extends Type {
myParameters = parameters; myParameters = parameters;
} }
public ClassType(Identifier type, boolean isNullable) { public ClassType(Identifier type) {
myType = type; myType = type;
myNullable = isNullable; myNullable = false;
myParameters = Collections.<Type>emptyList(); myParameters = Collections.emptyList();
} }
@NotNull @NotNull
@@ -9,7 +9,7 @@ import java.util.Set;
* @author ignatov * @author ignatov
*/ */
public class Constructor extends Function { public class Constructor extends Function {
private boolean myIsPrimary; private final boolean myIsPrimary;
public Constructor(Identifier identifier, Set<String> modifiers, Type type, List<Element> typeParameters, Element params, Block block, boolean isPrimary) { public Constructor(Identifier identifier, Set<String> modifiers, Type type, List<Element> typeParameters, Element params, Block block, boolean isPrimary) {
super(identifier, modifiers, type, typeParameters, params, block); super(identifier, modifiers, type, typeParameters, params, block);
@@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull;
* @author ignatov * @author ignatov
*/ */
public class DummyStringStatement extends Statement { public class DummyStringStatement extends Statement {
private String myString; private final String myString;
public DummyStringStatement(String string) { public DummyStringStatement(String string) {
myString = string; myString = string;
+1 -1
View File
@@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull;
* @author ignatov * @author ignatov
*/ */
public abstract class Element extends Node { public abstract class Element extends Node {
public static Element EMPTY_ELEMENT = new EmptyElement(); public static final Element EMPTY_ELEMENT = new EmptyElement();
/** /**
* @author ignatov * @author ignatov
@@ -24,7 +24,7 @@ public abstract class Expression extends Statement {
} }
} }
public boolean isNullable() { boolean isNullable() {
return false; return false;
} }
@@ -13,7 +13,6 @@ public class ExpressionList extends Expression {
public ExpressionList(List<Expression> expressions, List<Type> types) { public ExpressionList(List<Expression> expressions, List<Type> types) {
myExpressions = expressions; myExpressions = expressions;
List<Type> types1 = types;
} }
public ExpressionList(List<Expression> expressions) { public ExpressionList(List<Expression> expressions) {
+1 -1
View File
@@ -13,6 +13,6 @@ public interface INode {
public Kind getKind(); public Kind getKind();
public enum Kind { public enum Kind {
UNDEFINED, TYPE, CONSTRUCTOR, BREAK, CONTINUE, VARARG, TRAIT, ASSINGNMENT_EXPRESSION, CALL_CHAIN, UNDEFINED, TYPE, CONSTRUCTOR, BREAK, CONTINUE, VARARG, TRAIT, ASSIGNMENT_EXPRESSION, CALL_CHAIN,
} }
} }
@@ -6,7 +6,5 @@ package org.jetbrains.jet.j2k.ast;
public interface Identifier extends INode { public interface Identifier extends INode {
public static Identifier EMPTY_IDENTIFIER = new IdentifierImpl(""); public static Identifier EMPTY_IDENTIFIER = new IdentifierImpl("");
public String getName();
public boolean isEmpty(); public boolean isEmpty();
} }
@@ -25,11 +25,6 @@ public class IdentifierImpl extends Expression implements Identifier {
myIsNullable = isNullable; myIsNullable = isNullable;
} }
@Override
public String getName() {
return myName;
}
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
return myName.length() == 0; return myName.length() == 0;
@@ -6,8 +6,8 @@ import org.jetbrains.annotations.NotNull;
* @author ignatov * @author ignatov
*/ */
public class IsOperator extends Expression { public class IsOperator extends Expression {
private Expression myExpression; private final Expression myExpression;
private Element myTypeElement; private final Element myTypeElement;
public IsOperator(Expression expression, Element typeElement) { public IsOperator(Expression expression, Element typeElement) {
myExpression = expression; myExpression = expression;
@@ -9,7 +9,7 @@ import java.util.Set;
*/ */
public class LocalVariable extends Expression { public class LocalVariable extends Expression {
private final Identifier myIdentifier; private final Identifier myIdentifier;
private Set<String> myModifiersSet; private final Set<String> myModifiersSet;
private final Type myType; private final Type myType;
private final Expression myInitializer; private final Expression myInitializer;
@@ -8,7 +8,7 @@ import org.jetbrains.annotations.NotNull;
public class MethodCallExpression extends Expression { public class MethodCallExpression extends Expression {
private final Expression myMethodCall; private final Expression myMethodCall;
private final Element myParamList; private final Element myParamList;
private boolean myIsResultNullable; private final boolean myIsResultNullable;
public MethodCallExpression(Expression methodCall, Element paramList, boolean nullable) { public MethodCallExpression(Expression methodCall, Element paramList, boolean nullable) {
myMethodCall = methodCall; myMethodCall = methodCall;
@@ -9,8 +9,8 @@ import java.util.List;
* @author ignatov * @author ignatov
*/ */
public class PolyadicExpression extends Expression { public class PolyadicExpression extends Expression {
private List<Expression> myExpressions; private final List<Expression> myExpressions;
private String myToken; private final String myToken;
public PolyadicExpression(List<Expression> expressions, String token) { public PolyadicExpression(List<Expression> expressions, String token) {
super(); super();
@@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull;
* @author ignatov * @author ignatov
*/ */
public class SuperExpression extends Expression { public class SuperExpression extends Expression {
private Identifier myIdentifier; private final Identifier myIdentifier;
public SuperExpression(Identifier identifier) { public SuperExpression(Identifier identifier) {
myIdentifier = identifier; myIdentifier = identifier;
@@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull;
* @author ignatov * @author ignatov
*/ */
public class ThisExpression extends Expression { public class ThisExpression extends Expression {
private Identifier myIdentifier; private final Identifier myIdentifier;
public ThisExpression(Identifier identifier) { public ThisExpression(Identifier identifier) {
myIdentifier = identifier; myIdentifier = identifier;
+2 -2
View File
@@ -15,8 +15,8 @@ public abstract class Type extends Element {
return Kind.TYPE; return Kind.TYPE;
} }
public void setNullable(boolean nullable) { public void convertToNotNull() {
myNullable = nullable; myNullable = false;
} }
public boolean isNullable() { public boolean isNullable() {
@@ -6,7 +6,7 @@ import org.jetbrains.annotations.NotNull;
* @author ignatov * @author ignatov
*/ */
public class TypeElement extends Element { public class TypeElement extends Element {
private Type myType; private final Type myType;
public TypeElement(Type type) { public TypeElement(Type type) {
myType = type; myType = type;
@@ -10,11 +10,10 @@ import static org.jetbrains.jet.j2k.Converter.*;
/** /**
* @author ignatov * @author ignatov
*/ */
public class ElementVisitor extends JavaElementVisitor implements Visitor { public class ElementVisitor extends JavaElementVisitor {
private Element myResult = Element.EMPTY_ELEMENT; private Element myResult = Element.EMPTY_ELEMENT;
@NotNull @NotNull
@Override
public Element getResult() { public Element getResult() {
return myResult; return myResult;
} }
@@ -14,7 +14,7 @@ import static org.jetbrains.jet.j2k.Converter.*;
/** /**
* @author ignatov * @author ignatov
*/ */
public class ExpressionVisitor extends StatementVisitor implements Visitor { public class ExpressionVisitor extends StatementVisitor {
private Expression myResult = Expression.EMPTY_EXPRESSION; private Expression myResult = Expression.EMPTY_EXPRESSION;
@NotNull @NotNull
@@ -13,7 +13,7 @@ import static org.jetbrains.jet.j2k.Converter.*;
/** /**
* @author ignatov * @author ignatov
*/ */
public class StatementVisitor extends ElementVisitor implements Visitor { public class StatementVisitor extends ElementVisitor {
private Statement myResult = Statement.EMPTY_STATEMENT; private Statement myResult = Statement.EMPTY_STATEMENT;
@NotNull @NotNull
@@ -9,7 +9,7 @@ import java.util.HashSet;
* @author ignatov * @author ignatov
*/ */
public class SuperVisitor extends JavaRecursiveElementVisitor { public class SuperVisitor extends JavaRecursiveElementVisitor {
private HashSet<PsiExpressionList> myResolvedSuperCallParameters; private final HashSet<PsiExpressionList> myResolvedSuperCallParameters;
public SuperVisitor() { public SuperVisitor() {
myResolvedSuperCallParameters = new HashSet<PsiExpressionList>(); myResolvedSuperCallParameters = new HashSet<PsiExpressionList>();
@@ -1,9 +1,6 @@
package org.jetbrains.jet.j2k.visitors; package org.jetbrains.jet.j2k.visitors;
import com.intellij.psi.JavaRecursiveElementVisitor; import com.intellij.psi.*;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceExpression;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.HashSet; import java.util.HashSet;
@@ -12,14 +9,16 @@ import java.util.HashSet;
* @author ignatov * @author ignatov
*/ */
public class ThisVisitor extends JavaRecursiveElementVisitor { public class ThisVisitor extends JavaRecursiveElementVisitor {
HashSet<PsiMethod> myResolvedConstructors = new HashSet<PsiMethod>(); private final HashSet<PsiMethod> myResolvedConstructors = new HashSet<PsiMethod>();
@Override @Override
public void visitReferenceExpression(PsiReferenceExpression expression) { public void visitReferenceExpression(PsiReferenceExpression expression) {
for (PsiReference r : expression.getReferences()) for (PsiReference r : expression.getReferences())
if (r.getCanonicalText().equals("this")) if (r.getCanonicalText().equals("this")) {
if (r.resolve() != null && r.resolve() instanceof PsiMethod && ((PsiMethod) r.resolve()).isConstructor()) final PsiElement res = r.resolve();
myResolvedConstructors.add((PsiMethod) r.resolve()); if (res != null && res instanceof PsiMethod && ((PsiMethod) res).isConstructor())
myResolvedConstructors.add((PsiMethod) res);
}
} }
@Nullable @Nullable
@@ -11,7 +11,7 @@ import static org.jetbrains.jet.j2k.Converter.typesToTypeList;
/** /**
* @author ignatov * @author ignatov
*/ */
public class TypeVisitor extends PsiTypeVisitor<Type> implements Visitor { public class TypeVisitor extends PsiTypeVisitor<Type> {
private Type myResult = Type.EMPTY_TYPE; private Type myResult = Type.EMPTY_TYPE;
@NotNull @NotNull
@@ -19,11 +19,6 @@ public class TypeVisitor extends PsiTypeVisitor<Type> implements Visitor {
return myResult; return myResult;
} }
@Override
public Type visitType(PsiType type) {
return super.visitType(type);
}
@Override @Override
public Type visitPrimitiveType(PsiPrimitiveType primitiveType) { public Type visitPrimitiveType(PsiPrimitiveType primitiveType) {
final String name = primitiveType.getCanonicalText(); final String name = primitiveType.getCanonicalText();
@@ -1,12 +0,0 @@
package org.jetbrains.jet.j2k.visitors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.j2k.ast.Element;
/**
* @author ignatov
*/
public interface Visitor {
@NotNull
Element getResult();
}
@@ -20,8 +20,8 @@ import static org.jetbrains.jet.j2k.TestCaseBuilder.getTestDataPathBase;
* @author ignatov * @author ignatov
*/ */
public class JavaToKotlinConverterTest extends LightDaemonAnalyzerTestCase { public class JavaToKotlinConverterTest extends LightDaemonAnalyzerTestCase {
private String myDataPath; private final String myDataPath;
private String myName; private final String myName;
public JavaToKotlinConverterTest(String dataPath, String name) { public JavaToKotlinConverterTest(String dataPath, String name) {
myDataPath = dataPath; myDataPath = dataPath;
@@ -51,18 +51,14 @@ public class JavaToKotlinConverterTest extends LightDaemonAnalyzerTestCase {
final File tmp = new File(kotlinPath + ".tmp"); final File tmp = new File(kotlinPath + ".tmp");
if (!expected.equals(actual)) writeStringToFile(tmp, actual); if (!expected.equals(actual)) writeStringToFile(tmp, actual);
if (expected.equals(actual) && tmp.exists()) tmp.delete(); if (expected.equals(actual) && tmp.exists()) //noinspection ResultOfMethodCallIgnored
tmp.delete();
Assert.assertEquals(expected, actual); Assert.assertEquals(expected, actual);
} }
@Override
protected void setUp() throws Exception {
super.setUp();
}
@NotNull @NotNull
protected String getTestFilePath() { String getTestFilePath() {
return myDataPath + File.separator + myName + ".jav"; return myDataPath + File.separator + myName + ".jav";
} }
@@ -101,13 +97,13 @@ public class JavaToKotlinConverterTest extends LightDaemonAnalyzerTestCase {
} }
@NotNull @NotNull
protected String fileToKotlin(String text) throws IOException { String fileToKotlin(String text) throws IOException {
configureFromText(text); configureFromText(text);
return prettify(Converter.fileToFile((PsiJavaFile) myFile).toKotlin()); return prettify(Converter.fileToFile((PsiJavaFile) myFile).toKotlin());
} }
@NotNull @NotNull
protected String methodToKotlin(String text) throws IOException { String methodToKotlin(String text) throws IOException {
String result = fileToKotlin("final class C {" + text + "}") String result = fileToKotlin("final class C {" + text + "}")
.replaceAll("class C\\(\\) \\{", ""); .replaceAll("class C\\(\\) \\{", "");
result = result.substring(0, result.lastIndexOf("}")); result = result.substring(0, result.lastIndexOf("}"));
@@ -115,7 +111,7 @@ public class JavaToKotlinConverterTest extends LightDaemonAnalyzerTestCase {
} }
@NotNull @NotNull
protected String statementToKotlin(String text) throws Exception { String statementToKotlin(String text) throws Exception {
String result = methodToKotlin("void main() {" + text + "}"); String result = methodToKotlin("void main() {" + text + "}");
int pos = result.lastIndexOf("}"); int pos = result.lastIndexOf("}");
result = result.substring(0, pos).replaceFirst("open fun main\\(\\) : Unit \\{", ""); result = result.substring(0, pos).replaceFirst("open fun main\\(\\) : Unit \\{", "");
@@ -123,7 +119,7 @@ public class JavaToKotlinConverterTest extends LightDaemonAnalyzerTestCase {
} }
@NotNull @NotNull
protected String expressionToKotlin(String code) throws Exception { String expressionToKotlin(String code) throws Exception {
String result = statementToKotlin("Object o =" + code + "}"); String result = statementToKotlin("Object o =" + code + "}");
result = result.replaceFirst("var o : Any\\? =", ""); result = result.replaceFirst("var o : Any\\? =", "");
return prettify(result); return prettify(result);
@@ -15,8 +15,8 @@ import java.util.List;
/** /**
* @author ignatov * @author ignatov
*/ */
public abstract class TestCaseBuilder { abstract class TestCaseBuilder {
private static FilenameFilter emptyFilter = new FilenameFilter() { private static final FilenameFilter emptyFilter = new FilenameFilter() {
@Override @Override
public boolean accept(File file, String name) { public boolean accept(File file, String name) {
return true; return true;
@@ -41,7 +41,7 @@ public abstract class TestCaseBuilder {
} }
@NotNull @NotNull
public static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, boolean recursive, final FilenameFilter filter, @NotNull NamedTestFactory factory) { private static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, boolean recursive, final FilenameFilter filter, @NotNull NamedTestFactory factory) {
TestSuite suite = new TestSuite(dataPath); TestSuite suite = new TestSuite(dataPath);
final String extensionJava = ".jav"; final String extensionJava = ".jav";