正如标题中所解释的,我正在尝试使用批处理IResourceChangeEvents的方法创建IMarkers。我的目标是以正确,稳定的方式创建IMarkers。使用IWorkspaceRunnable是否存在任何限制(用于批量更改事件),还是在当前线程中创建IMarkers更安全?是否存在任何可预见的错误,例如并发问题?
这是一个开放式问题,但我只想了解使用IWorkspaceRunnable创建IMarkers的优缺点。以下是代码示例,包括和不包含批次更改:
// Generates marker with the given attributes
public static IMarker generateMarker(final IFile file, final Map<String, Object> attributes,
final String markerType) throws CoreException, BadLocationException, IOException
{
if (!attributes.containsKey(IMarker.LINE_NUMBER))
{
// Assumes that attributes has a mapping for IMarker.CHAR_START, which is invariant when creating markers in Solstice
int line = ResourceUtility.convertToDocument(file).getLineOfOffset((int) attributes.get(IMarker.CHAR_START));
attributes.put(IMarker.LINE_NUMBER, line + 1); // lines indexed at 1, not 0
}
IWorkspaceRunnable r = new IWorkspaceRunnable()
{
public void run(@Nullable IProgressMonitor monitor) throws CoreException
{
IMarker marker = file.createMarker(markerType);
marker.setAttributes(attributes);
MarkerField.marker_ = marker; // MarkerField is just an inner class. Functions as a pointer to a pointer.
}
};
file.getWorkspace().run(r, ResourceUtility.getRuleFactory().markerRule(file), 0, null);
return MarkerField.marker_;
}
或者,我可以取消批处理机制,并使用以下代码:
// Generates marker with the given attributes
public static IMarker generateMarker(final IFile file, final Map<String, Object> attributes,
final String markerType) throws CoreException, BadLocationException, IOException
{
if (!attributes.containsKey(IMarker.LINE_NUMBER))
{
// Assumes that attributes has a mapping for IMarker.CHAR_START, which is invariant when creating markers in Solstice
int line = ResourceUtility.convertToDocument(file).getLineOfOffset((int) attributes.get(IMarker.CHAR_START));
attributes.put(IMarker.LINE_NUMBER, line + 1); // lines indexed at 1, not 0
}
IMarker marker = file.createMarker(markerType);
marker.setAttributes(attributes);
return marker;
}
每种方法的优点/缺点是什么?
答案 0 :(得分:0)
事实证明,使用IWorkspaceRunnable进行批量更改是安全的。以下是一些消息来源:
Eclipse社区论坛帖子讨论了这个问题: http://www.eclipse.org/forums/index.php/m/126939/?srch=batching+resource#msg_126939
使用相同机制的MarkerUtilities类几乎与我上面的代码相似。摘录如下。
我现在唯一的问题是访问我刚创建的IMarker是否安全并返回它。我认为这是可以的,因为在返回IMarker时,IWorkspaceRunnable线程已经完成。
/**
* Creates a marker on the given resource with the given type and attributes.
* <p>
* This method modifies the workspace (progress is not reported to the user).</p>
*
* @param resource the resource
* @param attributes the attribute map (key type: <code>String</code>,
* value type: <code>Object</code>)
* @param markerType the type of marker
* @throws CoreException if this method fails
* @see IResource#createMarker(java.lang.String)
*/
public static void createMarker(final IResource resource, final Map attributes, final String markerType) throws CoreException {
IWorkspaceRunnable r= new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException {
IMarker marker= resource.createMarker(markerType);
marker.setAttributes(attributes);
}
};
resource.getWorkspace().run(r, null,IWorkspace.AVOID_UPDATE, null);
}