From b1554385456a64fca23a536f7751baf69084a18b Mon Sep 17 00:00:00 2001 From: Tanish Ranjan <62828604+Tanish-Ranjan@users.noreply.github.com> Date: Thu, 29 Aug 2024 08:43:42 +0530 Subject: [PATCH 1/6] Extracting JARs from AARs --- .../GradleBuildServerBuildSupport.java | 3 +- .../GradleBuildServerProjectImporter.java | 18 ------- .../microsoft/gradle/bs/importer/Utils.java | 47 +++++++++++++++++++ 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java index 978d36b9e..a02c4ff9c 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java @@ -641,7 +641,8 @@ private List getDependencyJars(DependencyModulesResult dependen } String classifier = artifactData.getClassifier(); try { - File jarFile = new File(new URI(uri)); + File artifactFile = new File(new URI(uri)); + File jarFile = Utils.getJarFile(artifactFile); if (classifier == null) { artifact = jarFile; } else if ("sources".equals(classifier)) { diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java index e55483e9c..e065b5159 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java @@ -101,24 +101,6 @@ public boolean applies(IProgressMonitor monitor) throws OperationCanceledExcepti return false; } - for (java.nio.file.Path directory : directories) { - // we don't support android - BasicFileDetector androidDetector = new BasicFileDetector(directory, ANDROID_MANIFEST) - .includeNested(false) - .addExclusions("**/build") //default gradle build dir - .addExclusions("**/bin"); - Collection androidDirectories = androidDetector.scan(monitor); - if (!androidDirectories.isEmpty()) { - Telemetry telemetry = new Telemetry("hasAndroidManifest", "true"); - Utils.sendTelemetry(JavaLanguageServerPlugin.getProjectsManager().getConnection(), - telemetry); - return false; - } - } - - Telemetry telemetry = new Telemetry("hasAndroidManifest", "false"); - Utils.sendTelemetry(JavaLanguageServerPlugin.getProjectsManager().getConnection(), - telemetry); return true; } diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/Utils.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/Utils.java index fd312aee6..fa83e9266 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/Utils.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/Utils.java @@ -2,14 +2,21 @@ import static org.eclipse.jdt.ls.core.internal.handlers.MapFlattener.getString; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.nio.file.Path; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; import org.eclipse.core.resources.ICommand; import org.eclipse.core.resources.IProject; @@ -214,4 +221,44 @@ public static void sendTelemetry(JavaLanguageClient client, Object message) { client.sendNotification(new ExecuteCommandParams("_java.gradle.buildServer.sendTelemetry", Arrays.asList(message))); } + + /** + * Extracts the jar file from the given artifact. + */ + public static File getJarFile(File file) { + + String filepath = file.getAbsolutePath(); + + if (filepath.endsWith(".aar")) { + + // Extracting classes.jar from AAR files + try(ZipInputStream is = new ZipInputStream(new FileInputStream(file))) { + + ZipEntry entry; + while ((entry = is.getNextEntry()) != null) { + if (entry.getName().equals("classes.jar")) { + String fileName = file.getName(); + fileName = fileName.substring(0, fileName.length() - 4); + fileName = fileName + ".jar"; + File outputFile = Path.of(file.getParentFile().getAbsolutePath(), fileName).toFile(); + try (FileOutputStream outputStream = new FileOutputStream(outputFile)) { + byte[] buffer = new byte[1024]; + int len; + while((len = is.read(buffer)) > 0) { + outputStream.write(buffer, 0, len); + } + return outputFile; + } + } + } + + } catch(IOException e) { + } + + } + + return file; + + } + } From 059cc6dfbf388d3c991678797b90c4a5d03ff938 Mon Sep 17 00:00:00 2001 From: Tanish Ranjan <62828604+Tanish-Ranjan@users.noreply.github.com> Date: Thu, 29 Aug 2024 15:08:06 +0530 Subject: [PATCH 2/6] Addressing Comments --- .../GradleBuildServerProjectImporter.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java index e065b5159..e55483e9c 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java @@ -101,6 +101,24 @@ public boolean applies(IProgressMonitor monitor) throws OperationCanceledExcepti return false; } + for (java.nio.file.Path directory : directories) { + // we don't support android + BasicFileDetector androidDetector = new BasicFileDetector(directory, ANDROID_MANIFEST) + .includeNested(false) + .addExclusions("**/build") //default gradle build dir + .addExclusions("**/bin"); + Collection androidDirectories = androidDetector.scan(monitor); + if (!androidDirectories.isEmpty()) { + Telemetry telemetry = new Telemetry("hasAndroidManifest", "true"); + Utils.sendTelemetry(JavaLanguageServerPlugin.getProjectsManager().getConnection(), + telemetry); + return false; + } + } + + Telemetry telemetry = new Telemetry("hasAndroidManifest", "false"); + Utils.sendTelemetry(JavaLanguageServerPlugin.getProjectsManager().getConnection(), + telemetry); return true; } From 4f6265177d1904c5c2a49da8347b1b2d547b0327 Mon Sep 17 00:00:00 2001 From: Tanish Ranjan <62828604+Tanish-Ranjan@users.noreply.github.com> Date: Fri, 30 Aug 2024 14:05:51 +0530 Subject: [PATCH 3/6] Addressing comments. --- .../src/com/microsoft/gradle/bs/importer/Utils.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/Utils.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/Utils.java index fa83e9266..c365b5dd6 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/Utils.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/Utils.java @@ -29,6 +29,7 @@ import org.eclipse.core.runtime.URIUtil; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.ls.core.internal.JavaClientConnection.JavaLanguageClient; +import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; import org.eclipse.jdt.ls.core.internal.ProjectUtils; import org.eclipse.jdt.ls.core.internal.preferences.Preferences; import org.eclipse.lsp4j.ExecuteCommandParams; @@ -223,7 +224,8 @@ public static void sendTelemetry(JavaLanguageClient client, Object message) { } /** - * Extracts the jar file from the given artifact. + * Extracts the jar file from the aar file, since JDT.LS is not able to understand + * the structure of aar files. */ public static File getJarFile(File file) { @@ -253,6 +255,7 @@ public static File getJarFile(File file) { } } catch(IOException e) { + JavaLanguageServerPlugin.logException(e); } } From 9021f77659658c560a8da5905e1d65e7719e964b Mon Sep 17 00:00:00 2001 From: Tanish Ranjan <62828604+Tanish-Ranjan@users.noreply.github.com> Date: Tue, 3 Sep 2024 19:11:11 +0530 Subject: [PATCH 4/6] Made empty Java Home and Gradle Version acceptable for Jvm Build Targets --- .../GradleBuildServerBuildSupport.java | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java index a02c4ff9c..d08605b85 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java @@ -513,30 +513,32 @@ private void setProjectJdk(Map classpathMap, List classpathAttributes = new LinkedList<>(); - if (isModular) { - classpathAttributes.add(modularAttribute); + List classpathAttributes = new LinkedList<>(); + if (isModular) { + classpathAttributes.add(modularAttribute); + } + classpathAttributes.add(buildServerAttribute); + IClasspathEntry jdkEntry = JavaCore.newContainerEntry( + JavaRuntime.newJREContainerPath(vm), + ClasspathEntry.NO_ACCESS_RULES, + classpathAttributes.toArray(new IClasspathAttribute[0]), + false /*isExported*/ + ); + classpathMap.putIfAbsent(jdkEntry.getPath(), jdkEntry); + } catch (URISyntaxException e) { + throw new CoreException(new Status(IStatus.ERROR, ImporterPlugin.PLUGIN_ID, + "Invalid Java home: " + jvmBuildTarget.getJavaHome(), e)); } - classpathAttributes.add(buildServerAttribute); - IClasspathEntry jdkEntry = JavaCore.newContainerEntry( - JavaRuntime.newJREContainerPath(vm), - ClasspathEntry.NO_ACCESS_RULES, - classpathAttributes.toArray(new IClasspathAttribute[0]), - false /*isExported*/ - ); - classpathMap.putIfAbsent(jdkEntry.getPath(), jdkEntry); - } catch (URISyntaxException e) { - throw new CoreException(new Status(IStatus.ERROR, ImporterPlugin.PLUGIN_ID, - "Invalid Java home: " + jvmBuildTarget.getJavaHome(), e)); } } @@ -585,8 +587,10 @@ private JvmBuildTargetEx getJvmTarget(List buildTargets) throws Cor } if (StringUtils.isBlank(jvmTarget.getJavaHome()) || StringUtils.isBlank(jvmTarget.getGradleVersion())) { - throw new CoreException(new Status(IStatus.ERROR, ImporterPlugin.PLUGIN_ID, - "Invalid JVM build target: " + jvmTarget.toString())); + JavaLanguageServerPlugin.logException( + new CoreException(new Status(IStatus.WARNING, ImporterPlugin.PLUGIN_ID, + "Invalid JVM build target: Missing JavaHome or GradleVersion.")) + ); } return jvmTarget; From 974ebc4e114459b65b61c7e8c95634986511ae9f Mon Sep 17 00:00:00 2001 From: Tanish Ranjan <62828604+Tanish-Ranjan@users.noreply.github.com> Date: Wed, 4 Sep 2024 13:02:41 +0530 Subject: [PATCH 5/6] Addressing comments. --- .../gradle/bs/importer/GradleBuildServerBuildSupport.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java index d08605b85..432111bde 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java @@ -513,7 +513,8 @@ private void setProjectJdk(Map classpathMap, List buildTargets) throws Cor if (StringUtils.isBlank(jvmTarget.getJavaHome()) || StringUtils.isBlank(jvmTarget.getGradleVersion())) { JavaLanguageServerPlugin.logException( new CoreException(new Status(IStatus.WARNING, ImporterPlugin.PLUGIN_ID, - "Invalid JVM build target: Missing JavaHome or GradleVersion.")) + "Empty Java Home or Gradle Version in JVM target.")) ); } From 4bb27a8263ad828e89eddcb0a74bed6c8a24a3e5 Mon Sep 17 00:00:00 2001 From: Tanish Ranjan <62828604+Tanish-Ranjan@users.noreply.github.com> Date: Thu, 5 Sep 2024 08:58:07 +0530 Subject: [PATCH 6/6] Addressing comments. --- .../gradle/bs/importer/GradleBuildServerBuildSupport.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java index 432111bde..e004eae80 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerBuildSupport.java @@ -513,8 +513,8 @@ private void setProjectJdk(Map classpathMap, List