Eclipse (JDT) - 向导中的 performFinish 方法
Eclipse (JDT) - performFinish method in wizard
我需要在 Eclipse 中使用向导做一些事情,所以我检查了 JDT 他们是如何实现向导的,发现了这段我不理解的奇怪代码。
它忽略向导调度规则(从 getSchedulingRule 返回),以防从已经执行的作业调用代码(它使用该作业的调度规则)。因此,如果向导需要整个工作空间的调度规则,但当前线程已经在执行任何作业,则使用该作业的调度规则代替,这可能会导致在工作空间中执行新的可运行对象时出现问题。我在代码中添加了一些注释以使其更加清晰。
任何 Eclipse 专家都可以解释为什么 try 块按原样实现(不仅仅是使用 getSchedulingRule)吗?
/**
* Returns the scheduling rule for creating the element.
* @return returns the scheduling rule
*/
protected ISchedulingRule getSchedulingRule() {
return ResourcesPlugin.getWorkspace().getRoot(); // look all by default
}
/*
* @see Wizard#performFinish
*/
@Override
public boolean performFinish() {
IWorkspaceRunnable op= new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
try {
finishPage(monitor);
} catch (InterruptedException e) {
throw new OperationCanceledException(e.getMessage());
}
}
};
try {
//TODO: i need explanation of this block. Wizard should be used
// from UI thread, so the code Job.getJobManager().currentJob()
// means that there is possible Job currently executed by UI thread.
// Ok now if there is a job, use its scheduling rule ignoring getSchedulingRule.
// This could be maybe to force that this new runnable isn't executed until this thread finishes
// its current Job. Okb but if the current Job rule isn't so powerfull as this wizard needs, what than?
// It will cause error when executing op, because the runnable will not have enough access
// cause ignoring getSchedulingRule...
ISchedulingRule rule= null;
Job job= Job.getJobManager().currentJob();
if (job != null)
rule= job.getRule();
IRunnableWithProgress runnable= null;
if (rule != null)
runnable= new WorkbenchRunnableAdapter(op, rule, true);
else
runnable= new WorkbenchRunnableAdapter(op, getSchedulingRule());
getContainer().run(canRunForked(), true, runnable);
} catch (InvocationTargetException e) {
handleFinishException(getShell(), e);
return false;
} catch (InterruptedException e) {
return false;
}
return true;
}
我不确定我能否解释所有这些,但需要注意的关键是
Job.getJobManager().currentJob();
仅returns 当前线程 中的当前作业。由于 performFinish
在 UI 线程中通常是 运行,因此这不是普通的后台作业。 UI 线程中的 UIJob
个作业 运行。在我看来,这段代码正试图从某个 UI 作业中获取向导或相关代码已经启动的规则。
调用的 true
个参数:
new WorkbenchRunnableAdapter(op, rule, true)
将导致 WorkbenchRunnableAdapter
调用
Job.getJobManager().transferRule(fRule, thread);
如果线程发生变化。我认为这意味着代码试图在 运行nable 和之前 运行ning.
的任何作业的整个执行过程中保持使用相同的规则
我需要在 Eclipse 中使用向导做一些事情,所以我检查了 JDT 他们是如何实现向导的,发现了这段我不理解的奇怪代码。
它忽略向导调度规则(从 getSchedulingRule 返回),以防从已经执行的作业调用代码(它使用该作业的调度规则)。因此,如果向导需要整个工作空间的调度规则,但当前线程已经在执行任何作业,则使用该作业的调度规则代替,这可能会导致在工作空间中执行新的可运行对象时出现问题。我在代码中添加了一些注释以使其更加清晰。
任何 Eclipse 专家都可以解释为什么 try 块按原样实现(不仅仅是使用 getSchedulingRule)吗?
/**
* Returns the scheduling rule for creating the element.
* @return returns the scheduling rule
*/
protected ISchedulingRule getSchedulingRule() {
return ResourcesPlugin.getWorkspace().getRoot(); // look all by default
}
/*
* @see Wizard#performFinish
*/
@Override
public boolean performFinish() {
IWorkspaceRunnable op= new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
try {
finishPage(monitor);
} catch (InterruptedException e) {
throw new OperationCanceledException(e.getMessage());
}
}
};
try {
//TODO: i need explanation of this block. Wizard should be used
// from UI thread, so the code Job.getJobManager().currentJob()
// means that there is possible Job currently executed by UI thread.
// Ok now if there is a job, use its scheduling rule ignoring getSchedulingRule.
// This could be maybe to force that this new runnable isn't executed until this thread finishes
// its current Job. Okb but if the current Job rule isn't so powerfull as this wizard needs, what than?
// It will cause error when executing op, because the runnable will not have enough access
// cause ignoring getSchedulingRule...
ISchedulingRule rule= null;
Job job= Job.getJobManager().currentJob();
if (job != null)
rule= job.getRule();
IRunnableWithProgress runnable= null;
if (rule != null)
runnable= new WorkbenchRunnableAdapter(op, rule, true);
else
runnable= new WorkbenchRunnableAdapter(op, getSchedulingRule());
getContainer().run(canRunForked(), true, runnable);
} catch (InvocationTargetException e) {
handleFinishException(getShell(), e);
return false;
} catch (InterruptedException e) {
return false;
}
return true;
}
我不确定我能否解释所有这些,但需要注意的关键是
Job.getJobManager().currentJob();
仅returns 当前线程 中的当前作业。由于 performFinish
在 UI 线程中通常是 运行,因此这不是普通的后台作业。 UI 线程中的 UIJob
个作业 运行。在我看来,这段代码正试图从某个 UI 作业中获取向导或相关代码已经启动的规则。
调用的 true
个参数:
new WorkbenchRunnableAdapter(op, rule, true)
将导致 WorkbenchRunnableAdapter
调用
Job.getJobManager().transferRule(fRule, thread);
如果线程发生变化。我认为这意味着代码试图在 运行nable 和之前 运行ning.
的任何作业的整个执行过程中保持使用相同的规则