Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address comments
  • Loading branch information
jdneo committed Jun 14, 2024
commit d82ecb812edafb945c5ac08ea5f519dce5f48744
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,7 @@ public static Object compile(CompileParams params, IProgressMonitor monitor) {
throw new IllegalArgumentException("The compile parameters should not be null.");
}

IProject mainProject = null;
if (StringUtils.isNotBlank(params.getProjectName())) {
mainProject = ProjectUtils.getProject(params.getProjectName());
} else if (mainProject == null && StringUtils.isNotBlank(params.getMainClass())) {
try {
List<IJavaProject> javaProjects = ResolveClasspathsHandler.getJavaProjectFromType(params.getMainClass());
if (javaProjects.size() == 1) {
mainProject = javaProjects.get(0).getProject();
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Failed to resolve project from main class name.", e);
}
}

IProject mainProject = JdtUtils.getMainProject(params.getProjectName(), params.getMainClass());
if (JdtUtils.isBspProject(mainProject) && !ProjectUtils.isGradleProject(mainProject)) {
// Just need to trigger a build for the target project, the Gradle build server will
// handle the build dependencies for us.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.IBuildConfiguration;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
Expand All @@ -60,6 +61,7 @@
import org.eclipse.jdt.internal.core.util.Util;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.JobHelpers;
import org.eclipse.jdt.ls.core.internal.ProjectUtils;

import com.microsoft.java.debug.core.Configuration;
import com.microsoft.java.debug.core.DebugException;
Expand All @@ -68,6 +70,7 @@
import com.microsoft.java.debug.core.IDebugSession;
import com.microsoft.java.debug.core.StackFrameUtility;
import com.microsoft.java.debug.core.adapter.AdapterUtils;
import com.microsoft.java.debug.core.adapter.Constants;
import com.microsoft.java.debug.core.adapter.ErrorCode;
import com.microsoft.java.debug.core.adapter.HotCodeReplaceEvent;
import com.microsoft.java.debug.core.adapter.IDebugAdapterContext;
Expand Down Expand Up @@ -109,6 +112,8 @@ public class JavaHotCodeReplaceProvider implements IHotCodeReplaceProvider, IRes

private List<String> deltaClassNames = new ArrayList<>();

private String mainProject = "";

/**
* Visitor for resource deltas.
*/
Expand Down Expand Up @@ -274,6 +279,7 @@ public void initialize(IDebugAdapterContext context, Map<String, Object> options
}
this.context = context;
currentDebugSession = context.getDebugSession();
this.mainProject = ((String) options.get(Constants.PROJECT_NAME));
}

@Override
Expand Down Expand Up @@ -324,25 +330,7 @@ public void onClassRedefined(Consumer<List<String>> consumer) {

@Override
public CompletableFuture<List<String>> redefineClasses() {
try {
IProject mainProject = null;
List<IJavaProject> javaProjects = ResolveClasspathsHandler.getJavaProjectFromType(context.getMainClass());
if (javaProjects.size() == 1) {
mainProject = javaProjects.get(0).getProject();
}

if (mainProject != null && JdtUtils.isBspProject(mainProject)) {
ResourcesPlugin.getWorkspace().build(
new IBuildConfiguration[]{mainProject.getActiveBuildConfig()},
IncrementalProjectBuilder.INCREMENTAL_BUILD,
false /*buildReference*/,
new NullProgressMonitor()
);
}
} catch (CoreException e) {
JavaLanguageServerPlugin.log(e);
}

triggerBuildForBspProject();
JobHelpers.waitForBuildJobs(10 * 1000);
return CompletableFuture.supplyAsync(() -> {
List<String> classNames = new ArrayList<>();
Expand Down Expand Up @@ -761,4 +749,39 @@ private List<StackFrame> getStackFrames(ThreadReference thread, boolean refresh)
}
});
}

/**
* Trigger build separately if the main project is a BSP project.
* This is because auto build for BSP project will not update the class files to disk.
*/
private void triggerBuildForBspProject() {
// check if the workspace contains BSP project first. This is for performance consideration.
// Due to that getJavaProjectFromType() is a heavy operation.
if (!containsBspProjects()) {
return;
}

IProject mainProject = JdtUtils.getMainProject(this.mainProject, context.getMainClass());
if (mainProject != null && JdtUtils.isBspProject(mainProject)) {
try {
ResourcesPlugin.getWorkspace().build(
new IBuildConfiguration[]{mainProject.getActiveBuildConfig()},
IncrementalProjectBuilder.INCREMENTAL_BUILD,
false /*buildReference*/,
new NullProgressMonitor()
);
} catch (CoreException e) {
// ignore compilation errors
}
}
}

private boolean containsBspProjects() {
for (IJavaProject javaProject : ProjectUtils.getJavaProjects()) {
if (JdtUtils.isBspProject(javaProject.getProject())) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer;
import org.eclipse.jdt.launching.sourcelookup.containers.PackageFragmentRootSourceContainer;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.ProjectUtils;

import com.microsoft.java.debug.core.DebugException;
Expand Down Expand Up @@ -424,4 +425,28 @@ public static boolean isBspProject(IProject project) {
return project != null && ProjectUtils.isJavaProject(project)
&& ProjectUtils.hasNature(project, "com.microsoft.gradle.bs.importer.GradleBuildServerProjectNature");
}

/**
* Get main project according to the main project name or main class name,
* or return <code>null</code> if the main project cannot be resolved.
*/
public static IProject getMainProject(String mainProjectName, String mainClassName) {
IProject mainProject = null;
if (StringUtils.isNotEmpty(mainProjectName)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use isNotBlank instead

mainProject = ProjectUtils.getProject(mainProjectName);
}

if (mainProject == null && StringUtils.isNotBlank(mainClassName)) {
try {
List<IJavaProject> javaProjects = ResolveClasspathsHandler.getJavaProjectFromType(mainClassName);
if (javaProjects.size() == 1) {
mainProject = javaProjects.get(0).getProject();
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Failed to resolve project from main class name.", e);
}
}

return mainProject;
}
}