1414package com .microsoft .java .debug .plugin .internal ;
1515
1616import java .util .ArrayList ;
17+ import java .util .LinkedList ;
1718import java .util .List ;
1819import java .util .logging .Logger ;
1920
2829import org .eclipse .core .resources .ResourcesPlugin ;
2930import org .eclipse .core .runtime .CoreException ;
3031import org .eclipse .core .runtime .IProgressMonitor ;
31- import org .eclipse .core .runtime .OperationCanceledException ;
3232import org .eclipse .jdt .core .IJavaProject ;
3333import org .eclipse .jdt .core .IType ;
3434import org .eclipse .jdt .ls .core .internal .BuildWorkspaceStatus ;
3535import org .eclipse .jdt .ls .core .internal .JavaLanguageServerPlugin ;
3636import org .eclipse .jdt .ls .core .internal .ProjectUtils ;
3737import org .eclipse .jdt .ls .core .internal .ResourceUtils ;
38+ import org .eclipse .jdt .ls .core .internal .handlers .BuildWorkspaceHandler ;
3839import org .eclipse .jdt .ls .core .internal .managers .ProjectsManager ;
40+ import org .eclipse .lsp4j .TextDocumentIdentifier ;
41+ import org .eclipse .lsp4j .extended .ProjectBuildParams ;
3942
4043import com .microsoft .java .debug .core .Configuration ;
4144
@@ -45,20 +48,12 @@ public class Compile {
4548 private static final int GRADLE_BS_COMPILATION_ERROR = 100 ;
4649
4750 public static Object compile (CompileParams params , IProgressMonitor monitor ) {
48- IProject mainProject = params == null ? null : ProjectUtils .getProject (params .getProjectName ());
49- if (mainProject == null ) {
50- try {
51- // Q: is infer project by main class name necessary? perf impact?
52- List <IJavaProject > javaProjects = ResolveClasspathsHandler .getJavaProjectFromType (params .getMainClass ());
53- if (javaProjects .size () == 1 ) {
54- mainProject = javaProjects .get (0 ).getProject ();
55- }
56- } catch (CoreException e ) {
57- JavaLanguageServerPlugin .logException ("Failed to resolve project from main class name." , e );
58- }
51+ if (params == null ) {
52+ throw new IllegalArgumentException ("The compile parameters should not be null." );
5953 }
6054
61- if (isBspProject (mainProject ) && !ProjectUtils .isGradleProject (mainProject )) {
55+ IProject mainProject = JdtUtils .getMainProject (params .getProjectName (), params .getMainClass ());
56+ if (JdtUtils .isBspProject (mainProject ) && !ProjectUtils .isGradleProject (mainProject )) {
6257 // Just need to trigger a build for the target project, the Gradle build server will
6358 // handle the build dependencies for us.
6459 try {
@@ -78,20 +73,37 @@ public static Object compile(CompileParams params, IProgressMonitor monitor) {
7873 return BuildWorkspaceStatus .SUCCEED ;
7974 }
8075
81- try {
82- if (monitor .isCanceled ()) {
83- return BuildWorkspaceStatus .CANCELLED ;
84- }
76+ if (monitor .isCanceled ()) {
77+ return BuildWorkspaceStatus .CANCELLED ;
78+ }
8579
86- long compileAt = System . currentTimeMillis ();
87- if ( params != null && params . isFullBuild ()) {
88- ResourcesPlugin . getWorkspace (). build ( IncrementalProjectBuilder . CLEAN_BUILD , monitor );
89- ResourcesPlugin . getWorkspace (). build ( IncrementalProjectBuilder . FULL_BUILD , monitor );
90- } else {
91- ResourcesPlugin . getWorkspace (). build ( IncrementalProjectBuilder . INCREMENTAL_BUILD , monitor ) ;
80+ ProjectBuildParams buildParams = new ProjectBuildParams ();
81+ List < TextDocumentIdentifier > identifiers = new LinkedList <>();
82+ buildParams . setFullBuild ( params . isFullBuild );
83+ for ( IJavaProject javaProject : ProjectUtils . getJavaProjects ()) {
84+ if ( ProjectsManager . getDefaultProject (). equals ( javaProject . getProject ())) {
85+ continue ;
9286 }
93- logger .info ("Time cost for ECJ: " + (System .currentTimeMillis () - compileAt ) + "ms" );
87+ // we only build project which is not a BSP project, in case that the compile request is triggered by
88+ // HCR with auto-build disabled, the build for BSP projects will be triggered by JavaHotCodeReplaceProvider.
89+ if (!JdtUtils .isBspProject (javaProject .getProject ())) {
90+ identifiers .add (new TextDocumentIdentifier (javaProject .getProject ().getLocationURI ().toString ()));
91+ }
92+ }
93+ if (identifiers .size () == 0 ) {
94+ return BuildWorkspaceStatus .SUCCEED ;
95+ }
9496
97+ buildParams .setIdentifiers (identifiers );
98+ long compileAt = System .currentTimeMillis ();
99+ BuildWorkspaceHandler buildWorkspaceHandler = new BuildWorkspaceHandler (JavaLanguageServerPlugin .getProjectsManager ());
100+ BuildWorkspaceStatus status = buildWorkspaceHandler .buildProjects (buildParams , monitor );
101+ logger .info ("Time cost for ECJ: " + (System .currentTimeMillis () - compileAt ) + "ms" );
102+ if (status == BuildWorkspaceStatus .FAILED || status == BuildWorkspaceStatus .CANCELLED ) {
103+ return status ;
104+ }
105+
106+ try {
95107 IResource currentResource = mainProject ;
96108 if (isUnmanagedFolder (mainProject ) && StringUtils .isNotBlank (params .getMainClass ())) {
97109 IType mainType = ProjectUtils .getJavaProject (mainProject ).findType (params .getMainClass ());
@@ -135,29 +147,21 @@ public static Object compile(CompileParams params, IProgressMonitor monitor) {
135147 }
136148 }
137149
138- if (problemMarkers .isEmpty ()) {
139- return BuildWorkspaceStatus .SUCCEED ;
150+ if (! problemMarkers .isEmpty ()) {
151+ return BuildWorkspaceStatus .WITH_ERROR ;
140152 }
141-
142- return BuildWorkspaceStatus .WITH_ERROR ;
143153 } catch (CoreException e ) {
144- JavaLanguageServerPlugin .logException ("Failed to build workspace." , e );
145- return BuildWorkspaceStatus .FAILED ;
146- } catch (OperationCanceledException e ) {
147- return BuildWorkspaceStatus .CANCELLED ;
154+ JavaLanguageServerPlugin .log (e );
148155 }
156+
157+ return BuildWorkspaceStatus .SUCCEED ;
149158 }
150159
151160 private static boolean isUnmanagedFolder (IProject project ) {
152161 return project != null && ProjectUtils .isUnmanagedFolder (project )
153162 && ProjectUtils .isJavaProject (project );
154163 }
155164
156- private static boolean isBspProject (IProject project ) {
157- return project != null && ProjectUtils .isJavaProject (project )
158- && ProjectUtils .hasNature (project , "com.microsoft.gradle.bs.importer.GradleBuildServerProjectNature" );
159- }
160-
161165 private static IProject getDefaultProject () {
162166 return getWorkspaceRoot ().getProject (ProjectsManager .DEFAULT_PROJECT_NAME );
163167 }
0 commit comments