0

如标题中所述,我正在尝试使用批处理 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;
}

每种方法的优缺点是什么?

4

1 回答 1

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);
}
于 2013-09-24T01:59:51.280 回答