Skip to content

Commit

Permalink
feat - Extract classes.jar from *.aar (#1594)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanish-Ranjan authored Sep 5, 2024
1 parent 7c322dc commit 23bfc97
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -513,30 +513,33 @@ private void setProjectJdk(Map<IPath, IClasspathEntry> classpathMap, List<BuildT
javaProject.setOption(JavaCore.COMPILER_COMPLIANCE, targetCompatibility);
}

String highestJavaVersion = getHighestCompatibleJavaVersion(jvmBuildTarget.getGradleVersion());
try {
IVMInstall vm = EclipseVmUtil.findOrRegisterStandardVM(
targetCompatibility, // expectedVersion
sourceCompatibility, // lowestVersion
highestJavaVersion,
new File(new URI(jvmBuildTarget.getJavaHome())) // fallback jdk
);
if (StringUtils.isNotBlank(jvmBuildTarget.getJavaHome())
&& StringUtils.isNotBlank(jvmBuildTarget.getGradleVersion())) {
String highestJavaVersion = getHighestCompatibleJavaVersion(jvmBuildTarget.getGradleVersion());
try {
IVMInstall vm = EclipseVmUtil.findOrRegisterStandardVM(
targetCompatibility, // expectedVersion
sourceCompatibility, // lowestVersion
highestJavaVersion,
new File(new URI(jvmBuildTarget.getJavaHome())) // fallback jdk
);

List<IClasspathAttribute> classpathAttributes = new LinkedList<>();
if (isModular) {
classpathAttributes.add(modularAttribute);
List<IClasspathAttribute> 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));
}
}

Expand Down Expand Up @@ -585,8 +588,10 @@ private JvmBuildTargetEx getJvmTarget(List<BuildTarget> 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,
"Empty Java Home or Gradle Version in JVM target."))
);
}

return jvmTarget;
Expand Down Expand Up @@ -641,7 +646,8 @@ private List<IClasspathEntry> 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,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;
Expand Down Expand Up @@ -214,4 +222,46 @@ 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 aar file, since JDT.LS is not able to understand
* the structure of aar files.
*/
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) {
JavaLanguageServerPlugin.logException(e);
}

}

return file;

}

}

0 comments on commit 23bfc97

Please sign in to comment.