Search in sources :

Example 51 with IMarker

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);
}
Also used : IMarker(org.eclipse.core.resources.IMarker) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 52 with IMarker

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);
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) AST(org.eclipse.jdt.core.dom.AST) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IMarker(org.eclipse.core.resources.IMarker) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 53 with IMarker

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);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) AST(org.eclipse.jdt.core.dom.AST) WrappingPMDMarker(ch.acanda.eclipse.pmd.marker.WrappingPMDMarker) IMarker(org.eclipse.core.resources.IMarker)

Example 54 with IMarker

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);
}
Also used : IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IMarker(org.eclipse.core.resources.IMarker)

Example 55 with IMarker

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;
}
Also used : IMarker(org.eclipse.core.resources.IMarker)

Aggregations

IMarker (org.eclipse.core.resources.IMarker)115 CoreException (org.eclipse.core.runtime.CoreException)31 Test (org.junit.Test)31 IFile (org.eclipse.core.resources.IFile)23 IResource (org.eclipse.core.resources.IResource)16 ArrayList (java.util.ArrayList)15 IProject (org.eclipse.core.resources.IProject)8 IPath (org.eclipse.core.runtime.IPath)8 Position (org.eclipse.jface.text.Position)8 Annotation (org.eclipse.jface.text.source.Annotation)7 Matchers.anyString (org.mockito.Matchers.anyString)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 Rule (net.sourceforge.pmd.Rule)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 BadLocationException (org.eclipse.jface.text.BadLocationException)6 RuleViolation (net.sourceforge.pmd.RuleViolation)5 JavaLanguageModule (net.sourceforge.pmd.lang.java.JavaLanguageModule)5 IDocument (org.eclipse.jface.text.IDocument)5 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)5