From 84dc88e61dae5cd11f99a4a51250eb703edb734a Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Tue, 28 Jun 2022 17:15:54 +0530 Subject: [PATCH 01/22] convert DebugActivity to kotlin --- .../com/pranav/java/ide/DebugActivity.java | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/pranav/java/ide/DebugActivity.java b/app/src/main/java/com/pranav/java/ide/DebugActivity.java index 8e6f5ce54..c2944979c 100644 --- a/app/src/main/java/com/pranav/java/ide/DebugActivity.java +++ b/app/src/main/java/com/pranav/java/ide/DebugActivity.java @@ -1,33 +1,32 @@ -package com.pranav.java.ide; +package com.pranav.java.ide -import android.content.ClipData; -import android.content.ClipboardManager; -import android.os.Bundle; +import android.content.ClipData +import android.content.ClipboardManager +import android.os.Bundle -import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatActivity -import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.dialog.MaterialAlertDialogBuilder -public final class DebugActivity extends AppCompatActivity { +class DebugActivity : AppCompatActivity() { - @Override - protected void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + override fun onCreate(val savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) - var intent = getIntent(); - var error = intent.getStringExtra("error"); + val intent = getIntent() + val error = intent?.getStringExtra("error")!! - new MaterialAlertDialogBuilder(DebugActivity.this) + MaterialAlertDialogBuilder(this) .setTitle("An error occurred...") .setMessage(error) - .setPositiveButton("QUIT", (dialog, which) -> finish()) + .setPositiveButton("QUIT", { _, _ -> finish()}) .setNegativeButton( "COPY", - (dialog, which) -> { - ((ClipboardManager) getSystemService(CLIPBOARD_SERVICE)) - .setPrimaryClip(ClipData.newPlainText("clipboard", error)); + { _, _ -> + (getSystemService(CLIPBOARD_SERVICE) as ClipboardManager) + .setPrimaryClip(ClipData.newPlainText("clipboard", error)) }) .create() - .show(); + .show() } } From 14b99c7c092b1f2a0a716de7f6a74bc37875e5a4 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Tue, 28 Jun 2022 17:18:33 +0530 Subject: [PATCH 02/22] change extension to kt --- .../com/pranav/java/ide/{DebugActivity.java => DebugActivity.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/src/main/java/com/pranav/java/ide/{DebugActivity.java => DebugActivity.kt} (100%) diff --git a/app/src/main/java/com/pranav/java/ide/DebugActivity.java b/app/src/main/java/com/pranav/java/ide/DebugActivity.kt similarity index 100% rename from app/src/main/java/com/pranav/java/ide/DebugActivity.java rename to app/src/main/java/com/pranav/java/ide/DebugActivity.kt From c5ef5db37b6d28aae130b08b29c3e6d8ea2da6ee Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Tue, 28 Jun 2022 17:23:42 +0530 Subject: [PATCH 03/22] No Commit Message --- app/src/main/java/com/pranav/java/ide/DebugActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/pranav/java/ide/DebugActivity.kt b/app/src/main/java/com/pranav/java/ide/DebugActivity.kt index c2944979c..b9c2555a8 100644 --- a/app/src/main/java/com/pranav/java/ide/DebugActivity.kt +++ b/app/src/main/java/com/pranav/java/ide/DebugActivity.kt @@ -10,7 +10,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder class DebugActivity : AppCompatActivity() { - override fun onCreate(val savedInstanceState: Bundle?) { + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val intent = getIntent() From 839846b496f0f5daf00864526da7d6e82c755cce Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Tue, 28 Jun 2022 17:40:02 +0530 Subject: [PATCH 04/22] convert Indexer to kotlin --- .../com/pranav/java/ide/ApplicationLoader.kt | 2 +- .../main/java/com/pranav/common/Indexer.java | 71 ----------------- .../main/java/com/pranav/common/Indexer.kt | 78 +++++++++++++++++++ 3 files changed, 79 insertions(+), 72 deletions(-) delete mode 100644 common/src/main/java/com/pranav/common/Indexer.java create mode 100644 common/src/main/java/com/pranav/common/Indexer.kt diff --git a/app/src/main/java/com/pranav/java/ide/ApplicationLoader.kt b/app/src/main/java/com/pranav/java/ide/ApplicationLoader.kt index 08cb8c9dd..3498c296a 100644 --- a/app/src/main/java/com/pranav/java/ide/ApplicationLoader.kt +++ b/app/src/main/java/com/pranav/java/ide/ApplicationLoader.kt @@ -21,7 +21,7 @@ class ApplicationLoader : Application() { dpToPx.initalizeResources(mContext.getResources()) Thread.setDefaultUncaughtExceptionHandler { - thread, throwable -> + _, throwable -> val intent = Intent(getApplicationContext(), DebugActivity::class.java) intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) intent.putExtra("error", Log.getStackTraceString(throwable)) diff --git a/common/src/main/java/com/pranav/common/Indexer.java b/common/src/main/java/com/pranav/common/Indexer.java deleted file mode 100644 index 870df7ae2..000000000 --- a/common/src/main/java/com/pranav/common/Indexer.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.pranav.common; - -import com.pranav.common.util.FileUtil; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.File; -import java.io.IOException; - -public class Indexer { - - private JSONObject json; - - private String filePath; - - public Indexer(String fileName) throws JSONException { - filePath = FileUtil.getCacheDir() + fileName + ".json"; - load(); - } - - public void load() throws JSONException { - var indexFile = new File(filePath); - try { - if (!indexFile.exists()) { - FileUtil.writeFile(filePath, "{}"); - } - var index = FileUtil.readFile(indexFile); - json = new JSONObject(index); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public Indexer put(String key, String value) throws JSONException { - json.put(key, value); - return this; - } - - public Indexer put(String key, long value) throws JSONException { - json.put(key, value); - return this; - } - - public boolean notHas(String key) throws JSONException { - return !json.has(key); - } - - public String getString(String key) throws JSONException { - return json.getString(key); - } - - public long getLong(String key) { - try { - return json.getLong(key); - } catch (JSONException e) { - return 0; - } - } - - public String asString() throws JSONException { - return json.toString(4); - } - - public void flush() { - try { - FileUtil.writeFile(filePath, asString()); - } catch (Exception e) { - } - } -} diff --git a/common/src/main/java/com/pranav/common/Indexer.kt b/common/src/main/java/com/pranav/common/Indexer.kt new file mode 100644 index 000000000..f6021162e --- /dev/null +++ b/common/src/main/java/com/pranav/common/Indexer.kt @@ -0,0 +1,78 @@ +package com.pranav.common + +import com.pranav.common.util.FileUtil + +import org.json.JSONException +import org.json.JSONObject + +import java.io.File +import java.io.IOException + +class Indexer { + + private var JSONObject: json + + private var filePath: String + + @Throws(JSONException::class) + constructor(fileName: String) { + filePath = FileUtil.getCacheDir() + fileName + ".json" + load() + } + + @Throws(JSONException::class) + fun load() { + val indexFile = File(filePath) + try { + if (!indexFile.exists()) { + FileUtil.writeFile(filePath, "{}") + } + val index = FileUtil.readFile(indexFile) + json = JSONObject(index) + } catch (e: IOException) { + e.printStackTrace() + } + } + + @Throws(JSONException::class) + fun put(key: String, value: String) : Indexer { + json.put(key, value) + return this + } + + @Throws(JSONException::class) + fun put(key: String, value: Long) : Indexer { + json.put(key, value) + return this + } + + @Throws(JSONException::class) + fun notHas(key: String) : Boolean { + return !json.has(key) + } + + @Throws(JSONException::class) + fun getString(key: String) : String { + return json.getString(key) + } + + fun getLong(key: String) : Long { + try { + return json.getLong(key) + } catch (e: JSONException) { + return 0 + } + } + + @Throws(JSONException::class) + fun asString() : String { + return json.toString(4) + } + + fun flush() { + try { + FileUtil.writeFile(filePath, asString()) + } catch (e: Exception) { + } + } +} From cebbd4e5fbd76d697bdcea8333638e1f766f1598 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Tue, 28 Jun 2022 17:44:07 +0530 Subject: [PATCH 05/22] fix CI --- common/src/main/java/com/pranav/common/Indexer.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/com/pranav/common/Indexer.kt b/common/src/main/java/com/pranav/common/Indexer.kt index f6021162e..504ccbb05 100644 --- a/common/src/main/java/com/pranav/common/Indexer.kt +++ b/common/src/main/java/com/pranav/common/Indexer.kt @@ -10,7 +10,7 @@ import java.io.IOException class Indexer { - private var JSONObject: json + private var json: JSONObject private var filePath: String From b177c3d550346f801490bfff4e5bcf7cf9a81870 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Tue, 28 Jun 2022 17:48:58 +0530 Subject: [PATCH 06/22] add lateinit modifier --- common/src/main/java/com/pranav/common/Indexer.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/com/pranav/common/Indexer.kt b/common/src/main/java/com/pranav/common/Indexer.kt index 504ccbb05..87e36e0ce 100644 --- a/common/src/main/java/com/pranav/common/Indexer.kt +++ b/common/src/main/java/com/pranav/common/Indexer.kt @@ -10,7 +10,7 @@ import java.io.IOException class Indexer { - private var json: JSONObject + private lateinit var json: JSONObject private var filePath: String From 34da65f0d38d8d07320f903d031ea2ce55b06fbc Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Tue, 28 Jun 2022 19:52:01 +0530 Subject: [PATCH 07/22] convert 3 more files to kotlin --- .../treeview/binder/TreeFileNodeViewBinder.kt | 4 +- .../helper/TreeCreateNewFileContent.java | 37 --- .../helper/TreeCreateNewFileContent.kt | 35 +++ .../ide/ui/treeview/model/TreeFolder.java | 22 -- .../java/ide/ui/treeview/model/TreeFolder.kt | 22 ++ .../pranav/common/util/DiagnosticWrapper.java | 253 ------------------ .../com/pranav/common/util/Environment.java | 14 - .../com/pranav/common/util/Environment.kt | 15 ++ 8 files changed, 73 insertions(+), 329 deletions(-) delete mode 100644 app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.java create mode 100644 app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.kt delete mode 100644 app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.java create mode 100644 app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.kt delete mode 100644 common/src/main/java/com/pranav/common/util/DiagnosticWrapper.java delete mode 100644 common/src/main/java/com/pranav/common/util/Environment.java create mode 100644 common/src/main/java/com/pranav/common/util/Environment.kt diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/binder/TreeFileNodeViewBinder.kt b/app/src/main/java/com/pranav/java/ide/ui/treeview/binder/TreeFileNodeViewBinder.kt index d95257763..eaa195512 100644 --- a/app/src/main/java/com/pranav/java/ide/ui/treeview/binder/TreeFileNodeViewBinder.kt +++ b/app/src/main/java/com/pranav/java/ide/ui/treeview/binder/TreeFileNodeViewBinder.kt @@ -19,9 +19,7 @@ class TreeFileNodeViewBinder( private lateinit var viewHolder: ViewHolder - public fun bnd(): BaseNodeViewBinder { - return this - } + fun bnd(): BaseNodeViewBinder = this override fun bindView(treeNode: TreeNode) { viewHolder = ViewHolder(itemView) diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.java b/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.java deleted file mode 100644 index 44a20a0db..000000000 --- a/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.pranav.java.ide.ui.treeview.helper; - -import androidx.annotation.NonNull; - -public class TreeCreateNewFileContent { - - public static String BUILD_NEW_FILE_CONTENT(@NonNull String fileName) { - String content = - "import java.util.*;\n\n" - + "public class " - + fileName - + " {\n\n" - + " public static void main(String[] args) {\n" - + " System.out.println(\"Hello, World!\");\n" - + " }\n" - + "}\n"; - - return content; - } - - public static String BUILD_NEW_FILE_CONTENT_EXTEND_PACKAGE( - @NonNull String fileName, String extendPackage) { - String content = - "package " - + extendPackage - + ";\n\nimport java.util.*;\n\n" - + "public class " - + fileName - + " {\n\n" - + " public static void main(String[] args) {\n" - + " System.out.print(\"Hello, World!\");\n" - + " }\n" - + "}\n"; - - return content; - } -} diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.kt b/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.kt new file mode 100644 index 000000000..9435662db --- /dev/null +++ b/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.kt @@ -0,0 +1,35 @@ +package com.pranav.java.ide.ui.treeview.helper + +import androidx.annotation.NonNull + +object TreeCreateNewFileContent { + + fun BUILD_NEW_FILE_CONTENT(@NonNull fileName: String) : String { + return + """ +import java.util.ArrayList; + +public class $fileName { + public static void main(String[] args) { + System.out.println(\"Hello, World!\"); + } +} + """ + } + + fun BUILD_NEW_FILE_CONTENT_EXTEND_PACKAGE( + @NonNull fileName: String, extendPackage: String) : String { + return + """ +package $extendPackage; + +import java.util.*; + +public class $fileName { + public static void main(String[] args) { + System.out.print(\"Hello, World!\"); + }\n" +} + """ + } +} diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.java b/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.java deleted file mode 100644 index d1f657cd8..000000000 --- a/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.pranav.java.ide.ui.treeview.model; - -import android.content.Context; -import android.graphics.drawable.Drawable; - -import androidx.appcompat.content.res.AppCompatResources; - -import com.pranav.java.ide.R; - -import java.io.File; - -public class TreeFolder extends com.pranav.java.ide.ui.treeview.file.TreeFile { - - public TreeFolder(File file) { - super(file); - } - - @Override - public Drawable getIcon(Context context) { - return AppCompatResources.getDrawable(context, R.drawable.folder); - } -} diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.kt b/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.kt new file mode 100644 index 000000000..dbae5dbc5 --- /dev/null +++ b/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.kt @@ -0,0 +1,22 @@ +package com.pranav.java.ide.ui.treeview.model + +import android.content.Context +import android.graphics.drawable.Drawable + +import androidx.appcompat.content.res.AppCompatResources + +import com.pranav.java.ide.R +import com.pranav.java.ide.ui.treeview.file.TreeFile + +import java.io.File + +class TreeFolder : TreeFile() { + + constructor(file: File) { + super(file) + } + + override fun getIcon(context: Context) { + return AppCompatResources.getDrawable(context, R.drawable.folder) + } +} diff --git a/common/src/main/java/com/pranav/common/util/DiagnosticWrapper.java b/common/src/main/java/com/pranav/common/util/DiagnosticWrapper.java deleted file mode 100644 index 24e0a30ae..000000000 --- a/common/src/main/java/com/pranav/common/util/DiagnosticWrapper.java +++ /dev/null @@ -1,253 +0,0 @@ -package com.pranav.common.util; - -import java.io.File; -import java.util.Locale; -import java.util.Objects; - -import javax.tools.Diagnostic; -import javax.tools.JavaFileObject; - -public class DiagnosticWrapper implements Diagnostic { - - public static final int USE_LINE_POS = -31; - - private String code; - private File source; - private Kind kind; - - private long position; - private long startPosition; - private long endPosition; - - private long lineNumber; - private long columnNumber; - private CharSequence message; - - /** Extra information for this diagnostic */ - private Object mExtra; - - private int startLine; - private int endLine; - private int startColumn; - private int endColumn; - - public DiagnosticWrapper(Diagnostic obj) { - try { - this.code = obj.getCode(); - if (obj.getSource() != null) { - this.source = new File(obj.getSource().toUri()); - } - this.kind = obj.getKind(); - - this.position = obj.getPosition(); - this.startPosition = obj.getStartPosition(); - this.endPosition = obj.getEndPosition(); - - this.lineNumber = obj.getLineNumber(); - this.columnNumber = obj.getColumnNumber(); - - this.message = obj.getMessage(Locale.getDefault()); - - this.mExtra = obj; - } catch (Throwable e) { - // ignored - } - } - - @Override - public Kind getKind() { - return kind; - } - - @Override - public File getSource() { - return source; - } - - @Override - public long getPosition() { - return position; - } - - @Override - public long getStartPosition() { - return startPosition; - } - - @Override - public long getEndPosition() { - return endPosition; - } - - @Override - public long getLineNumber() { - return lineNumber; - } - - @Override - public long getColumnNumber() { - return columnNumber; - } - - @Override - public String getCode() { - return code; - } - - @Override - public String getMessage(Locale locale) { - return message.toString(); - } - - public CharSequence getMessageCharSequence() { - return message; - } - - public void setCode(String code) { - this.code = code; - } - - public void setSource(File source) { - this.source = source; - } - - public void setKind(Kind kind) { - this.kind = kind; - } - - public void setPosition(long position) { - this.position = position; - } - - public void setStartPosition(long startPosition) { - this.startPosition = startPosition; - } - - public void setEndPosition(long endPosition) { - this.endPosition = endPosition; - } - - public void setMessage(CharSequence message) { - this.message = message; - } - - public void setLineNumber(long lineNumber) { - this.lineNumber = lineNumber; - } - - public void setColumnNumber(long columnNumber) { - this.columnNumber = columnNumber; - } - - public Object getExtra() { - return mExtra; - } - - public void setExtra(Object mExtra) { - this.mExtra = mExtra; - } - - @Override - public String toString() { - return "startOffset: " - + startPosition - + "\n" - + "endOffset: " - + endPosition - + "\n" - + "position: " - + position - + "\n" - + "startLine: " - + startLine - + "\n" - + "startColumn: " - + startColumn - + "\n" - + "endLine: " - + endLine - + "\n" - + "endColumn: " - + endColumn - + "\n" - + "message: " - + message; - } - - @Override - public int hashCode() { - return Objects.hash( - code, - source, - kind, - position, - startPosition, - endPosition, - lineNumber, - columnNumber, - message, - mExtra); - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof DiagnosticWrapper) { - DiagnosticWrapper that = (DiagnosticWrapper) obj; - - if (that.message != null && this.message == null) { - return false; - } - - if (that.message == null && this.message != null) { - return false; - } - - if (!Objects.equals(that.message, this.message)) { - return false; - } - - if (!Objects.equals(that.source, this.source)) { - return false; - } - - if (that.lineNumber != this.lineNumber) { - return false; - } - - return that.columnNumber == this.columnNumber; - } - return super.equals(obj); - } - - public void setStartLine(int line) { - this.startLine = line; - } - - public int getStartLine() { - return startLine; - } - - public int getEndLine() { - return endLine; - } - - public void setEndLine(int endLine) { - this.endLine = endLine; - } - - public int getStartColumn() { - return startColumn; - } - - public void setStartColumn(int startColumn) { - this.startColumn = startColumn; - } - - public int getEndColumn() { - return endColumn; - } - - public void setEndColumn(int endColumn) { - this.endColumn = endColumn; - } -} diff --git a/common/src/main/java/com/pranav/common/util/Environment.java b/common/src/main/java/com/pranav/common/util/Environment.java deleted file mode 100644 index 5e4fef4de..000000000 --- a/common/src/main/java/com/pranav/common/util/Environment.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.itsaky.androidide.utils; - -import java.io.File; - -/* - * Required by nb-javac-android - */ -public class Environment { - public static File COMPILER_MODULE; - - public static void init(File f) { - COMPILER_MODULE = f; - } -} diff --git a/common/src/main/java/com/pranav/common/util/Environment.kt b/common/src/main/java/com/pranav/common/util/Environment.kt new file mode 100644 index 000000000..c9c68ee38 --- /dev/null +++ b/common/src/main/java/com/pranav/common/util/Environment.kt @@ -0,0 +1,15 @@ +package com.itsaky.androidide.utils + +import java.io.File + +/* + * Required by nb-javac-android + */ + object Environment { + @JvmStatic + lateinit var COMPILER_MODULE: File + + fun init(f: File) { + COMPILER_MODULE = f + } +} From a983b347d98c5f9be009a5cb33396cca3ba1aa95 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Tue, 28 Jun 2022 20:42:15 +0530 Subject: [PATCH 08/22] Use object class instead of a companion one for static methods --- .../java/com/pranav/common/util/ConcurrentUtil.kt | 13 +++++-------- .../main/java/com/pranav/common/util/Environment.kt | 1 + 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/com/pranav/common/util/ConcurrentUtil.kt b/common/src/main/java/com/pranav/common/util/ConcurrentUtil.kt index 42f54a88c..9d9242f35 100644 --- a/common/src/main/java/com/pranav/common/util/ConcurrentUtil.kt +++ b/common/src/main/java/com/pranav/common/util/ConcurrentUtil.kt @@ -1,20 +1,17 @@ package com.pranav.common.util import kotlinx.coroutines.* -import java.util.concurrent.Executors; -class ConcurrentUtil { - companion object { +object ConcurrentUtil { @JvmStatic fun execute(runnable: Runnable) = runBlocking { - launch { - runnable.run() - } + launch { + runnable.run() + } } @JvmStatic fun inParallel(runnable: Runnable) = GlobalScope.launch(Dispatchers.Default) { - runnable.run() + runnable.run() } - } } diff --git a/common/src/main/java/com/pranav/common/util/Environment.kt b/common/src/main/java/com/pranav/common/util/Environment.kt index c9c68ee38..e20824fe8 100644 --- a/common/src/main/java/com/pranav/common/util/Environment.kt +++ b/common/src/main/java/com/pranav/common/util/Environment.kt @@ -9,6 +9,7 @@ import java.io.File @JvmStatic lateinit var COMPILER_MODULE: File + @JvmStatic fun init(f: File) { COMPILER_MODULE = f } From 9b4ae0faa2994cabc1bda43e39aba6cbdc47aba3 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Tue, 28 Jun 2022 20:46:36 +0530 Subject: [PATCH 09/22] fix type mismatch --- .../ide/ui/treeview/helper/TreeCreateNewFileContent.kt | 8 ++------ .../com/pranav/java/ide/ui/treeview/model/TreeFolder.kt | 6 +++--- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.kt b/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.kt index 9435662db..30823738a 100644 --- a/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.kt +++ b/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.kt @@ -4,8 +4,7 @@ import androidx.annotation.NonNull object TreeCreateNewFileContent { - fun BUILD_NEW_FILE_CONTENT(@NonNull fileName: String) : String { - return + fun BUILD_NEW_FILE_CONTENT(@NonNull fileName: String) = """ import java.util.ArrayList; @@ -15,11 +14,9 @@ public class $fileName { } } """ - } fun BUILD_NEW_FILE_CONTENT_EXTEND_PACKAGE( - @NonNull fileName: String, extendPackage: String) : String { - return + @NonNull fileName: String, extendPackage: String) = """ package $extendPackage; @@ -31,5 +28,4 @@ public class $fileName { }\n" } """ - } } diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.kt b/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.kt index dbae5dbc5..7ad333ea1 100644 --- a/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.kt +++ b/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.kt @@ -10,13 +10,13 @@ import com.pranav.java.ide.ui.treeview.file.TreeFile import java.io.File -class TreeFolder : TreeFile() { +class TreeFolder(file: File) : TreeFile() { - constructor(file: File) { + init { super(file) } - override fun getIcon(context: Context) { + override fun getIcon(context: Context): Drawable { return AppCompatResources.getDrawable(context, R.drawable.folder) } } From 8a5476206f8e1a1ca8aac1170bac8fcbd55ccd59 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Tue, 28 Jun 2022 21:06:03 +0530 Subject: [PATCH 10/22] fix kotlin --- .../com/pranav/java/ide/ui/treeview/model/TreeFolder.kt | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.kt b/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.kt index 7ad333ea1..b43434155 100644 --- a/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.kt +++ b/app/src/main/java/com/pranav/java/ide/ui/treeview/model/TreeFolder.kt @@ -10,13 +10,9 @@ import com.pranav.java.ide.ui.treeview.file.TreeFile import java.io.File -class TreeFolder(file: File) : TreeFile() { +class TreeFolder(file: File) : TreeFile(file) { - init { - super(file) - } - - override fun getIcon(context: Context): Drawable { + override fun getIcon(context: Context): Drawable? { return AppCompatResources.getDrawable(context, R.drawable.folder) } } From 64c205b40b723378557e204d8978ec48e075eb1b Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Tue, 28 Jun 2022 21:35:11 +0530 Subject: [PATCH 11/22] fix CI --- .../java/ide/ui/treeview/helper/TreeCreateNewFileContent.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.kt b/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.kt index 30823738a..d9ec77fbe 100644 --- a/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.kt +++ b/app/src/main/java/com/pranav/java/ide/ui/treeview/helper/TreeCreateNewFileContent.kt @@ -4,6 +4,7 @@ import androidx.annotation.NonNull object TreeCreateNewFileContent { + @JvmStatic fun BUILD_NEW_FILE_CONTENT(@NonNull fileName: String) = """ import java.util.ArrayList; @@ -15,6 +16,7 @@ public class $fileName { } """ + @JvmStatic fun BUILD_NEW_FILE_CONTENT_EXTEND_PACKAGE( @NonNull fileName: String, extendPackage: String) = """ From 2589deb4c4a234fec2c39c45b891f488e9754072 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Tue, 28 Jun 2022 22:45:28 +0530 Subject: [PATCH 12/22] convert TreeFile to kotlin --- .../java/ide/ui/treeview/file/TreeFile.java | 62 ------------------- .../java/ide/ui/treeview/file/TreeFile.kt | 59 ++++++++++++++++++ 2 files changed, 59 insertions(+), 62 deletions(-) delete mode 100644 app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.java create mode 100644 app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.java b/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.java deleted file mode 100644 index bde02582c..000000000 --- a/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.pranav.java.ide.ui.treeview.file; - -import android.content.Context; -import android.graphics.drawable.Drawable; - -import androidx.annotation.Nullable; -import androidx.appcompat.content.res.AppCompatResources; - -import com.pranav.java.ide.R; -import com.pranav.java.ide.ui.treeview.model.TreeFolder; -import com.pranav.java.ide.ui.treeview.model.TreeJavaFile; - -import java.io.File; -import java.util.Objects; - -public class TreeFile { - - @Nullable - public static TreeFile fromFile(File file) { - if (file == null) { - return null; - } - if (file.isDirectory()) { - return new TreeFolder(file); - } - if (file.getName().endsWith(".java")) { - return new TreeJavaFile(file); - } - return new TreeFile(file); - } - - private final File mFile; - - public TreeFile(File file) { - mFile = file; - } - - public File getFile() { - return mFile; - } - - public Drawable getIcon(Context context) { - return AppCompatResources.getDrawable(context, R.drawable.java_file); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - var treeFile = (TreeFile) o; - return Objects.equals(mFile, treeFile.mFile); - } - - @Override - public int hashCode() { - return Objects.hash(mFile); - } -} diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt b/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt new file mode 100644 index 000000000..6e7f218f9 --- /dev/null +++ b/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt @@ -0,0 +1,59 @@ +package com.pranav.java.ide.ui.treeview.file + +import android.content.Context +import android.graphics.drawable.Drawable + +import androidx.annotation.Nullable +import androidx.appcompat.content.res.AppCompatResources + +import com.pranav.java.ide.R; +import com.pranav.java.ide.ui.treeview.model.TreeFolder +import com.pranav.java.ide.ui.treeview.model.TreeJavaFile + +import java.io.File +import java.util.Objects + +open class TreeFile { + + @Nullable + @JvmStatic + fun fromFile(file: File) : TreeFile? { + if (file == null) { + return null + } + if (file.isDirectory()) { + return TreeFolder(file) + } + if (file.getName().endsWith(".java")) { + return TreeJavaFile(file) + } + return TreeFile(file) + } + + private lateinit var mFile: File + + constructor(file: File) { + mFile = file + } + + fun getFile() = mFile + + fun getIcon(context: Context) : Drawable? { + return AppCompatResources.getDrawable(context, R.drawable.java_file) + } + + override fun equals(o: Object): Boolean { + if (this == o) { + return true + } + if (o == null || getClass() != o.getClass()) { + return false + } + val treeFile = o as TreeFile + return Objects.equals(mFile, treeFile.mFile) + } + + override fun hashCode() : Int { + return Objects.hash(mFile) + } +} From 87b41fa0ebd81295160932ac8a81b948f79381f0 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Tue, 28 Jun 2022 22:55:44 +0530 Subject: [PATCH 13/22] fix code --- .../treeview/binder/TreeFileNodeViewBinder.kt | 2 +- .../java/ide/ui/treeview/file/TreeFile.kt | 34 ++++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/binder/TreeFileNodeViewBinder.kt b/app/src/main/java/com/pranav/java/ide/ui/treeview/binder/TreeFileNodeViewBinder.kt index eaa195512..144cf5a37 100644 --- a/app/src/main/java/com/pranav/java/ide/ui/treeview/binder/TreeFileNodeViewBinder.kt +++ b/app/src/main/java/com/pranav/java/ide/ui/treeview/binder/TreeFileNodeViewBinder.kt @@ -34,7 +34,7 @@ class TreeFileNodeViewBinder( visibility = if (treeNode.isLeaf) View.INVISIBLE else View.VISIBLE } - val file = treeNode.content.file + val file = treeNode.content.getFile() viewHolder.dirName.text = file.name diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt b/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt index 6e7f218f9..ce18bbcd2 100644 --- a/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt +++ b/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt @@ -6,7 +6,7 @@ import android.graphics.drawable.Drawable import androidx.annotation.Nullable import androidx.appcompat.content.res.AppCompatResources -import com.pranav.java.ide.R; +import com.pranav.java.ide.R import com.pranav.java.ide.ui.treeview.model.TreeFolder import com.pranav.java.ide.ui.treeview.model.TreeJavaFile @@ -15,19 +15,21 @@ import java.util.Objects open class TreeFile { - @Nullable - @JvmStatic - fun fromFile(file: File) : TreeFile? { - if (file == null) { - return null + companion object { + @Nullable + @JvmStatic + fun fromFile(file: File) : TreeFile? { + if (file == null) { + return null + } + if (file.isDirectory()) { + return TreeFolder(file) + } + if (file.getName().endsWith(".java")) { + return TreeJavaFile(file) + } + return TreeFile(file) } - if (file.isDirectory()) { - return TreeFolder(file) - } - if (file.getName().endsWith(".java")) { - return TreeJavaFile(file) - } - return TreeFile(file) } private lateinit var mFile: File @@ -38,15 +40,15 @@ open class TreeFile { fun getFile() = mFile - fun getIcon(context: Context) : Drawable? { + open fun getIcon(context: Context) : Drawable? { return AppCompatResources.getDrawable(context, R.drawable.java_file) } - override fun equals(o: Object): Boolean { + override fun equals(o: Object): Boolean? { if (this == o) { return true } - if (o == null || getClass() != o.getClass()) { + if (o == null || this::class != o.getClass()) { return false } val treeFile = o as TreeFile From 7947b8e5baa73edb08704d9b0d7d75fdb95101a6 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Wed, 29 Jun 2022 09:45:50 +0530 Subject: [PATCH 14/22] fix CI --- .../main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt b/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt index ce18bbcd2..35578e7fe 100644 --- a/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt +++ b/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt @@ -44,7 +44,7 @@ open class TreeFile { return AppCompatResources.getDrawable(context, R.drawable.java_file) } - override fun equals(o: Object): Boolean? { + override fun equals(o: Any?): Boolean { if (this == o) { return true } From 9cbf3e968ef06994ca1e7cbc27b3dad2c8eda3a9 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Wed, 29 Jun 2022 09:48:01 +0530 Subject: [PATCH 15/22] use kotlin styled getClass method --- .../main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt b/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt index 35578e7fe..6e9789285 100644 --- a/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt +++ b/app/src/main/java/com/pranav/java/ide/ui/treeview/file/TreeFile.kt @@ -48,7 +48,7 @@ open class TreeFile { if (this == o) { return true } - if (o == null || this::class != o.getClass()) { + if (o == null || this::class != o::class) { return false } val treeFile = o as TreeFile From e88b6aab3ae76d57e65f76ee1a311338ba856415 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Wed, 29 Jun 2022 12:55:56 +0530 Subject: [PATCH 16/22] convert 5 more files to kotlin --- android-compiler/build.gradle | 7 ++++ .../disassembler/EclipseDisassembler.java | 22 ----------- .../code/disassembler/EclipseDisassembler.kt | 21 ++++++++++ .../code/disassembler/JavapDisassembler.java | 31 --------------- .../code/disassembler/JavapDisassembler.kt | 33 ++++++++++++++++ .../code/formatter/EclipseJavaFormatter.java | 39 ------------------- .../code/formatter/EclipseJavaFormatter.kt | 39 +++++++++++++++++++ .../code/formatter/GoogleJavaFormatter.java | 28 ------------- .../code/formatter/GoogleJavaFormatter.kt | 29 ++++++++++++++ .../com/pranav/android/task/JavaBuilder.java | 28 ------------- .../com/pranav/android/task/JavaBuilder.kt | 26 +++++++++++++ 11 files changed, 155 insertions(+), 148 deletions(-) delete mode 100644 android-compiler/src/main/java/com/pranav/android/code/disassembler/EclipseDisassembler.java create mode 100644 android-compiler/src/main/java/com/pranav/android/code/disassembler/EclipseDisassembler.kt delete mode 100644 android-compiler/src/main/java/com/pranav/android/code/disassembler/JavapDisassembler.java create mode 100644 android-compiler/src/main/java/com/pranav/android/code/disassembler/JavapDisassembler.kt delete mode 100644 android-compiler/src/main/java/com/pranav/android/code/formatter/EclipseJavaFormatter.java create mode 100644 android-compiler/src/main/java/com/pranav/android/code/formatter/EclipseJavaFormatter.kt delete mode 100644 android-compiler/src/main/java/com/pranav/android/code/formatter/GoogleJavaFormatter.java create mode 100644 android-compiler/src/main/java/com/pranav/android/code/formatter/GoogleJavaFormatter.kt delete mode 100644 android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.java create mode 100644 android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.kt diff --git a/android-compiler/build.gradle b/android-compiler/build.gradle index ff228872c..75e14dec5 100644 --- a/android-compiler/build.gradle +++ b/android-compiler/build.gradle @@ -1,5 +1,6 @@ plugins { id 'java-library' + id 'org.jetbrains.kotlin.jvm' id 'com.android.lint' } @@ -8,6 +9,12 @@ java { targetCompatibility = JavaVersion.VERSION_11 } +compileKotlin { + kotlinOptions { + jvmTarget = JavaVersion.VERSION_11 + } +} + dependencies { // r8 dependency implementation 'com.android.tools:r8:3.3.28' diff --git a/android-compiler/src/main/java/com/pranav/android/code/disassembler/EclipseDisassembler.java b/android-compiler/src/main/java/com/pranav/android/code/disassembler/EclipseDisassembler.java deleted file mode 100644 index c6cdc93a7..000000000 --- a/android-compiler/src/main/java/com/pranav/android/code/disassembler/EclipseDisassembler.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.pranav.android.code.disassembler; - -import org.eclipse.jdt.internal.core.util.Disassembler; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.InvalidPathException; -import java.nio.file.Paths; - -public class EclipseDisassembler { - - final byte[] classFileBytes; - - public EclipseDisassembler(String classFile) throws IOException, InvalidPathException { - - classFileBytes = Files.readAllBytes(Paths.get(classFile)); - } - - public String disassemble() throws Throwable { - return new Disassembler().disassemble(classFileBytes, System.lineSeparator()); - } -} diff --git a/android-compiler/src/main/java/com/pranav/android/code/disassembler/EclipseDisassembler.kt b/android-compiler/src/main/java/com/pranav/android/code/disassembler/EclipseDisassembler.kt new file mode 100644 index 000000000..0d6fd70af --- /dev/null +++ b/android-compiler/src/main/java/com/pranav/android/code/disassembler/EclipseDisassembler.kt @@ -0,0 +1,21 @@ +package com.pranav.android.code.disassembler + +import org.eclipse.jdt.internal.core.util.Disassembler + +import java.io.IOException +import java.nio.file.Files +import java.nio.file.Paths + +class EclipseDisassembler(filePath: String) { + + private lateinit var classFileBytes: ByteArray + + init { + classFileBytes = Files.readAllBytes(Paths.get(filePath)) + } + + @Throws(Throwable::class) + fun disassemble() : String { + return Disassembler().disassemble(classFileBytes, System.lineSeparator()) + } +} diff --git a/android-compiler/src/main/java/com/pranav/android/code/disassembler/JavapDisassembler.java b/android-compiler/src/main/java/com/pranav/android/code/disassembler/JavapDisassembler.java deleted file mode 100644 index 9512382dd..000000000 --- a/android-compiler/src/main/java/com/pranav/android/code/disassembler/JavapDisassembler.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.pranav.android.code.disassembler; - -import com.sun.tools.javap.JavapTask; - -import java.io.StringWriter; -import java.util.ArrayList; - -public class JavapDisassembler { - - final String path; - - public JavapDisassembler(String classFile) { - path = classFile; - } - - public String disassemble() throws Throwable { - // Create an arraylist for storing javap arguments - var args = new ArrayList(); - args.add("-c"); - args.add(path); - // Create a StringWriter object that will store the output - var writer = new StringWriter(); - // Create a JavapTask to handle the arguments - var task = new JavapTask(); - task.handleOptions(args.toArray(new String[0])); - task.setLog(writer); - task.run(); - // return the disassembled file as string - return writer.toString(); - } -} diff --git a/android-compiler/src/main/java/com/pranav/android/code/disassembler/JavapDisassembler.kt b/android-compiler/src/main/java/com/pranav/android/code/disassembler/JavapDisassembler.kt new file mode 100644 index 000000000..d4f4f5d10 --- /dev/null +++ b/android-compiler/src/main/java/com/pranav/android/code/disassembler/JavapDisassembler.kt @@ -0,0 +1,33 @@ +package com.pranav.android.code.disassembler + +import com.sun.tools.javap.JavapTask + +import java.io.StringWriter +import java.util.ArrayList + +class JavapDisassembler(filePath: String) { + + private val path: String + + init { + path = classFile + } + + @Throws(Throwable::class) + fun disassemble() : String { + // Create an arraylist for storing javap arguments + val args = listOf( + "-c", + path + ) + // Create a StringWriter object that will store the output + val writer = StringWriter() + // Create a JavapTask to handle the arguments + val task = JavapTask() + task.handleOptions(args.toTypedArray()) + task.setLog(writer) + task.run() + // return the disassembled file as string + return writer.toString() + } +} diff --git a/android-compiler/src/main/java/com/pranav/android/code/formatter/EclipseJavaFormatter.java b/android-compiler/src/main/java/com/pranav/android/code/formatter/EclipseJavaFormatter.java deleted file mode 100644 index 364fd862a..000000000 --- a/android-compiler/src/main/java/com/pranav/android/code/formatter/EclipseJavaFormatter.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.pranav.android.code.formatter; - -import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; -import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions; -import org.eclipse.jface.text.Document; - -public class EclipseJavaFormatter { - - private String source; - - public EclipseJavaFormatter(String source) { - this.source = source; - } - - public String format() { - var options = DefaultCodeFormatterOptions.getEclipseDefaultSettings(); - - var codeFormatter = new DefaultCodeFormatter(options); - - var edit = - codeFormatter.format( - DefaultCodeFormatter.K_COMPILATION_UNIT, - source, - 0, // starting index - source.length(), // length - 0, // initial indentation - System.lineSeparator() // line separator - ); - - var document = new Document(source); - try { - edit.apply(document); - } catch (Exception e) { - throw new IllegalStateException(e); - } - // return the formatted code - return document.get(); - } -} diff --git a/android-compiler/src/main/java/com/pranav/android/code/formatter/EclipseJavaFormatter.kt b/android-compiler/src/main/java/com/pranav/android/code/formatter/EclipseJavaFormatter.kt new file mode 100644 index 000000000..b05239b12 --- /dev/null +++ b/android-compiler/src/main/java/com/pranav/android/code/formatter/EclipseJavaFormatter.kt @@ -0,0 +1,39 @@ +package com.pranav.android.code.formatter + +import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter +import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions +import org.eclipse.jface.text.Document + +class EclipseJavaFormatter(code: String) { + + private val source: String + + init { + source = code; + } + + fun format(): String { + val options = DefaultCodeFormatterOptions.getEclipseDefaultSettings() + + val codeFormatter = DefaultCodeFormatter(options) + + val edit = + codeFormatter.format( + DefaultCodeFormatter.K_COMPILATION_UNIT, + source, + 0, // starting index + source.length, // length + 0, // initial indentation + System.lineSeparator() // line separator + ) + + val document = Document(source) + try { + edit.apply(document) + } catch (e: Exception) { + throw IllegalStateException(e) + } + // return the formatted code + return document.get() + } +} diff --git a/android-compiler/src/main/java/com/pranav/android/code/formatter/GoogleJavaFormatter.java b/android-compiler/src/main/java/com/pranav/android/code/formatter/GoogleJavaFormatter.java deleted file mode 100644 index 4e1498ad0..000000000 --- a/android-compiler/src/main/java/com/pranav/android/code/formatter/GoogleJavaFormatter.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.pranav.android.code.formatter; - -import com.google.googlejavaformat.java.Formatter; -import com.google.googlejavaformat.java.FormatterException; -import com.google.googlejavaformat.java.JavaFormatterOptions; - -public class GoogleJavaFormatter { - - private String source; - - public GoogleJavaFormatter(String source) { - this.source = source; - } - - public String format() { - var options = - JavaFormatterOptions.builder() - .style(JavaFormatterOptions.Style.AOSP) // Use AOSP formatting style - .formatJavadoc(true) // Format Javadoc with code - .build(); - var formatter = new Formatter(options); - try { - return formatter.formatSourceAndFixImports(source); - } catch (FormatterException e) { - throw new IllegalStateException(e); - } - } -} diff --git a/android-compiler/src/main/java/com/pranav/android/code/formatter/GoogleJavaFormatter.kt b/android-compiler/src/main/java/com/pranav/android/code/formatter/GoogleJavaFormatter.kt new file mode 100644 index 000000000..92e301e95 --- /dev/null +++ b/android-compiler/src/main/java/com/pranav/android/code/formatter/GoogleJavaFormatter.kt @@ -0,0 +1,29 @@ +package com.pranav.android.code.formatter + +import com.google.googlejavaformat.java.Formatter +import com.google.googlejavaformat.java.FormatterException +import com.google.googlejavaformat.java.JavaFormatterOptions + +class GoogleJavaFormatter(input: String) { + + private val source: String + + init { + source = input + } + + fun format(): String { + val options = + JavaFormatterOptions.builder() + .style(JavaFormatterOptions.Style.AOSP) // Use AOSP formatting style + .formatJavadoc(true) // Format Javadoc with code + .build() + val formatter = Formatter(options) + try { + return formatter.formatSourceAndFixImports(source) + } catch (e: Exception) { + e.printStackTrace() + } + return source + } +} diff --git a/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.java b/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.java deleted file mode 100644 index 27da34ad4..000000000 --- a/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.pranav.android.task; - -import android.content.Context; - -import com.pranav.android.interfaces.*; -import com.pranav.android.task.java.*; - -public class JavaBuilder extends Builder { - - public final ClassLoader classloader; - - public Context mContext; - - public JavaBuilder(Context context, ClassLoader loader) { - mContext = context; - classloader = loader; - } - - @Override - public Context getContext() { - return mContext; - } - - @Override - public ClassLoader getClassloader() { - return this.classloader; - } -} diff --git a/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.kt b/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.kt new file mode 100644 index 000000000..c8ab118c5 --- /dev/null +++ b/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.kt @@ -0,0 +1,26 @@ +package com.pranav.android.task + +import android.content.Context + +import com.pranav.android.interfaces.* +import com.pranav.android.task.java.* + +class JavaBuilder : Builder() { + + private var classloader: ClassLoader + + public var mContext: Context + + contructor(context: Context, loader: ClassLoader) { + mContext = context + classloader = loader + } + + override fun getContext() : Context { + return mContext + } + + override fun getClassloader() : ClassLoader { + return classloader + } +} From 0be7c3968de4c6e6372dace2c81ab8844da2921a Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Wed, 29 Jun 2022 13:10:52 +0530 Subject: [PATCH 17/22] use primary constructor --- .../pranav/android/code/disassembler/JavapDisassembler.kt | 2 +- .../src/main/java/com/pranav/android/task/JavaBuilder.kt | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/android-compiler/src/main/java/com/pranav/android/code/disassembler/JavapDisassembler.kt b/android-compiler/src/main/java/com/pranav/android/code/disassembler/JavapDisassembler.kt index d4f4f5d10..a02f05e30 100644 --- a/android-compiler/src/main/java/com/pranav/android/code/disassembler/JavapDisassembler.kt +++ b/android-compiler/src/main/java/com/pranav/android/code/disassembler/JavapDisassembler.kt @@ -10,7 +10,7 @@ class JavapDisassembler(filePath: String) { private val path: String init { - path = classFile + path = filePath } @Throws(Throwable::class) diff --git a/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.kt b/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.kt index c8ab118c5..6a2f25287 100644 --- a/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.kt +++ b/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.kt @@ -5,13 +5,13 @@ import android.content.Context import com.pranav.android.interfaces.* import com.pranav.android.task.java.* -class JavaBuilder : Builder() { +class JavaBuilder(context: Context, loader: ClassLoader) : Builder() { private var classloader: ClassLoader - public var mContext: Context + private var mContext: Context - contructor(context: Context, loader: ClassLoader) { + init { mContext = context classloader = loader } From 4bf37f7892898672835051b942be4c7b743eb6d9 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Wed, 29 Jun 2022 13:35:24 +0530 Subject: [PATCH 18/22] convert a few more files to kotlin --- .../exception/CompilationFailedException.java | 12 ----- .../exception/CompilationFailedException.kt | 5 ++ .../pranav/android/interfaces/Builder.java | 10 ---- .../com/pranav/android/interfaces/Builder.kt | 10 ++++ .../com/pranav/android/interfaces/Task.java | 8 --- .../com/pranav/android/interfaces/Task.kt | 9 ++++ .../com/pranav/android/task/java/D8Task.java | 50 ------------------- .../com/pranav/android/task/java/D8Task.kt | 47 +++++++++++++++++ 8 files changed, 71 insertions(+), 80 deletions(-) delete mode 100644 android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.java create mode 100644 android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.kt delete mode 100644 android-compiler/src/main/java/com/pranav/android/interfaces/Builder.java create mode 100644 android-compiler/src/main/java/com/pranav/android/interfaces/Builder.kt delete mode 100644 android-compiler/src/main/java/com/pranav/android/interfaces/Task.java create mode 100644 android-compiler/src/main/java/com/pranav/android/interfaces/Task.kt delete mode 100644 android-compiler/src/main/java/com/pranav/android/task/java/D8Task.java create mode 100644 android-compiler/src/main/java/com/pranav/android/task/java/D8Task.kt diff --git a/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.java b/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.java deleted file mode 100644 index 4ae2317eb..000000000 --- a/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.pranav.android.exception; - -public class CompilationFailedException extends Exception { - - public CompilationFailedException(Throwable e) { - super(e); - } - - public CompilationFailedException(String message) { - super(message); - } -} diff --git a/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.kt b/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.kt new file mode 100644 index 000000000..868aa5ad6 --- /dev/null +++ b/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.kt @@ -0,0 +1,5 @@ +package com.pranav.android.exception + +class CompilationFailedException(message: String) : Exception(message) { + constructor(e: Throwable): super(e) +} diff --git a/android-compiler/src/main/java/com/pranav/android/interfaces/Builder.java b/android-compiler/src/main/java/com/pranav/android/interfaces/Builder.java deleted file mode 100644 index e61d1b3f3..000000000 --- a/android-compiler/src/main/java/com/pranav/android/interfaces/Builder.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.pranav.android.interfaces; - -import android.content.Context; - -public abstract class Builder { - - public abstract Context getContext(); - - public abstract ClassLoader getClassloader(); -} diff --git a/android-compiler/src/main/java/com/pranav/android/interfaces/Builder.kt b/android-compiler/src/main/java/com/pranav/android/interfaces/Builder.kt new file mode 100644 index 000000000..4a59b341e --- /dev/null +++ b/android-compiler/src/main/java/com/pranav/android/interfaces/Builder.kt @@ -0,0 +1,10 @@ +package com.pranav.android.interfaces + +import android.content.Context + +abstract class Builder { + + abstract fun getContext() : Context + + abstract fun getClassloader() : ClassLoader +} diff --git a/android-compiler/src/main/java/com/pranav/android/interfaces/Task.java b/android-compiler/src/main/java/com/pranav/android/interfaces/Task.java deleted file mode 100644 index d3312df41..000000000 --- a/android-compiler/src/main/java/com/pranav/android/interfaces/Task.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.pranav.android.interfaces; - -public abstract class Task { - - public abstract String getTaskName(); - - public abstract void doFullTask() throws Exception; -} diff --git a/android-compiler/src/main/java/com/pranav/android/interfaces/Task.kt b/android-compiler/src/main/java/com/pranav/android/interfaces/Task.kt new file mode 100644 index 000000000..5c3a55968 --- /dev/null +++ b/android-compiler/src/main/java/com/pranav/android/interfaces/Task.kt @@ -0,0 +1,9 @@ +package com.pranav.android.interfaces; + +public abstract class Task { + + abstract fum getTaskName() : String + + @Throws(Exception::class) + abstract fun doFullTask() +} diff --git a/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.java b/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.java deleted file mode 100644 index f20965297..000000000 --- a/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.pranav.android.task.java; - -import com.android.tools.r8.D8; -import com.android.tools.r8.D8Command; -import com.android.tools.r8.OutputMode; - -import com.pranav.android.interfaces.*; -import com.pranav.common.util.FileUtil; - -import java.io.File; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; - -public class D8Task extends Task { - - private Exception ex = null; - - @Override - public void doFullTask() throws Exception { - D8.run( - D8Command.builder() - .setOutput(Paths.get(FileUtil.getBinDir()), OutputMode.DexIndexed) - .addLibraryFiles(Paths.get(FileUtil.getClasspathDir(), "android.jar")) - .addProgramFiles( - getClassFiles(new File(FileUtil.getBinDir(), "classes"))) - .build()); - } - - private ArrayList getClassFiles(File root) { - var paths = new ArrayList(); - - var files = root.listFiles(); - if (files != null) { - for (var f : files) { - if (f.isFile()) { - paths.add(f.toPath()); - } else { - paths.addAll(getClassFiles(f)); - } - } - } - return paths; - } - - @Override - public String getTaskName() { - return "D8 Task"; - } -} diff --git a/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.kt b/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.kt new file mode 100644 index 000000000..778834875 --- /dev/null +++ b/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.kt @@ -0,0 +1,47 @@ +package com.pranav.android.task.java + +import com.android.tools.r8.D8 +import com.android.tools.r8.D8Command +import com.android.tools.r8.OutputMode + +import com.pranav.android.interfaces.* +import com.pranav.common.util.FileUtil + +import java.io.File +import java.nio.file.Path +import java.nio.file.Paths +import java.util.List + +class D8Task : Task() { + + @Throws(Exception::class) + override fun doFullTask() { + D8.run( + D8Command.builder() + .setOutput(Paths.get(FileUtil.getBinDir()), OutputMode.DexIndexed) + .addLibraryFiles(Paths.get(FileUtil.getClasspathDir(), "android.jar")) + .addProgramFiles( + getClassFiles(File(FileUtil.getBinDir(), "classes"))) + .build()) + } + + private fun getClassFiles(root: File) : List { + val paths = listOf() + + val files = root.listFiles() + if (files != null) { + for (var f : files) { + if (f.isFile()) { + paths.add(f.toPath()) + } else { + paths.addAll(getClassFiles(f)) + } + } + } + return paths + } + + override fun getTaskName() : String { + return "D8 Task" + } +} From 3e162a3a6d4a066c39779b349eefed2727606309 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Wed, 29 Jun 2022 13:44:21 +0530 Subject: [PATCH 19/22] fix errors --- .../android/exception/CompilationFailedException.kt | 5 +++-- .../src/main/java/com/pranav/android/task/java/D8Task.kt | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.kt b/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.kt index 868aa5ad6..bc333585c 100644 --- a/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.kt +++ b/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.kt @@ -1,5 +1,6 @@ package com.pranav.android.exception -class CompilationFailedException(message: String) : Exception(message) { - constructor(e: Throwable): super(e) +class CompilationFailedException : Exception() { + constructor(message: String?) : super(message) + constructor(e: Throwable?): super(e) } diff --git a/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.kt b/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.kt index 778834875..9aa4f346e 100644 --- a/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.kt +++ b/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.kt @@ -10,7 +10,7 @@ import com.pranav.common.util.FileUtil import java.io.File import java.nio.file.Path import java.nio.file.Paths -import java.util.List +import java.util.ArrayList class D8Task : Task() { @@ -25,12 +25,12 @@ class D8Task : Task() { .build()) } - private fun getClassFiles(root: File) : List { - val paths = listOf() + private fun getClassFiles(root: File) : ArrayList { + val paths = arrayListOf() val files = root.listFiles() if (files != null) { - for (var f : files) { + for (f in files) { if (f.isFile()) { paths.add(f.toPath()) } else { From e6c57c33da66c36ab410fdc9f2d6f54951c22ce1 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Wed, 29 Jun 2022 13:45:18 +0530 Subject: [PATCH 20/22] fix errors --- .../com/pranav/android/exception/CompilationFailedException.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.kt b/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.kt index bc333585c..30a3b9335 100644 --- a/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.kt +++ b/android-compiler/src/main/java/com/pranav/android/exception/CompilationFailedException.kt @@ -1,6 +1,6 @@ package com.pranav.android.exception -class CompilationFailedException : Exception() { +class CompilationFailedException : Exception { constructor(message: String?) : super(message) constructor(e: Throwable?): super(e) } From f6678a7baf1503ee4a00838d42a036a03c769b5e Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Wed, 29 Jun 2022 14:15:51 +0530 Subject: [PATCH 21/22] use interface instead of abstract classes --- .../src/main/java/com/pranav/android/interfaces/Builder.kt | 6 +++--- .../src/main/java/com/pranav/android/interfaces/Task.kt | 6 +++--- .../src/main/java/com/pranav/android/task/JavaBuilder.kt | 2 +- .../src/main/java/com/pranav/android/task/java/D8Task.kt | 2 +- .../com/pranav/android/task/java/ECJCompilationTask.java | 2 +- .../java/com/pranav/android/task/java/ExecuteJavaTask.java | 2 +- .../src/main/java/com/pranav/android/task/java/JarTask.java | 2 +- .../com/pranav/android/task/java/JavacCompilationTask.java | 2 +- 8 files changed, 12 insertions(+), 12 deletions(-) diff --git a/android-compiler/src/main/java/com/pranav/android/interfaces/Builder.kt b/android-compiler/src/main/java/com/pranav/android/interfaces/Builder.kt index 4a59b341e..739354021 100644 --- a/android-compiler/src/main/java/com/pranav/android/interfaces/Builder.kt +++ b/android-compiler/src/main/java/com/pranav/android/interfaces/Builder.kt @@ -2,9 +2,9 @@ package com.pranav.android.interfaces import android.content.Context -abstract class Builder { +interface Builder { - abstract fun getContext() : Context + fun getContext() : Context - abstract fun getClassloader() : ClassLoader + fun getClassloader() : ClassLoader } diff --git a/android-compiler/src/main/java/com/pranav/android/interfaces/Task.kt b/android-compiler/src/main/java/com/pranav/android/interfaces/Task.kt index 5c3a55968..d443df362 100644 --- a/android-compiler/src/main/java/com/pranav/android/interfaces/Task.kt +++ b/android-compiler/src/main/java/com/pranav/android/interfaces/Task.kt @@ -1,9 +1,9 @@ package com.pranav.android.interfaces; -public abstract class Task { +interface class Task { - abstract fum getTaskName() : String + fun getTaskName() : String @Throws(Exception::class) - abstract fun doFullTask() + fun doFullTask() } diff --git a/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.kt b/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.kt index 6a2f25287..fc66222c7 100644 --- a/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.kt +++ b/android-compiler/src/main/java/com/pranav/android/task/JavaBuilder.kt @@ -5,7 +5,7 @@ import android.content.Context import com.pranav.android.interfaces.* import com.pranav.android.task.java.* -class JavaBuilder(context: Context, loader: ClassLoader) : Builder() { +class JavaBuilder(context: Context, loader: ClassLoader) : Builder { private var classloader: ClassLoader diff --git a/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.kt b/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.kt index 9aa4f346e..00e979d25 100644 --- a/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.kt +++ b/android-compiler/src/main/java/com/pranav/android/task/java/D8Task.kt @@ -12,7 +12,7 @@ import java.nio.file.Path import java.nio.file.Paths import java.util.ArrayList -class D8Task : Task() { +class D8Task : Task { @Throws(Exception::class) override fun doFullTask() { diff --git a/android-compiler/src/main/java/com/pranav/android/task/java/ECJCompilationTask.java b/android-compiler/src/main/java/com/pranav/android/task/java/ECJCompilationTask.java index 23b29b211..a2a224cb3 100644 --- a/android-compiler/src/main/java/com/pranav/android/task/java/ECJCompilationTask.java +++ b/android-compiler/src/main/java/com/pranav/android/task/java/ECJCompilationTask.java @@ -15,7 +15,7 @@ import java.io.PrintWriter; import java.util.ArrayList; -public class ECJCompilationTask extends Task { +public class ECJCompilationTask implements Task { private final StringBuilder errs = new StringBuilder(); private final SharedPreferences prefs; diff --git a/android-compiler/src/main/java/com/pranav/android/task/java/ExecuteJavaTask.java b/android-compiler/src/main/java/com/pranav/android/task/java/ExecuteJavaTask.java index dad4e8664..121e8158a 100644 --- a/android-compiler/src/main/java/com/pranav/android/task/java/ExecuteJavaTask.java +++ b/android-compiler/src/main/java/com/pranav/android/task/java/ExecuteJavaTask.java @@ -12,7 +12,7 @@ import java.io.PrintStream; import java.lang.reflect.Modifier; -public class ExecuteJavaTask extends Task { +public class ExecuteJavaTask implements Task { private final Builder mBuilder; private final String clazz; diff --git a/android-compiler/src/main/java/com/pranav/android/task/java/JarTask.java b/android-compiler/src/main/java/com/pranav/android/task/java/JarTask.java index bd267c69c..a74f80c24 100644 --- a/android-compiler/src/main/java/com/pranav/android/task/java/JarTask.java +++ b/android-compiler/src/main/java/com/pranav/android/task/java/JarTask.java @@ -13,7 +13,7 @@ import java.util.jar.JarOutputStream; import java.util.jar.Manifest; -public class JarTask extends Task { +public class JarTask implements Task { @Override public void doFullTask() throws Exception { diff --git a/android-compiler/src/main/java/com/pranav/android/task/java/JavacCompilationTask.java b/android-compiler/src/main/java/com/pranav/android/task/java/JavacCompilationTask.java index c0b77712e..342e336ce 100644 --- a/android-compiler/src/main/java/com/pranav/android/task/java/JavacCompilationTask.java +++ b/android-compiler/src/main/java/com/pranav/android/task/java/JavacCompilationTask.java @@ -22,7 +22,7 @@ import javax.tools.SimpleJavaFileObject; import javax.tools.StandardLocation; -public class JavacCompilationTask extends Task { +public class JavacCompilationTask implements Task { private final SharedPreferences prefs; From 39441f713d77281ab6c9fac43ff2e0fd2078baf8 Mon Sep 17 00:00:00 2001 From: PranavPurwar Date: Wed, 29 Jun 2022 15:38:27 +0530 Subject: [PATCH 22/22] remove 'class' --- .../src/main/java/com/pranav/android/interfaces/Task.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android-compiler/src/main/java/com/pranav/android/interfaces/Task.kt b/android-compiler/src/main/java/com/pranav/android/interfaces/Task.kt index d443df362..29c9c9c90 100644 --- a/android-compiler/src/main/java/com/pranav/android/interfaces/Task.kt +++ b/android-compiler/src/main/java/com/pranav/android/interfaces/Task.kt @@ -1,6 +1,6 @@ package com.pranav.android.interfaces; -interface class Task { +interface Task { fun getTaskName() : String