Skip to content

Commit d82ecb8

Browse files
committed
Address comments
1 parent b09c23b commit d82ecb8

File tree

3 files changed

+68
-33
lines changed

3 files changed

+68
-33
lines changed

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/Compile.java

+1-14
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,7 @@ public static Object compile(CompileParams params, IProgressMonitor monitor) {
5252
throw new IllegalArgumentException("The compile parameters should not be null.");
5353
}
5454

55-
IProject mainProject = null;
56-
if (StringUtils.isNotBlank(params.getProjectName())) {
57-
mainProject = ProjectUtils.getProject(params.getProjectName());
58-
} else if (mainProject == null && StringUtils.isNotBlank(params.getMainClass())) {
59-
try {
60-
List<IJavaProject> javaProjects = ResolveClasspathsHandler.getJavaProjectFromType(params.getMainClass());
61-
if (javaProjects.size() == 1) {
62-
mainProject = javaProjects.get(0).getProject();
63-
}
64-
} catch (CoreException e) {
65-
JavaLanguageServerPlugin.logException("Failed to resolve project from main class name.", e);
66-
}
67-
}
68-
55+
IProject mainProject = JdtUtils.getMainProject(params.getProjectName(), params.getMainClass());
6956
if (JdtUtils.isBspProject(mainProject) && !ProjectUtils.isGradleProject(mainProject)) {
7057
// Just need to trigger a build for the target project, the Gradle build server will
7158
// handle the build dependencies for us.

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaHotCodeReplaceProvider.java

+42-19
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.logging.Level;
3535
import java.util.logging.Logger;
3636

37+
import org.apache.commons.lang3.StringUtils;
3738
import org.eclipse.core.resources.IBuildConfiguration;
3839
import org.eclipse.core.resources.IFile;
3940
import org.eclipse.core.resources.IMarker;
@@ -60,6 +61,7 @@
6061
import org.eclipse.jdt.internal.core.util.Util;
6162
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
6263
import org.eclipse.jdt.ls.core.internal.JobHelpers;
64+
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
6365

6466
import com.microsoft.java.debug.core.Configuration;
6567
import com.microsoft.java.debug.core.DebugException;
@@ -68,6 +70,7 @@
6870
import com.microsoft.java.debug.core.IDebugSession;
6971
import com.microsoft.java.debug.core.StackFrameUtility;
7072
import com.microsoft.java.debug.core.adapter.AdapterUtils;
73+
import com.microsoft.java.debug.core.adapter.Constants;
7174
import com.microsoft.java.debug.core.adapter.ErrorCode;
7275
import com.microsoft.java.debug.core.adapter.HotCodeReplaceEvent;
7376
import com.microsoft.java.debug.core.adapter.IDebugAdapterContext;
@@ -109,6 +112,8 @@ public class JavaHotCodeReplaceProvider implements IHotCodeReplaceProvider, IRes
109112

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

115+
private String mainProject = "";
116+
112117
/**
113118
* Visitor for resource deltas.
114119
*/
@@ -274,6 +279,7 @@ public void initialize(IDebugAdapterContext context, Map<String, Object> options
274279
}
275280
this.context = context;
276281
currentDebugSession = context.getDebugSession();
282+
this.mainProject = ((String) options.get(Constants.PROJECT_NAME));
277283
}
278284

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

325331
@Override
326332
public CompletableFuture<List<String>> redefineClasses() {
327-
try {
328-
IProject mainProject = null;
329-
List<IJavaProject> javaProjects = ResolveClasspathsHandler.getJavaProjectFromType(context.getMainClass());
330-
if (javaProjects.size() == 1) {
331-
mainProject = javaProjects.get(0).getProject();
332-
}
333-
334-
if (mainProject != null && JdtUtils.isBspProject(mainProject)) {
335-
ResourcesPlugin.getWorkspace().build(
336-
new IBuildConfiguration[]{mainProject.getActiveBuildConfig()},
337-
IncrementalProjectBuilder.INCREMENTAL_BUILD,
338-
false /*buildReference*/,
339-
new NullProgressMonitor()
340-
);
341-
}
342-
} catch (CoreException e) {
343-
JavaLanguageServerPlugin.log(e);
344-
}
345-
333+
triggerBuildForBspProject();
346334
JobHelpers.waitForBuildJobs(10 * 1000);
347335
return CompletableFuture.supplyAsync(() -> {
348336
List<String> classNames = new ArrayList<>();
@@ -761,4 +749,39 @@ private List<StackFrame> getStackFrames(ThreadReference thread, boolean refresh)
761749
}
762750
});
763751
}
752+
753+
/**
754+
* Trigger build separately if the main project is a BSP project.
755+
* This is because auto build for BSP project will not update the class files to disk.
756+
*/
757+
private void triggerBuildForBspProject() {
758+
// check if the workspace contains BSP project first. This is for performance consideration.
759+
// Due to that getJavaProjectFromType() is a heavy operation.
760+
if (!containsBspProjects()) {
761+
return;
762+
}
763+
764+
IProject mainProject = JdtUtils.getMainProject(this.mainProject, context.getMainClass());
765+
if (mainProject != null && JdtUtils.isBspProject(mainProject)) {
766+
try {
767+
ResourcesPlugin.getWorkspace().build(
768+
new IBuildConfiguration[]{mainProject.getActiveBuildConfig()},
769+
IncrementalProjectBuilder.INCREMENTAL_BUILD,
770+
false /*buildReference*/,
771+
new NullProgressMonitor()
772+
);
773+
} catch (CoreException e) {
774+
// ignore compilation errors
775+
}
776+
}
777+
}
778+
779+
private boolean containsBspProjects() {
780+
for (IJavaProject javaProject : ProjectUtils.getJavaProjects()) {
781+
if (JdtUtils.isBspProject(javaProject.getProject())) {
782+
return true;
783+
}
784+
}
785+
return false;
786+
}
764787
}

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtUtils.java

+25
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.eclipse.jdt.launching.JavaRuntime;
4141
import org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer;
4242
import org.eclipse.jdt.launching.sourcelookup.containers.PackageFragmentRootSourceContainer;
43+
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
4344
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
4445

4546
import com.microsoft.java.debug.core.DebugException;
@@ -424,4 +425,28 @@ public static boolean isBspProject(IProject project) {
424425
return project != null && ProjectUtils.isJavaProject(project)
425426
&& ProjectUtils.hasNature(project, "com.microsoft.gradle.bs.importer.GradleBuildServerProjectNature");
426427
}
428+
429+
/**
430+
* Get main project according to the main project name or main class name,
431+
* or return <code>null</code> if the main project cannot be resolved.
432+
*/
433+
public static IProject getMainProject(String mainProjectName, String mainClassName) {
434+
IProject mainProject = null;
435+
if (StringUtils.isNotEmpty(mainProjectName)) {
436+
mainProject = ProjectUtils.getProject(mainProjectName);
437+
}
438+
439+
if (mainProject == null && StringUtils.isNotBlank(mainClassName)) {
440+
try {
441+
List<IJavaProject> javaProjects = ResolveClasspathsHandler.getJavaProjectFromType(mainClassName);
442+
if (javaProjects.size() == 1) {
443+
mainProject = javaProjects.get(0).getProject();
444+
}
445+
} catch (CoreException e) {
446+
JavaLanguageServerPlugin.logException("Failed to resolve project from main class name.", e);
447+
}
448+
}
449+
450+
return mainProject;
451+
}
427452
}

0 commit comments

Comments
 (0)