From e678e072f1871ea158ba209a88335429ea7568d8 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Fri, 27 Jan 2012 17:31:05 +0400 Subject: [PATCH] Initial include of the J2K Converter --- .idea/modules.xml | 1 + j2k/j2k.iml | 5 +- .../jetbrains/jet/j2k/JavaToKotlinCli.java | 294 +++++++++--------- .../StandaloneJavaToKotlinConverterTest.java | 249 ++++++++------- 4 files changed, 281 insertions(+), 268 deletions(-) diff --git a/.idea/modules.xml b/.idea/modules.xml index 36ef0191ae1..e12dd594db4 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -12,6 +12,7 @@ + diff --git a/j2k/j2k.iml b/j2k/j2k.iml index 2b7c256c235..07225cb1a06 100644 --- a/j2k/j2k.iml +++ b/j2k/j2k.iml @@ -8,9 +8,8 @@ - - - + + diff --git a/j2k/src/org/jetbrains/jet/j2k/JavaToKotlinCli.java b/j2k/src/org/jetbrains/jet/j2k/JavaToKotlinCli.java index 1f322e030ee..094fa5db7de 100644 --- a/j2k/src/org/jetbrains/jet/j2k/JavaToKotlinCli.java +++ b/j2k/src/org/jetbrains/jet/j2k/JavaToKotlinCli.java @@ -1,147 +1,147 @@ -package org.jetbrains.jet.j2k; - -import com.intellij.psi.PsiFile; -import com.intellij.psi.PsiJavaFile; -import org.apache.commons.cli.*; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.logging.Logger; -import java.util.regex.Pattern; - -import static org.apache.commons.io.FileUtils.readFileToString; -import static org.apache.commons.io.FileUtils.writeStringToFile; - -/** - * @author ignatov - */ -@SuppressWarnings({"CallToPrintStackTrace", "UseOfSystemOutOrSystemErr"}) -public class JavaToKotlinCli { - private static final Logger myLogger = Logger.getAnonymousLogger(); - - private JavaToKotlinCli() { - } - - public static void main(String[] args) { - CommandLineParser parser = new BasicParser(); - Options options = new Options() - .addOption("h", "help", false, "Print usage information") - .addOption("f", "from", true, "Directory with Java sources") - .addOption("t", "to", true, "Directory with Kotlin sources") - .addOption("p", "public-only", false, "Only public and protected members") - .addOption("fqn", "fqn", false, "Full qualified names") - .addOption("d", "declarations-only", false, "Declarations only") - ; - - try { - CommandLine commandLine = parser.parse(options, args); - - if (commandLine.hasOption("help")) - showHelpAndExit(); - - if (commandLine.hasOption("from") && commandLine.hasOption("to")) { - String from = commandLine.getOptionValue("from"); - String to = commandLine.getOptionValue("to"); - - for (Option o : commandLine.getOptions()) { - Converter.addSetting(o.getLongOpt()); - } - - if (!from.isEmpty() && !to.isEmpty()) - convertSourceTree(from, to); - else - showHelpAndExit(); - } else - showHelpAndExit(); - } catch (ParseException e) { - e.printStackTrace(); - } - } - - @SuppressWarnings("ResultOfMethodCallIgnored") - private static void convertSourceTree(String javaPath, String kotlinPath) { - try { - File javaDir = new File(javaPath); - File kotlinDir = new File(kotlinPath); - - if (kotlinDir.exists()) - kotlinDir.delete(); - - if (!kotlinDir.exists() && !kotlinDir.mkdir()) - myLogger.warning("Creation failed: " + kotlinDir.getAbsolutePath()); - - for (File f : getJavaFiles(javaDir.getAbsolutePath())) { - String relative = javaDir.toURI().relativize(f.toURI()).getPath().replace(".java", ".kt"); - File file = new File(kotlinPath, relative); - - if (file.exists()) - file.delete(); - - if (f.isDirectory()) - if (!file.exists() && !file.mkdir()) - myLogger.warning("Creation failed: " + file.getAbsolutePath()); - - if (f.isFile()) { - writeStringToFile(file, fileToKotlin(f)); - } - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - @NotNull - private static String fileToKotlin(File f) throws IOException { - final String javaCode = readJavaFileToString(f); - return generateKotlinCode(JavaToKotlinTranslator.createFile(JavaToKotlinTranslator.setUpJavaCoreEnvironment(), javaCode)); - } - - @NotNull - private static String generateKotlinCode(@Nullable PsiFile file) { - if (file != null && file instanceof PsiJavaFile) { - JavaToKotlinTranslator.setClassIdentifiers(file); - return JavaToKotlinTranslator.prettify(Converter.fileToFile((PsiJavaFile) file).toKotlin()); - } - return ""; - } - - @NotNull - private static String readJavaFileToString(@NotNull File javaFile) throws IOException { - return Pattern.compile("\\s*/\\*.*\\*/", Pattern.DOTALL).matcher(readFileToString(javaFile)).replaceAll(""); - } - - private static void showHelpAndExit() { - System.err.println("Usage: java -jar java2kotlin.jar -f -t "); - System.exit(1); - } - - static public List getJavaFiles(String startDirName) throws FileNotFoundException { - return getJavaFiles(new File(startDirName)); - } - - private static List getJavaFiles(File start) throws FileNotFoundException { - List result = new ArrayList(); - - if (start.isFile()) - return Arrays.asList(start); - - for (File file : Arrays.asList(start.listFiles())) { - if ((file.isFile() && file.getName().endsWith(".java")) || file.isDirectory()) - result.add(file); - - if (file.isDirectory()) { - List deeperList = getJavaFiles(file); - result.addAll(deeperList); - } - } - return result; - } -} \ No newline at end of file +//package org.jetbrains.jet.j2k; +// +//import com.intellij.psi.PsiFile; +//import com.intellij.psi.PsiJavaFile; +//import org.apache.commons.cli.*; +//import org.jetbrains.annotations.NotNull; +//import org.jetbrains.annotations.Nullable; +// +//import java.io.File; +//import java.io.FileNotFoundException; +//import java.io.IOException; +//import java.util.ArrayList; +//import java.util.Arrays; +//import java.util.List; +//import java.util.logging.Logger; +//import java.util.regex.Pattern; +// +//import static org.apache.commons.io.FileUtils.readFileToString; +//import static org.apache.commons.io.FileUtils.writeStringToFile; +// +///** +// * @author ignatov +// */ +//@SuppressWarnings({"CallToPrintStackTrace", "UseOfSystemOutOrSystemErr"}) +//public class JavaToKotlinCli { +// private static final Logger myLogger = Logger.getAnonymousLogger(); +// +// private JavaToKotlinCli() { +// } +// +// public static void main(String[] args) { +// CommandLineParser parser = new BasicParser(); +// Options options = new Options() +// .addOption("h", "help", false, "Print usage information") +// .addOption("f", "from", true, "Directory with Java sources") +// .addOption("t", "to", true, "Directory with Kotlin sources") +// .addOption("p", "public-only", false, "Only public and protected members") +// .addOption("fqn", "fqn", false, "Full qualified names") +// .addOption("d", "declarations-only", false, "Declarations only") +// ; +// +// try { +// CommandLine commandLine = parser.parse(options, args); +// +// if (commandLine.hasOption("help")) +// showHelpAndExit(); +// +// if (commandLine.hasOption("from") && commandLine.hasOption("to")) { +// String from = commandLine.getOptionValue("from"); +// String to = commandLine.getOptionValue("to"); +// +// for (Option o : commandLine.getOptions()) { +// Converter.addSetting(o.getLongOpt()); +// } +// +// if (!from.isEmpty() && !to.isEmpty()) +// convertSourceTree(from, to); +// else +// showHelpAndExit(); +// } else +// showHelpAndExit(); +// } catch (ParseException e) { +// e.printStackTrace(); +// } +// } +// +// @SuppressWarnings("ResultOfMethodCallIgnored") +// private static void convertSourceTree(String javaPath, String kotlinPath) { +// try { +// File javaDir = new File(javaPath); +// File kotlinDir = new File(kotlinPath); +// +// if (kotlinDir.exists()) +// kotlinDir.delete(); +// +// if (!kotlinDir.exists() && !kotlinDir.mkdir()) +// myLogger.warning("Creation failed: " + kotlinDir.getAbsolutePath()); +// +// for (File f : getJavaFiles(javaDir.getAbsolutePath())) { +// String relative = javaDir.toURI().relativize(f.toURI()).getPath().replace(".java", ".kt"); +// File file = new File(kotlinPath, relative); +// +// if (file.exists()) +// file.delete(); +// +// if (f.isDirectory()) +// if (!file.exists() && !file.mkdir()) +// myLogger.warning("Creation failed: " + file.getAbsolutePath()); +// +// if (f.isFile()) { +// writeStringToFile(file, fileToKotlin(f)); +// } +// } +// } catch (FileNotFoundException e) { +// e.printStackTrace(); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// } +// +// @NotNull +// private static String fileToKotlin(File f) throws IOException { +// final String javaCode = readJavaFileToString(f); +// return generateKotlinCode(JavaToKotlinTranslator.createFile(JavaToKotlinTranslator.setUpJavaCoreEnvironment(), javaCode)); +// } +// +// @NotNull +// private static String generateKotlinCode(@Nullable PsiFile file) { +// if (file != null && file instanceof PsiJavaFile) { +// JavaToKotlinTranslator.setClassIdentifiers(file); +// return JavaToKotlinTranslator.prettify(Converter.fileToFile((PsiJavaFile) file).toKotlin()); +// } +// return ""; +// } +// +// @NotNull +// private static String readJavaFileToString(@NotNull File javaFile) throws IOException { +// return Pattern.compile("\\s*/\\*.*\\*/", Pattern.DOTALL).matcher(readFileToString(javaFile)).replaceAll(""); +// } +// +// private static void showHelpAndExit() { +// System.err.println("Usage: java -jar java2kotlin.jar -f -t "); +// System.exit(1); +// } +// +// static public List getJavaFiles(String startDirName) throws FileNotFoundException { +// return getJavaFiles(new File(startDirName)); +// } +// +// private static List getJavaFiles(File start) throws FileNotFoundException { +// List result = new ArrayList(); +// +// if (start.isFile()) +// return Arrays.asList(start); +// +// for (File file : Arrays.asList(start.listFiles())) { +// if ((file.isFile() && file.getName().endsWith(".java")) || file.isDirectory()) +// result.add(file); +// +// if (file.isDirectory()) { +// List deeperList = getJavaFiles(file); +// result.addAll(deeperList); +// } +// } +// return result; +// } +//} \ No newline at end of file diff --git a/j2k/test/org/jetbrains/jet/j2k/StandaloneJavaToKotlinConverterTest.java b/j2k/test/org/jetbrains/jet/j2k/StandaloneJavaToKotlinConverterTest.java index d901e64f04d..a168d150a46 100644 --- a/j2k/test/org/jetbrains/jet/j2k/StandaloneJavaToKotlinConverterTest.java +++ b/j2k/test/org/jetbrains/jet/j2k/StandaloneJavaToKotlinConverterTest.java @@ -1,6 +1,7 @@ package org.jetbrains.jet.j2k; import com.intellij.core.JavaCoreEnvironment; +import com.intellij.openapi.util.io.FileUtil; import com.intellij.psi.PsiFile; import com.intellij.psi.PsiJavaFile; import junit.framework.Assert; @@ -13,134 +14,146 @@ import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; -import static org.apache.commons.io.FileUtils.readFileToString; -import static org.apache.commons.io.FileUtils.writeStringToFile; import static org.jetbrains.jet.j2k.TestCaseBuilder.getTestDataPathBase; /** * @author ignatov */ public class StandaloneJavaToKotlinConverterTest extends TestCase { - private final String myDataPath; - private final String myName; - @NotNull - private final JavaCoreEnvironment myJavaCoreEnvironment; + private final String myDataPath; + private final String myName; + @NotNull + private final JavaCoreEnvironment myJavaCoreEnvironment; - public StandaloneJavaToKotlinConverterTest(String dataPath, String name) { - myDataPath = dataPath; - myName = name; - myJavaCoreEnvironment = JavaToKotlinTranslator.setUpJavaCoreEnvironment(); - } - - @Override - protected void runTest() throws Throwable { - String javaPath = "testData" + File.separator + getTestFilePath(); - String kotlinPath = javaPath.replace(".jav", ".kt"); - - final File kotlinFile = new File(kotlinPath); - if (!kotlinFile.exists()) - writeStringToFile(kotlinFile, ""); - final String expected = readFileToString(kotlinFile); - final File javaFile = new File(javaPath); - final String javaCode = readFileToString(javaFile); - - String actual = ""; - if (javaFile.getParent().endsWith("/expression")) actual = expressionToKotlin(javaCode); - else if (javaFile.getParent().endsWith("/statement")) actual = statementToKotlin(javaCode); - else if (javaFile.getParent().endsWith("/method")) actual = methodToKotlin(javaCode); - else if (javaFile.getParent().endsWith("/class")) actual = fileToKotlin(javaCode); - else if (javaFile.getParent().endsWith("/file")) actual = fileToKotlin(javaCode); - else if (javaFile.getParent().endsWith("/comp")) actual = fileToFileWithCompatibilityImport(javaCode); - - assert !actual.isEmpty() : "Specify what is it: file, class, method, statement or expression"; - - final File tmp = new File(kotlinPath + ".tmp"); - if (!expected.equals(actual)) writeStringToFile(tmp, actual); - if (expected.equals(actual) && tmp.exists()) //noinspection ResultOfMethodCallIgnored - tmp.delete(); - - Assert.assertEquals(expected, actual); - } - - @NotNull - String getTestFilePath() { - return myDataPath + File.separator + myName + ".jav"; - } - - - @NotNull - @Override - public String getName() { - return "test_" + myName; - } - - @NotNull - public static Test suite() { - TestSuite suite = new TestSuite(); - suite.addTest(TestCaseBuilder.suiteForDirectory(getTestDataPathBase(), "/ast", new TestCaseBuilder.NamedTestFactory() { - @NotNull - @Override - public Test createTest(@NotNull String dataPath, @NotNull String name) { - return new StandaloneJavaToKotlinConverterTest(dataPath, name); - } - })); - return suite; - } - - @NotNull - private static String fileToFileWithCompatibilityImport(@NotNull String text) { - return JavaToKotlinTranslator.generateKotlinCodeWithCompatibilityImport(text); - } - - @NotNull - private String fileToKotlin(@NotNull String text) { - return generateKotlinCode(JavaToKotlinTranslator.createFile(myJavaCoreEnvironment, text)); - } - - @NotNull - private static String generateKotlinCode(@Nullable PsiFile file) { - if (file != null && file instanceof PsiJavaFile) { - JavaToKotlinTranslator.setClassIdentifiers(file); - return prettify(Converter.elementToKotlin(file)); + public StandaloneJavaToKotlinConverterTest(String dataPath, String name) { + myDataPath = dataPath; + myName = name; + myJavaCoreEnvironment = JavaToKotlinTranslator.setUpJavaCoreEnvironment(); } - return ""; - } - @NotNull - private String methodToKotlin(String text) throws IOException { - String result = fileToKotlin("final class C {" + text + "}") - .replaceAll("class C\\(\\) \\{", ""); - result = result.substring(0, result.lastIndexOf("}")); - return prettify(result); - } + @Override + protected void runTest() throws Throwable { + String javaPath = "testData" + File.separator + getTestFilePath(); + String kotlinPath = javaPath.replace(".jav", ".kt"); - @NotNull - private String statementToKotlin(String text) throws Exception { - String result = methodToKotlin("void main() {" + text + "}"); - int pos = result.lastIndexOf("}"); - result = result.substring(0, pos).replaceFirst("fun main\\(\\) : Unit \\{", ""); - return prettify(result); - } + final File kotlinFile = new File(kotlinPath); + if (!kotlinFile.exists()) { + FileUtil.writeToFile(kotlinFile, ""); + } + final String expected = FileUtil.loadFile(kotlinFile); + final File javaFile = new File(javaPath); + final String javaCode = FileUtil.loadFile(javaFile); - @NotNull - private String expressionToKotlin(String code) throws Exception { - String result = statementToKotlin("Object o =" + code + "}"); - result = result.replaceFirst("var o : Any\\? =", ""); - return prettify(result); - } + String actual = ""; + if (javaFile.getParent().endsWith("/expression")) { + actual = expressionToKotlin(javaCode); + } + else if (javaFile.getParent().endsWith("/statement")) { + actual = statementToKotlin(javaCode); + } + else if (javaFile.getParent().endsWith("/method")) { + actual = methodToKotlin(javaCode); + } + else if (javaFile.getParent().endsWith("/class")) { + actual = fileToKotlin(javaCode); + } + else if (javaFile.getParent().endsWith("/file")) { + actual = fileToKotlin(javaCode); + } + else if (javaFile.getParent().endsWith("/comp")) actual = fileToFileWithCompatibilityImport(javaCode); - @NotNull - private static String prettify(@Nullable String code) { - if (code == null) - return ""; - return code - .trim() - .replaceAll("\r\n", "\n") - .replaceAll(" \n", "\n") - .replaceAll("\n ", "\n") - .replaceAll("\n+", "\n") - .replaceAll(" +", " ") - .trim() - ; - } + assert !actual.isEmpty() : "Specify what is it: file, class, method, statement or expression"; + + final File tmp = new File(kotlinPath + ".tmp"); + if (!expected.equals(actual)) FileUtil.writeToFile(tmp, actual); + if (expected.equals(actual) && tmp.exists()) //noinspection ResultOfMethodCallIgnored + { + tmp.delete(); + } + + Assert.assertEquals(expected, actual); + } + + @NotNull + String getTestFilePath() { + return myDataPath + File.separator + myName + ".jav"; + } + + + @NotNull + @Override + public String getName() { + return "test_" + myName; + } + + @NotNull + public static Test suite() { + TestSuite suite = new TestSuite(); + suite.addTest(TestCaseBuilder.suiteForDirectory(getTestDataPathBase(), "/ast", new TestCaseBuilder.NamedTestFactory() { + @NotNull + @Override + public Test createTest(@NotNull String dataPath, @NotNull String name) { + return new StandaloneJavaToKotlinConverterTest(dataPath, name); + } + })); + return suite; + } + + @NotNull + private static String fileToFileWithCompatibilityImport(@NotNull String text) { + return JavaToKotlinTranslator.generateKotlinCodeWithCompatibilityImport(text); + } + + @NotNull + private String fileToKotlin(@NotNull String text) { + return generateKotlinCode(JavaToKotlinTranslator.createFile(myJavaCoreEnvironment, text)); + } + + @NotNull + private static String generateKotlinCode(@Nullable PsiFile file) { + if (file != null && file instanceof PsiJavaFile) { + JavaToKotlinTranslator.setClassIdentifiers(file); + return prettify(Converter.elementToKotlin(file)); + } + return ""; + } + + @NotNull + private String methodToKotlin(String text) throws IOException { + String result = fileToKotlin("final class C {" + text + "}") + .replaceAll("class C\\(\\) \\{", ""); + result = result.substring(0, result.lastIndexOf("}")); + return prettify(result); + } + + @NotNull + private String statementToKotlin(String text) throws Exception { + String result = methodToKotlin("void main() {" + text + "}"); + int pos = result.lastIndexOf("}"); + result = result.substring(0, pos).replaceFirst("fun main\\(\\) : Unit \\{", ""); + return prettify(result); + } + + @NotNull + private String expressionToKotlin(String code) throws Exception { + String result = statementToKotlin("Object o =" + code + "}"); + result = result.replaceFirst("var o : Any\\? =", ""); + return prettify(result); + } + + @NotNull + private static String prettify(@Nullable String code) { + if (code == null) { + return ""; + } + return code + .trim() + .replaceAll("\r\n", "\n") + .replaceAll(" \n", "\n") + .replaceAll("\n ", "\n") + .replaceAll("\n+", "\n") + .replaceAll(" +", " ") + .trim() + ; + } } \ No newline at end of file