KT-639 Collect all classes for convertation and only for those classes use mechanism with static init functions invocation
This commit is contained in:
@@ -16,6 +16,20 @@ import java.util.*;
|
||||
* @author ignatov
|
||||
*/
|
||||
public class Converter {
|
||||
private static Set<String> ourClassIdentifiers = new HashSet<String>();
|
||||
|
||||
public static void setClassIdentifiers(Set<String> identifiers) {
|
||||
ourClassIdentifiers = identifiers;
|
||||
}
|
||||
|
||||
public static Set<String> getClassIdentifiers() {
|
||||
return new HashSet<String>(ourClassIdentifiers);
|
||||
}
|
||||
|
||||
public static void clearClassIdentifiers() {
|
||||
ourClassIdentifiers.clear();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static File fileToFile(@NotNull PsiJavaFile javaFile) {
|
||||
final PsiImportList importList = javaFile.getImportList();
|
||||
@@ -122,7 +136,7 @@ public class Converter {
|
||||
for (final Member m : members) {
|
||||
// and modify secondaries
|
||||
if (m.getKind() == INode.Kind.CONSTRUCTOR) {
|
||||
Function f = (Function)m;
|
||||
Function f = (Function) m;
|
||||
if (!((Constructor) f).isPrimary()) {
|
||||
for (Field fo : finalOrWithEmptyInitializer) {
|
||||
String init = getDefaultInitializer(fo);
|
||||
@@ -277,7 +291,7 @@ public class Converter {
|
||||
if (method.getParent() instanceof PsiClass) {
|
||||
final PsiModifierList parentModifierList = ((PsiClass) method.getParent()).getModifierList();
|
||||
if (parentModifierList != null && parentModifierList.hasExplicitModifier(Modifier.FINAL))
|
||||
modifiers.add(Modifier.NOT_OPEN);
|
||||
modifiers.add(Modifier.NOT_OPEN);
|
||||
}
|
||||
|
||||
if (method.isConstructor()) { // TODO: simplify
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.jetbrains.jet.j2k.visitors;
|
||||
|
||||
import com.intellij.psi.JavaRecursiveElementVisitor;
|
||||
import com.intellij.psi.PsiClass;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author ignatov
|
||||
*/
|
||||
public class ClassVisitor extends JavaRecursiveElementVisitor {
|
||||
private Set<String> myClassIdentifiers;
|
||||
|
||||
public ClassVisitor() {
|
||||
myClassIdentifiers = new HashSet<String>();
|
||||
}
|
||||
|
||||
public Set<String> getClassIdentifiers() {
|
||||
return new HashSet<String>(myClassIdentifiers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitClass(PsiClass aClass) {
|
||||
myClassIdentifiers.add(aClass.getQualifiedName());
|
||||
super.visitClass(aClass);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package org.jetbrains.jet.j2k.visitors;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.j2k.Converter;
|
||||
import org.jetbrains.jet.j2k.ast.*;
|
||||
|
||||
import java.util.List;
|
||||
@@ -85,7 +84,7 @@ public class ElementVisitor extends JavaElementVisitor {
|
||||
public void visitParameterList(PsiParameterList list) {
|
||||
super.visitParameterList(list);
|
||||
myResult = new ParameterList(
|
||||
Converter.parametersToParameterList(list.getParameters())
|
||||
parametersToParameterList(list.getParameters())
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -211,10 +211,12 @@ public class ExpressionVisitor extends StatementVisitor {
|
||||
private static Expression createNewClassExpression(@NotNull PsiNewExpression expression) {
|
||||
final PsiAnonymousClass anonymousClass = expression.getAnonymousClass();
|
||||
final PsiMethod constructor = expression.resolveMethod();
|
||||
if (constructor == null || isConstructorPrimary(constructor)) {
|
||||
PsiJavaCodeReferenceElement classReference = expression.getClassOrAnonymousClassReference();
|
||||
final boolean isNotConvertedClass = classReference != null && !Converter.getClassIdentifiers().contains(classReference.getQualifiedName());
|
||||
if (constructor == null || isConstructorPrimary(constructor) || isNotConvertedClass) {
|
||||
return new NewClassExpression(
|
||||
expressionToExpression(expression.getQualifier()),
|
||||
elementToElement(expression.getClassOrAnonymousClassReference()),
|
||||
elementToElement(classReference),
|
||||
elementToElement(expression.getArgumentList()),
|
||||
anonymousClass != null ? anonymousClassToAnonymousClass(anonymousClass) : null
|
||||
);
|
||||
@@ -336,7 +338,7 @@ public class ExpressionVisitor extends StatementVisitor {
|
||||
PsiElement context = expression.getContext();
|
||||
while (context != null) {
|
||||
if (context instanceof PsiMethod && ((PsiMethod) context).isConstructor())
|
||||
return !Converter.isConstructorPrimary((PsiMethod) context);
|
||||
return !isConstructorPrimary((PsiMethod) context);
|
||||
context = context.getContext();
|
||||
}
|
||||
return false;
|
||||
@@ -346,7 +348,7 @@ public class ExpressionVisitor extends StatementVisitor {
|
||||
PsiElement context = expression.getContext();
|
||||
while (context != null) {
|
||||
if (context instanceof PsiMethod && ((PsiMethod) context).isConstructor())
|
||||
return Converter.isConstructorPrimary((PsiMethod) context);
|
||||
return isConstructorPrimary((PsiMethod) context);
|
||||
context = context.getContext();
|
||||
}
|
||||
return false;
|
||||
@@ -416,11 +418,4 @@ public class ExpressionVisitor extends StatementVisitor {
|
||||
getOperatorString(expression.getOperationTokenType())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Test {
|
||||
void test() {
|
||||
char c1 = 'c';
|
||||
Character c2 = 'C';
|
||||
}
|
||||
}
|
||||
@@ -126,7 +126,7 @@ public class StatementVisitor extends ElementVisitor {
|
||||
) {
|
||||
final Expression end = expressionToExpression(((PsiBinaryExpression) condition).getROperand());
|
||||
final Expression endExpression = operationTokenType == JavaTokenType.LT ?
|
||||
new BinaryExpression(end, new IdentifierImpl("1"), "-"):
|
||||
new BinaryExpression(end, new IdentifierImpl("1"), "-") :
|
||||
end;
|
||||
myResult = new ForeachWithRangeStatement(
|
||||
new IdentifierImpl(firstChild.getName()),
|
||||
|
||||
@@ -8,6 +8,7 @@ import junit.framework.Assert;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.j2k.visitors.ClassVisitor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -96,9 +97,16 @@ public class JavaToKotlinConverterTest extends LightDaemonAnalyzerTestCase {
|
||||
configureFromFileText("test.java", text);
|
||||
}
|
||||
|
||||
private static void setClassIdentifiers() {
|
||||
ClassVisitor c = new ClassVisitor();
|
||||
myFile.accept(c);
|
||||
Converter.setClassIdentifiers(c.getClassIdentifiers());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String fileToKotlin(String text) throws IOException {
|
||||
configureFromText(text);
|
||||
setClassIdentifiers();
|
||||
return prettify(Converter.fileToFile((PsiJavaFile) myFile).toKotlin());
|
||||
}
|
||||
|
||||
|
||||
@@ -16,76 +16,76 @@ import java.util.List;
|
||||
* @author ignatov
|
||||
*/
|
||||
abstract class TestCaseBuilder {
|
||||
private static final FilenameFilter emptyFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File file, String name) {
|
||||
return true;
|
||||
}
|
||||
private static final FilenameFilter emptyFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File file, String name) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
public static String getTestDataPathBase() {
|
||||
return "testData";
|
||||
}
|
||||
|
||||
public static String getHomeDirectory() {
|
||||
return new File(PathManager.getResourceRoot(TestCaseBuilder.class, "/org/jetbrains/jet/TestCaseBuilder.class")).getParentFile().getParentFile().getParent();
|
||||
}
|
||||
|
||||
public interface NamedTestFactory {
|
||||
@NotNull
|
||||
Test createTest(@NotNull String dataPath, @NotNull String name);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, boolean recursive, @NotNull NamedTestFactory factory) {
|
||||
return suiteForDirectory(baseDataDir, dataPath, recursive, emptyFilter, factory);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
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";
|
||||
|
||||
final FilenameFilter extensionFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
return name.endsWith(extensionJava);
|
||||
}
|
||||
};
|
||||
|
||||
public static String getTestDataPathBase() {
|
||||
return "testData";
|
||||
}
|
||||
|
||||
public static String getHomeDirectory() {
|
||||
return new File(PathManager.getResourceRoot(TestCaseBuilder.class, "/org/jetbrains/jet/TestCaseBuilder.class")).getParentFile().getParentFile().getParent();
|
||||
}
|
||||
|
||||
public interface NamedTestFactory {
|
||||
@NotNull Test createTest(@NotNull String dataPath, @NotNull String name);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static TestSuite suiteForDirectory(String baseDataDir, @NotNull final String dataPath, boolean recursive, @NotNull NamedTestFactory factory) {
|
||||
return suiteForDirectory(baseDataDir, dataPath, recursive, emptyFilter, factory);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
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";
|
||||
|
||||
final FilenameFilter extensionFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
return name.endsWith(extensionJava);
|
||||
}
|
||||
};
|
||||
FilenameFilter resultFilter;
|
||||
if (filter != emptyFilter) {
|
||||
resultFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File file, String s) {
|
||||
return extensionFilter.accept(file, s) && filter.accept(file, s);
|
||||
}
|
||||
};
|
||||
FilenameFilter resultFilter;
|
||||
if (filter != emptyFilter) {
|
||||
resultFilter = new FilenameFilter() {
|
||||
@Override
|
||||
public boolean accept(File file, String s) {
|
||||
return extensionFilter.accept(file, s) && filter.accept(file, s);
|
||||
}
|
||||
else {
|
||||
resultFilter = extensionFilter;
|
||||
}
|
||||
File dir = new File(baseDataDir + dataPath);
|
||||
FileFilter dirFilter = new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(File pathname) {
|
||||
return pathname.isDirectory();
|
||||
}
|
||||
};
|
||||
if (recursive) {
|
||||
File[] files = dir.listFiles(dirFilter);
|
||||
assert files != null : dir;
|
||||
List<File> subdirs = Arrays.asList(files);
|
||||
Collections.sort(subdirs);
|
||||
for (File subdir : subdirs) {
|
||||
suite.addTest(suiteForDirectory(baseDataDir, dataPath + "/" + subdir.getName(), recursive, filter, factory));
|
||||
}
|
||||
}
|
||||
List<File> files = Arrays.asList(dir.listFiles(resultFilter));
|
||||
Collections.sort(files);
|
||||
for (File file : files) {
|
||||
String fileName = file.getName();
|
||||
assert fileName != null;
|
||||
suite.addTest(factory.createTest(dataPath, fileName.substring(0, fileName.length() - extensionJava.length())));
|
||||
}
|
||||
return suite;
|
||||
};
|
||||
} else {
|
||||
resultFilter = extensionFilter;
|
||||
}
|
||||
File dir = new File(baseDataDir + dataPath);
|
||||
FileFilter dirFilter = new FileFilter() {
|
||||
@Override
|
||||
public boolean accept(File pathname) {
|
||||
return pathname.isDirectory();
|
||||
}
|
||||
};
|
||||
if (recursive) {
|
||||
File[] files = dir.listFiles(dirFilter);
|
||||
assert files != null : dir;
|
||||
List<File> subdirs = Arrays.asList(files);
|
||||
Collections.sort(subdirs);
|
||||
for (File subdir : subdirs) {
|
||||
suite.addTest(suiteForDirectory(baseDataDir, dataPath + "/" + subdir.getName(), recursive, filter, factory));
|
||||
}
|
||||
}
|
||||
List<File> files = Arrays.asList(dir.listFiles(resultFilter));
|
||||
Collections.sort(files);
|
||||
for (File file : files) {
|
||||
String fileName = file.getName();
|
||||
assert fileName != null;
|
||||
suite.addTest(factory.createTest(dataPath, fileName.substring(0, fileName.length() - extensionJava.length())));
|
||||
}
|
||||
return suite;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package demo;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
class Test {
|
||||
Test() { }
|
||||
Test(String s) { }
|
||||
}
|
||||
|
||||
class User {
|
||||
void main() {
|
||||
HashMap m = new HashMap(1);
|
||||
HashMap m2 = new HashMap(10);
|
||||
|
||||
Test t1 = new Test();
|
||||
Test t2 = new Test("");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace demo
|
||||
import java.util.HashMap
|
||||
open class Test() {
|
||||
class object {
|
||||
open fun init() : Test {
|
||||
val __ = Test()
|
||||
return __
|
||||
}
|
||||
open fun init(s : String?) : Test {
|
||||
val __ = Test()
|
||||
return __
|
||||
}
|
||||
}
|
||||
}
|
||||
open class User() {
|
||||
open fun main() : Unit {
|
||||
var m : HashMap<*, *>? = HashMap(1)
|
||||
var m2 : HashMap<*, *>? = HashMap(10)
|
||||
var t1 : Test? = Test.init()
|
||||
var t2 : Test? = Test.init("")
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
enum Color {
|
||||
WHITE(21), BLACK(22), RED(23), YELLOW(24), BLUE(25);
|
||||
WHITE(21), BLACK(22), RED(23), YELLOW(24), BLUE(25);
|
||||
|
||||
private int code;
|
||||
private int code;
|
||||
|
||||
private Color(int c) {
|
||||
code = c;
|
||||
}
|
||||
private Color(int c) {
|
||||
code = c;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package demo;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
class Test {
|
||||
void main() {
|
||||
HashMap<String, Integer> commonMap = new HashMap<String, Integer>();
|
||||
HashMap rawMap = new HashMap<String, Integer>();
|
||||
HashMap superRawMap = new HashMap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace demo
|
||||
import java.util.HashMap
|
||||
open class Test() {
|
||||
open fun main() : Unit {
|
||||
var commonMap : HashMap<String?, Int?>? = HashMap<String?, Int?>()
|
||||
var rawMap : HashMap<*, *>? = HashMap<String?, Int?>()
|
||||
var superRawMap : HashMap<*, *>? = HashMap()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user