use of org.eclipse.core.resources.IMarker in project eclipse-pmd by acanda.
the class WrappingPMDMarkerTest method getViolationClassName.
/**
* Verifies that {@link WrappingPMDMarker#getViolationClassName()} gets the violation class name from the wrapped marker.
*/
@Test
public void getViolationClassName() throws CoreException {
final IMarker marker = mock(IMarker.class);
final String expected = "ViolationClassName";
when(marker.getAttribute(eq("violationClassName"), anyString())).thenReturn(expected);
final PMDMarker pmdMarker = new WrappingPMDMarker(marker);
final String actual = pmdMarker.getViolationClassName();
assertEquals("The rule id should be read from the wrapped marker", expected, actual);
}
use of org.eclipse.core.resources.IMarker in project eclipse-pmd by acanda.
the class JavaQuickFix method fixMarkersInFile.
/**
* Fixes all provided markers in a file.
*
* @param markers The markers to fix. There is at least one marker in this collection and all markers can be fixed
* by this quick fix.
*/
protected void fixMarkersInFile(final IFile file, final List<IMarker> markers, final IProgressMonitor monitor) {
monitor.subTask(file.getFullPath().toOSString());
final Optional<ICompilationUnit> optionalCompilationUnit = getCompilationUnit(file);
if (!optionalCompilationUnit.isPresent()) {
return;
}
final ICompilationUnit compilationUnit = optionalCompilationUnit.get();
ITextFileBufferManager bufferManager = null;
final IPath path = compilationUnit.getPath();
try {
bufferManager = FileBuffers.getTextFileBufferManager();
bufferManager.connect(path, LocationKind.IFILE, null);
final ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE);
final IDocument document = textFileBuffer.getDocument();
final IAnnotationModel annotationModel = textFileBuffer.getAnnotationModel();
final ASTParser astParser = ASTParser.newParser(AST.JLS4);
astParser.setKind(ASTParser.K_COMPILATION_UNIT);
astParser.setResolveBindings(needsTypeResolution());
astParser.setSource(compilationUnit);
final SubProgressMonitor parserMonitor = new SubProgressMonitor(monitor, 100);
final CompilationUnit ast = (CompilationUnit) astParser.createAST(parserMonitor);
parserMonitor.done();
startFixingMarkers(ast);
final Map<?, ?> options = compilationUnit.getJavaProject().getOptions(true);
for (final IMarker marker : markers) {
try {
final MarkerAnnotation annotation = getMarkerAnnotation(annotationModel, marker);
// if the annotation is null it means that is was deleted by a previous quick fix
if (annotation != null) {
final Optional<T> node = getNodeFinder(annotationModel.getPosition(annotation)).findNode(ast);
if (node.isPresent()) {
final boolean isSuccessful = fixMarker(node.get(), document, options);
if (isSuccessful) {
marker.delete();
}
}
}
} finally {
monitor.worked(100);
}
}
finishFixingMarkers(ast, document, options);
// commit changes to underlying file if it is not opened in an editor
if (!isEditorOpen(file)) {
final SubProgressMonitor commitMonitor = new SubProgressMonitor(monitor, 100);
textFileBuffer.commit(commitMonitor, false);
commitMonitor.done();
} else {
monitor.worked(100);
}
} catch (CoreException | MalformedTreeException | BadLocationException e) {
// TODO: log error
// PMDPlugin.getDefault().error("Error processing quickfix", e);
} finally {
if (bufferManager != null) {
try {
bufferManager.disconnect(path, LocationKind.IFILE, null);
} catch (final CoreException e) {
// TODO: log error
// PMDPlugin.getDefault().error("Error processing quickfix", e);
}
}
}
}
use of org.eclipse.core.resources.IMarker in project eclipse-pmd by acanda.
the class ASTQuickFixTestCase method getQuickFix.
@SuppressWarnings("unchecked")
private ASTQuickFix<ASTNode> getQuickFix() {
try {
final Type typeArgument = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
final Class<T> quickFixClass = (Class<T>) typeArgument;
final IMarker marker = mock(IMarker.class);
when(marker.getAttribute(eq("ruleName"), isA(String.class))).thenReturn(params.rulename.orNull());
final String markerText = params.source.substring(params.offset, params.offset + params.length);
if (!markerText.contains("\n")) {
when(marker.getAttribute(eq("markerText"), isA(String.class))).thenReturn(markerText);
}
return (ASTQuickFix<ASTNode>) quickFixClass.getConstructor(PMDMarker.class).newInstance(new WrappingPMDMarker(marker));
} catch (SecurityException | ReflectiveOperationException e) {
throw new IllegalArgumentException(e);
}
}
use of org.eclipse.core.resources.IMarker in project flux by eclipse.
the class MarkerUtilities method createMarker.
/**
* 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);
}
use of org.eclipse.core.resources.IMarker in project che by eclipse.
the class MarkerDescription method createMarker.
/**
* Create a marker from the marker description.
*
* @return the created marker
* @throws CoreException
*/
public IMarker createMarker() throws CoreException {
IMarker marker = resource.createMarker(type);
marker.setAttributes(attributes);
return marker;
}
Aggregations