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