Skip to content

Commit

Permalink
Simplify UpdateClasspathJob
Browse files Browse the repository at this point in the history
Leverage the implicit cancellation checks and progress reporting of
SubMonitor.split()
  • Loading branch information
HannesWell committed Sep 21, 2023
1 parent a19c8ed commit c085f30
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.assertTrue;

import java.util.Hashtable;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
Expand Down Expand Up @@ -257,10 +258,7 @@ public void testMinimumComplianceOverwrite() throws Exception {

// updating class path should increase severity to warning
IPluginModelBase model = PluginRegistry.findModel(project.getProject());
UpdateClasspathJob job = new UpdateClasspathJob(new IPluginModelBase[] { model });
job.schedule();
job.join();

UpdateClasspathJob.scheduleFor(List.of(model), false).join();
// re-validate options
validateOption(project, JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_2);
validateOption(project, JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3);
Expand Down Expand Up @@ -305,9 +303,7 @@ public void testMinimumComplianceNoOverwrite() throws Exception {

// updating class path should increase severity to warning
IPluginModelBase model = PluginRegistry.findModel(project.getProject());
UpdateClasspathJob job = new UpdateClasspathJob(new IPluginModelBase[] { model });
job.schedule();
job.join();
UpdateClasspathJob.scheduleFor(List.of(model), false).join();

// re-validate options
validateOption(project, JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
*******************************************************************************/
package org.eclipse.pde.internal.ui.wizards.tools;

import org.eclipse.core.runtime.jobs.Job;
import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.pde.core.plugin.IPluginModelBase;
Expand Down Expand Up @@ -48,16 +50,13 @@ private IDialogSettings getSettingsSection(IDialogSettings master) {

@Override
public boolean performFinish() {
if (!PlatformUI.getWorkbench().saveAllEditors(true))
if (!PlatformUI.getWorkbench().saveAllEditors(true)) {
return false;

}
Object[] finalSelected = page1.getSelected();
page1.storeSettings();
IPluginModelBase[] modelArray = new IPluginModelBase[finalSelected.length];
System.arraycopy(finalSelected, 0, modelArray, 0, finalSelected.length);
Job j = new UpdateClasspathJob(modelArray);
j.setUser(true);
j.schedule();
List<IPluginModelBase> modelArray = Arrays.stream(finalSelected).map(IPluginModelBase.class::cast).toList();
UpdateClasspathJob.scheduleFor(modelArray, true);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,92 +13,69 @@
*******************************************************************************/
package org.eclipse.pde.internal.ui.wizards.tools;

import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.internal.core.ClasspathComputer;
import org.eclipse.pde.internal.core.builders.PDEMarkerFactory;
import org.eclipse.pde.internal.ui.IPDEUIConstants;
import org.eclipse.pde.internal.ui.PDEPlugin;
import org.eclipse.pde.internal.ui.PDEUIMessages;

public class UpdateClasspathJob extends Job {
IPluginModelBase[] fModels;

public UpdateClasspathJob(IPluginModelBase[] models) {
super(PDEUIMessages.UpdateClasspathJob_title);
setPriority(Job.LONG);
fModels = models;
public class UpdateClasspathJob {
private UpdateClasspathJob() {
}

public boolean doUpdateClasspath(IProgressMonitor monitor, IPluginModelBase[] models) throws CoreException {
monitor.beginTask(PDEUIMessages.UpdateClasspathJob_task, models.length);
try {
for (IPluginModelBase model : models) {
monitor.subTask(model.getPluginBase().getId());
// no reason to compile classpath for a non-Java model
IProject project = model.getUnderlyingResource().getProject();
if (!project.hasNature(JavaCore.NATURE_ID)) {
monitor.worked(1);
continue;
public static Job scheduleFor(List<IPluginModelBase> models, boolean user) {
WorkspaceJob job = new WorkspaceJob(PDEUIMessages.UpdateClasspathJob_title) {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
SubMonitor mon = SubMonitor.convert(monitor, PDEUIMessages.UpdateClasspathJob_task, models.size());
for (IPluginModelBase model : models) {
updateClasspath(model, mon.split(1));
}
IProjectDescription projDesc = project.getDescription();
if (projDesc == null)
continue;
projDesc.setReferencedProjects(new IProject[0]);
project.setDescription(projDesc, null);
IFile file = project.getFile(".project"); //$NON-NLS-1$
if (file.exists())
file.deleteMarkers(PDEMarkerFactory.MARKER_ID, true, IResource.DEPTH_ZERO);
ClasspathComputer.setClasspath(project, model);
monitor.worked(1);
if (monitor.isCanceled())
return false;
return Status.OK_STATUS;
}
} finally {
monitor.done();
}
return true;
}

class UpdateClasspathWorkspaceRunnable implements IWorkspaceRunnable {
boolean fCanceled = false;

@Override
public void run(IProgressMonitor monitor) throws CoreException {
fCanceled = !doUpdateClasspath(monitor, fModels);
}

public boolean isCanceled() {
return fCanceled;
}
};
job.setUser(user);
job.setPriority(Job.LONG);
job.schedule();
return job;
}

@Override
protected IStatus run(IProgressMonitor monitor) {
private static void updateClasspath(IPluginModelBase model, SubMonitor monitor) throws CoreException {
try {
UpdateClasspathWorkspaceRunnable runnable = new UpdateClasspathWorkspaceRunnable();
PDEPlugin.getWorkspace().run(runnable, monitor);
if (runnable.isCanceled()) {
return new Status(IStatus.CANCEL, IPDEUIConstants.PLUGIN_ID, IStatus.CANCEL, "", null); //$NON-NLS-1$
monitor.subTask(model.getPluginBase().getId());
// no reason to compile classpath for a non-Java model
IProject project = model.getUnderlyingResource().getProject();
if (project.hasNature(JavaCore.NATURE_ID)) {
IProjectDescription projDesc = project.getDescription();
if (projDesc != null) {
projDesc.setReferencedProjects(new IProject[0]);
project.setDescription(projDesc, null);
IFile file = project.getFile(".project"); //$NON-NLS-1$
if (file.exists()) {
file.deleteMarkers(PDEMarkerFactory.MARKER_ID, true, IResource.DEPTH_ZERO);
}
ClasspathComputer.setClasspath(project, model);
}
}

} catch (CoreException e) {
String title = PDEUIMessages.UpdateClasspathJob_error_title;
String message = PDEUIMessages.UpdateClasspathJob_error_message;
PDEPlugin.logException(e, title, message);
return Status.error(message, e);
PDEPlugin.logException(e, PDEUIMessages.UpdateClasspathJob_error_title, message);
throw new CoreException(Status.error(message, e));
}
return Status.OK_STATUS;
}

}

0 comments on commit c085f30

Please sign in to comment.