From d1ee17525bf94ab64c92cac3c4e25b127c13be3a Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Thu, 21 Apr 2022 18:13:17 +0530 Subject: [PATCH] Moved javac to a separate module; Fixed test failures --- .idea/compiler.xml | 2 +- .idea/misc.xml | 2 +- javac/.gitignore | 1 + javac/build.gradle | 28 +++++++++++++ .../libs/nb-javac-17.0.0.0-android.jar | Bin lsp/java/build.gradle | 35 +++++++++-------- .../lsp/java/compiler/ReusableCompiler.java | 5 ++- .../com/itsaky/lsp/java/utils/TestUtils.kt | 37 ++++++++++++++++++ .../lib/nbjavac/services/NBJavacTrees.java | 7 +--- settings.gradle | 1 + 10 files changed, 92 insertions(+), 26 deletions(-) create mode 100644 javac/.gitignore create mode 100644 javac/build.gradle rename {lsp/java => javac}/libs/nb-javac-17.0.0.0-android.jar (100%) create mode 100644 lsp/java/src/main/java/com/itsaky/lsp/java/utils/TestUtils.kt diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 96f1abef4..b516012a7 100755 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,7 +1,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 30ab9a6cb..06ac8f54d 100755 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -141,7 +141,7 @@ - + diff --git a/javac/.gitignore b/javac/.gitignore new file mode 100644 index 000000000..42afabfd2 --- /dev/null +++ b/javac/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/javac/build.gradle b/javac/build.gradle new file mode 100644 index 000000000..6af2e1ed8 --- /dev/null +++ b/javac/build.gradle @@ -0,0 +1,28 @@ +/* + * This file is part of AndroidIDE. + * + * AndroidIDE is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AndroidIDE is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with AndroidIDE. If not, see . + */ +plugins { + id 'java-library' +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +dependencies { + api files ('libs/nb-javac-17.0.0.0-android.jar') +} \ No newline at end of file diff --git a/lsp/java/libs/nb-javac-17.0.0.0-android.jar b/javac/libs/nb-javac-17.0.0.0-android.jar similarity index 100% rename from lsp/java/libs/nb-javac-17.0.0.0-android.jar rename to javac/libs/nb-javac-17.0.0.0-android.jar diff --git a/lsp/java/build.gradle b/lsp/java/build.gradle index ccf20c1b4..f4aaf1a15 100644 --- a/lsp/java/build.gradle +++ b/lsp/java/build.gradle @@ -20,24 +20,24 @@ apply plugin: 'kotlin-android' android { compileSdkVersion rootProject.ext.compileSdk buildToolsVersion rootProject.ext.buildTools - + defaultConfig { minSdkVersion rootProject.ext.minSdk targetSdkVersion rootProject.ext.targetSdk } - + buildTypes { release { minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + proguardFiles getDefaultProguardFile ('proguard-android-optimize.txt'), 'proguard-rules.pro' } } - + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } - + testOptions { unitTests { includeAndroidResources = true @@ -55,21 +55,22 @@ dependencies { implementation 'com.google.auto.value:auto-value-annotations:1.9' annotationProcessor 'com.google.auto.value:auto-value:1.9' - + compileOnly androidx.appcompat compileOnly google.material - compileOnly project(':actions') - compileOnly project(':common') - - implementation project(path: ':lsp:api') - implementation files ("libs/nb-javac-17.0.0.0-android.jar") - + compileOnly project (':actions') + compileOnly project (':common') + + implementation project (path: ':lsp:api') + implementation project (path: ':javac') + implementation "androidx.core:core-ktx:1.7.0" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - - testImplementation project(':lsp:testing') - testImplementation project(':common') - testImplementation project(':actions') + + testImplementation project (':lsp:testing') + testImplementation project (':common') + testImplementation project (':actions') + testImplementation project (':javac') testImplementation tests.junit testImplementation tests.google_truth testImplementation tests.robolectric @@ -79,5 +80,5 @@ dependencies { } repositories { - mavenCentral() + mavenCentral () } \ No newline at end of file diff --git a/lsp/java/src/main/java/com/itsaky/lsp/java/compiler/ReusableCompiler.java b/lsp/java/src/main/java/com/itsaky/lsp/java/compiler/ReusableCompiler.java index 1e9dd928e..d3d4faf9e 100644 --- a/lsp/java/src/main/java/com/itsaky/lsp/java/compiler/ReusableCompiler.java +++ b/lsp/java/src/main/java/com/itsaky/lsp/java/compiler/ReusableCompiler.java @@ -19,6 +19,7 @@ package com.itsaky.lsp.java.compiler; import com.itsaky.androidide.utils.Logger; +import com.itsaky.lsp.java.utils.TestUtils; import com.sun.source.tree.Tree; import com.sun.source.util.JavacTask; import com.sun.source.util.TaskEvent; @@ -205,7 +206,9 @@ public class ReusableCompiler { NBMemberEnter.preRegister(this, false); NBClassFinder.preRegister(this); - put(LazyTreeLoader.lazyTreeLoaderKey, new IDELazyTreeLoader()); + if (!TestUtils.isTestEnvironment()) { + put(LazyTreeLoader.lazyTreeLoaderKey, new IDELazyTreeLoader()); + } } @Override diff --git a/lsp/java/src/main/java/com/itsaky/lsp/java/utils/TestUtils.kt b/lsp/java/src/main/java/com/itsaky/lsp/java/utils/TestUtils.kt new file mode 100644 index 000000000..1bfc6767e --- /dev/null +++ b/lsp/java/src/main/java/com/itsaky/lsp/java/utils/TestUtils.kt @@ -0,0 +1,37 @@ +/* + * This file is part of AndroidIDE. + * + * AndroidIDE is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * AndroidIDE is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with AndroidIDE. If not, see . + */ + +package com.itsaky.lsp.java.utils + +/** + * Utility related to tests. + * + * @author Akash Yadav + */ +class TestUtils { + companion object { + @JvmStatic + fun isTestEnvironment (): Boolean { + return try { + Class.forName("org.robolectric.RobolectricTestRunner") + true + } catch (error: ClassNotFoundException) { + false + } + } + } +} \ No newline at end of file diff --git a/lsp/java/src/main/java/org/netbeans/lib/nbjavac/services/NBJavacTrees.java b/lsp/java/src/main/java/org/netbeans/lib/nbjavac/services/NBJavacTrees.java index 980bed37b..9546b00c5 100644 --- a/lsp/java/src/main/java/org/netbeans/lib/nbjavac/services/NBJavacTrees.java +++ b/lsp/java/src/main/java/org/netbeans/lib/nbjavac/services/NBJavacTrees.java @@ -42,11 +42,7 @@ public class NBJavacTrees extends JavacTrees { private final Map element2paths = new HashMap<>(); public static void preRegister(Context context) { - context.put(JavacTrees.class, new Context.Factory() { - public JavacTrees make(Context c) { - return new NBJavacTrees(c); - } - }); + context.put(JavacTrees.class, (Context.Factory) NBJavacTrees::new); } protected NBJavacTrees(Context context) { super(context); @@ -80,5 +76,4 @@ public class NBJavacTrees extends JavacTrees { } }; } - } diff --git a/settings.gradle b/settings.gradle index 88d51f402..48093f53e 100755 --- a/settings.gradle +++ b/settings.gradle @@ -13,3 +13,4 @@ include ':lsp:java' include ':lsp:xml' include ':lsp:testing' include ':actions' +include ':javac'