Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo committed Jun 14, 2024
1 parent b09c23b commit 05bd09d
Showing 1 changed file with 58 additions and 19 deletions.
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,55 @@ 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 = null;
if (StringUtils.isNotEmpty(this.mainProject)) {
mainProject = ProjectUtils.getProject(this.mainProject);
}

if (mainProject == null) {
try {
List<IJavaProject> javaProjects = ResolveClasspathsHandler.getJavaProjectFromType(context.getMainClass());
if (javaProjects.size() == 1) {
mainProject = javaProjects.get(0).getProject();
}
} catch (CoreException e) {
JavaLanguageServerPlugin.log(e);
}
}

if (mainProject != null && JdtUtils.isBspProject(mainProject)) {
try {
ResourcesPlugin.getWorkspace().build(
new IBuildConfiguration[]{mainProject.getActiveBuildConfig()},
IncrementalProjectBuilder.INCREMENTAL_BUILD,
false /*buildReference*/,
new NullProgressMonitor()
);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

private boolean containsBspProjects() {
for (IJavaProject javaProject : ProjectUtils.getJavaProjects()) {
if (JdtUtils.isBspProject(javaProject.getProject())) {
return true;
}
}
return false;
}
}

0 comments on commit 05bd09d

Please sign in to comment.