|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2022 Microsoft Corporation and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Microsoft Corporation - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | + |
| 14 | +package com.microsoft.java.debug.plugin.internal; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.logging.Logger; |
| 19 | + |
| 20 | +import org.apache.commons.lang3.StringUtils; |
| 21 | +import org.eclipse.core.resources.IMarker; |
| 22 | +import org.eclipse.core.resources.IProject; |
| 23 | +import org.eclipse.core.resources.IResource; |
| 24 | +import org.eclipse.core.resources.IWorkspaceRoot; |
| 25 | +import org.eclipse.core.resources.IncrementalProjectBuilder; |
| 26 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 27 | +import org.eclipse.core.runtime.CoreException; |
| 28 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 29 | +import org.eclipse.core.runtime.OperationCanceledException; |
| 30 | +import org.eclipse.jdt.core.IJavaProject; |
| 31 | +import org.eclipse.jdt.core.IType; |
| 32 | +import org.eclipse.jdt.ls.core.internal.BuildWorkspaceStatus; |
| 33 | +import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; |
| 34 | +import org.eclipse.jdt.ls.core.internal.ProjectUtils; |
| 35 | +import org.eclipse.jdt.ls.core.internal.ResourceUtils; |
| 36 | +import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager; |
| 37 | + |
| 38 | +import com.microsoft.java.debug.core.Configuration; |
| 39 | + |
| 40 | +public class Compile { |
| 41 | + private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME); |
| 42 | + |
| 43 | + public static BuildWorkspaceStatus compile(CompileParams params, IProgressMonitor monitor) { |
| 44 | + try { |
| 45 | + if (monitor.isCanceled()) { |
| 46 | + return BuildWorkspaceStatus.CANCELLED; |
| 47 | + } |
| 48 | + |
| 49 | + long compileAt = System.currentTimeMillis(); |
| 50 | + if (params != null && params.isFullBuild()) { |
| 51 | + ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.CLEAN_BUILD, monitor); |
| 52 | + ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, monitor); |
| 53 | + } else { |
| 54 | + ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor); |
| 55 | + } |
| 56 | + logger.info("Time cost for ECJ: " + (System.currentTimeMillis() - compileAt) + "ms"); |
| 57 | + |
| 58 | + IProject mainProject = params == null ? null : ProjectUtils.getProject(params.getProjectName()); |
| 59 | + IResource currentResource = mainProject; |
| 60 | + if (isUnmanagedFolder(mainProject) && StringUtils.isNotBlank(params.getMainClass())) { |
| 61 | + IType mainType = ProjectUtils.getJavaProject(mainProject).findType(params.getMainClass()); |
| 62 | + if (mainType != null && mainType.getResource() != null) { |
| 63 | + currentResource = mainType.getResource(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + List<IMarker> problemMarkers = new ArrayList<>(); |
| 68 | + if (currentResource != null) { |
| 69 | + List<IMarker> markers = ResourceUtils.getErrorMarkers(currentResource); |
| 70 | + if (markers != null) { |
| 71 | + problemMarkers.addAll(markers); |
| 72 | + } |
| 73 | + |
| 74 | + // Check if the referenced projects contain compilation errors. |
| 75 | + if (currentResource instanceof IProject && ProjectUtils.isJavaProject((IProject) currentResource)) { |
| 76 | + IJavaProject currentJavaProject = ProjectUtils.getJavaProject((IProject) currentResource); |
| 77 | + IJavaProject[] javaProjects = ProjectUtils.getJavaProjects(); |
| 78 | + for (IJavaProject otherJavaProject : javaProjects) { |
| 79 | + IProject other = otherJavaProject.getProject(); |
| 80 | + if (!other.equals(getDefaultProject()) && !other.equals((IProject) currentResource) |
| 81 | + && currentJavaProject.isOnClasspath(otherJavaProject)) { |
| 82 | + markers = ResourceUtils.getErrorMarkers(other); |
| 83 | + if (markers != null) { |
| 84 | + problemMarkers.addAll(markers); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } else { |
| 90 | + IJavaProject[] javaProjects = ProjectUtils.getJavaProjects(); |
| 91 | + for (IJavaProject javaProject : javaProjects) { |
| 92 | + IProject project = javaProject.getProject(); |
| 93 | + if (!project.equals(getDefaultProject())) { |
| 94 | + List<IMarker> markers = ResourceUtils.getErrorMarkers(project); |
| 95 | + if (markers != null) { |
| 96 | + problemMarkers.addAll(markers); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (problemMarkers.isEmpty()) { |
| 103 | + return BuildWorkspaceStatus.SUCCEED; |
| 104 | + } |
| 105 | + |
| 106 | + return BuildWorkspaceStatus.WITH_ERROR; |
| 107 | + } catch (CoreException e) { |
| 108 | + JavaLanguageServerPlugin.logException("Failed to build workspace.", e); |
| 109 | + return BuildWorkspaceStatus.FAILED; |
| 110 | + } catch (OperationCanceledException e) { |
| 111 | + return BuildWorkspaceStatus.CANCELLED; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static boolean isUnmanagedFolder(IProject project) { |
| 116 | + return project != null && ProjectUtils.isUnmanagedFolder(project) |
| 117 | + && ProjectUtils.isJavaProject(project); |
| 118 | + } |
| 119 | + |
| 120 | + private static IProject getDefaultProject() { |
| 121 | + return getWorkspaceRoot().getProject(ProjectsManager.DEFAULT_PROJECT_NAME); |
| 122 | + } |
| 123 | + |
| 124 | + private static IWorkspaceRoot getWorkspaceRoot() { |
| 125 | + return ResourcesPlugin.getWorkspace().getRoot(); |
| 126 | + } |
| 127 | + |
| 128 | + class CompileParams { |
| 129 | + String projectName; |
| 130 | + String mainClass; |
| 131 | + boolean isFullBuild = false; |
| 132 | + |
| 133 | + public String getMainClass() { |
| 134 | + return mainClass; |
| 135 | + } |
| 136 | + |
| 137 | + public boolean isFullBuild() { |
| 138 | + return isFullBuild; |
| 139 | + } |
| 140 | + |
| 141 | + public String getProjectName() { |
| 142 | + return projectName; |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments