diff options
author | Alexander Kurtakov | 2014-04-15 08:57:18 +0000 |
---|---|---|
committer | Alexander Kurtakov | 2014-04-15 10:09:48 +0000 |
commit | 98142e9b8ae6e1408706320980c22f38f4b4bf12 (patch) | |
tree | c6a61b789155d7fa2985a5f96944b7d3b3d8ccbe /gcov | |
parent | b61a696ebfe1df4ff7f39673040e355c7c2c0eeb (diff) | |
download | org.eclipse.linuxtools-98142e9b8ae6e1408706320980c22f38f4b4bf12.tar.gz org.eclipse.linuxtools-98142e9b8ae6e1408706320980c22f38f4b4bf12.tar.xz org.eclipse.linuxtools-98142e9b8ae6e1408706320980c22f38f4b4bf12.zip |
gcov: Fix warnings.
Thanks to Sonar for identifying them.
Change-Id: I1a20b2a0e752860e1d558fab82ab3b3689153ac2
Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
Reviewed-on: https://git.eclipse.org/r/25033
Tested-by: Hudson CI
Diffstat (limited to 'gcov')
4 files changed, 476 insertions, 476 deletions
diff --git a/gcov/org.eclipse.linuxtools.gcov.core/src/org/eclipse/linuxtools/internal/gcov/view/annotatedsource/GcovAnnotation.java b/gcov/org.eclipse.linuxtools.gcov.core/src/org/eclipse/linuxtools/internal/gcov/view/annotatedsource/GcovAnnotation.java index 4b3274fcb3..d39d3562d1 100644 --- a/gcov/org.eclipse.linuxtools.gcov.core/src/org/eclipse/linuxtools/internal/gcov/view/annotatedsource/GcovAnnotation.java +++ b/gcov/org.eclipse.linuxtools.gcov.core/src/org/eclipse/linuxtools/internal/gcov/view/annotatedsource/GcovAnnotation.java @@ -19,39 +19,39 @@ import org.eclipse.osgi.util.NLS; */ public class GcovAnnotation extends Annotation { - private static final String COVERAGE = "org.eclipse.gcov.CoverageAnnotation"; //$NON-NLS-1$ - private static final String NO_COVERAGE = "org.eclipse.gcov.NoCoverageAnnotation"; //$NON-NLS-1$ - - private final Position position; - private final long count; - - public GcovAnnotation(int offset, int length, long count) { - super(getAnnotationType(count), false, null); - this.position = new Position(offset, length); - this.count = count; - } - - public Position getPosition() { - return position; - } - - @Override - public String getText() { - if (count == 0) { - return Messages.CoverageAnnotationColumn_line_never_exec; - } else if (count == 1) { - return Messages.CoverageAnnotationColumn_line_exec_once; - } else if ( count > 0) { - return NLS.bind(Messages.CoverageAnnotationColumn_line_mulitiple_exec, Long.toString(count)); - } else { - return Messages.CoverageAnnotationColumn_non_exec_line; - } - } - - private static String getAnnotationType(long count) { - if (count == 0) { - return NO_COVERAGE; - } - return COVERAGE; - } + private static final String COVERAGE = "org.eclipse.gcov.CoverageAnnotation"; //$NON-NLS-1$ + private static final String NO_COVERAGE = "org.eclipse.gcov.NoCoverageAnnotation"; //$NON-NLS-1$ + + private final Position position; + private final long count; + + public GcovAnnotation(int offset, int length, long count) { + super(getAnnotationType(count), false, null); + this.position = new Position(offset, length); + this.count = count; + } + + public Position getPosition() { + return position; + } + + @Override + public String getText() { + if (count == 0) { + return Messages.CoverageAnnotationColumn_line_never_exec; + } else if (count == 1) { + return Messages.CoverageAnnotationColumn_line_exec_once; + } else if ( count > 0) { + return NLS.bind(Messages.CoverageAnnotationColumn_line_mulitiple_exec, Long.toString(count)); + } else { + return Messages.CoverageAnnotationColumn_non_exec_line; + } + } + + private static String getAnnotationType(long count) { + if (count == 0) { + return NO_COVERAGE; + } + return COVERAGE; + } } diff --git a/gcov/org.eclipse.linuxtools.gcov.core/src/org/eclipse/linuxtools/internal/gcov/view/annotatedsource/GcovAnnotationModel.java b/gcov/org.eclipse.linuxtools.gcov.core/src/org/eclipse/linuxtools/internal/gcov/view/annotatedsource/GcovAnnotationModel.java index 5bedee6209..e37b8225e0 100644 --- a/gcov/org.eclipse.linuxtools.gcov.core/src/org/eclipse/linuxtools/internal/gcov/view/annotatedsource/GcovAnnotationModel.java +++ b/gcov/org.eclipse.linuxtools.gcov.core/src/org/eclipse/linuxtools/internal/gcov/view/annotatedsource/GcovAnnotationModel.java @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.linuxtools.internal.gcov.view.annotatedsource; +import java.io.IOException; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Iterator; @@ -20,6 +21,7 @@ import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.ui.CDTUITools; import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.jface.text.BadLocationException; @@ -46,253 +48,251 @@ import org.eclipse.ui.texteditor.ITextEditor; */ public final class GcovAnnotationModel implements IAnnotationModel { - /** Key for identifying our model from other Editor models. */ - private static final Object KEY = new Object(); - - /** List of GcovAnnotation elements */ - private List<GcovAnnotation> annotations = new ArrayList<>(); - - /** List of IAnnotationModelListener */ - private List<IAnnotationModelListener> annotationModelListeners = new ArrayList<>(); - - private final ITextEditor editor; - private final IDocument document; - private int openConnections = 0; - private boolean annotated = false; - - private IDocumentListener documentListener = new IDocumentListener() { - @Override - public void documentChanged(DocumentEvent event) { - updateAnnotations(false); - } - - @Override - public void documentAboutToBeChanged(DocumentEvent event) {} - }; - - private GcovAnnotationModel(ITextEditor editor, IDocument document) { - this.editor = editor; - this.document = document; - updateAnnotations(true); - } - - /** - * Attaches a coverage annotation model for the given editor if the editor - * can be annotated. Does nothing if the model is already attached. - * - * @param editor Editor to which an annotation model should be attached - */ - public static void attach(ITextEditor editor) { - IDocumentProvider provider = editor.getDocumentProvider(); - if (provider == null) { - return; - } - IAnnotationModel model = provider.getAnnotationModel(editor.getEditorInput()); - if (!(model instanceof IAnnotationModelExtension)) { - return; - } - IAnnotationModelExtension modelex = (IAnnotationModelExtension) model; - IDocument document = provider.getDocument(editor.getEditorInput()); - - GcovAnnotationModel coveragemodel = (GcovAnnotationModel) modelex.getAnnotationModel(KEY); - if (coveragemodel == null) { - coveragemodel = new GcovAnnotationModel(editor, document); - modelex.addAnnotationModel(KEY, coveragemodel); - } else { - coveragemodel.updateAnnotations(false); - } - } - - public static void clear (ITextEditor editor) { - IDocumentProvider provider = editor.getDocumentProvider(); - if (provider == null) { - return; - } - IAnnotationModel model = provider.getAnnotationModel(editor.getEditorInput()); - if (!(model instanceof IAnnotationModelExtension)) { - return; - } - IAnnotationModelExtension modelex = (IAnnotationModelExtension) model; - IAnnotationModel coverageModel = modelex.getAnnotationModel(KEY); - if (coverageModel instanceof GcovAnnotationModel) { - ((GcovAnnotationModel) coverageModel).clear(); - } - } - - private void updateAnnotations(boolean force) { - // We do not annotate any editor displaying content of a project not tracked. - ICElement element = CDTUITools.getEditorInputCElement(editor.getEditorInput()); - if (!GcovAnnotationModelTracker.getInstance().containsProject(element.getCProject().getProject())) { - return; - } - - SourceFile coverage = findSourceCoverageForEditor(); - if (coverage != null) { - if (!annotated || force) { - createAnnotations(coverage); - } - } else { - if (annotated) { - clear(); - } - } - } - - private SourceFile findSourceCoverageForEditor() { - if (editor.isDirty()) { - return null; - } - final IEditorInput input = editor.getEditorInput(); - if (input == null) { - return null; - } - ICElement element = CDTUITools.getEditorInputCElement(input); - if (element == null) { - return null; - } - return findSourceCoverageForElement(element); - } - - private SourceFile findSourceCoverageForElement(ICElement element) { - List<SourceFile> sources = new ArrayList<> (); - ICProject cProject = element.getCProject(); - IPath target = GcovAnnotationModelTracker.getInstance().getBinaryPath(cProject.getProject()); - try { - IBinary[] binaries = cProject.getBinaryContainer().getBinaries(); - for (IBinary b : binaries) { - if (b.getResource().getLocation().equals(target)) { - CovManager covManager = new CovManager(b.getResource().getLocation().toOSString()); - covManager.processCovFiles(covManager.getGCDALocations(), null); - sources.addAll(covManager.getAllSrcs()); - } - } - } catch (Exception e) { - } - - for (SourceFile sf : sources) { - IPath sfPath = new Path(sf.getName()); - IFile file = STLink2SourceSupport.sharedInstance.getFileForPath(sfPath, cProject.getProject()); - if (file != null) { - if (element.getLocationURI().getPath().equals(file.getLocation().toOSString())) { - return sf; - } - } - } - - IPath binFolder = target.removeLastSegments(1); - for (SourceFile sf : sources) { - String sfPath = Paths.get(binFolder.toOSString(), sf.getName()).normalize().toString(); - if (sfPath.equals(element.getLocationURI().getPath())) { - return sf; - } - } - - return null; - } - - private void createAnnotations(SourceFile sourceFile) { - AnnotationModelEvent event = new AnnotationModelEvent(this); - clear(event); - List<Line> lines = sourceFile.getLines(); - for (int i = 0; i < lines.size(); i++) { - try { - Line line = lines.get((i+1) % lines.size()); - if (line.exists()) { - GcovAnnotation ca = new GcovAnnotation(document.getLineOffset(i), - document.getLineLength(i), line.getCount()); - annotations.add(ca); - event.annotationAdded(ca); - } - } catch (BadLocationException e) { - } - } - fireModelChanged(event); - annotated = true; - } - - private void clear() { - AnnotationModelEvent event = new AnnotationModelEvent(this); - clear(event); - fireModelChanged(event); - annotated = false; - } - - private void clear(AnnotationModelEvent event) { - for (final GcovAnnotation ca : annotations) { - event.annotationRemoved(ca, ca.getPosition()); - } - annotations.clear(); - } - - @Override - public void addAnnotationModelListener(IAnnotationModelListener listener) { - if (!annotationModelListeners.contains(listener)) { - annotationModelListeners.add(listener); - fireModelChanged(new AnnotationModelEvent(this, true)); - } - } - - @Override - public void removeAnnotationModelListener(IAnnotationModelListener listener) { - annotationModelListeners.remove(listener); - } - - private void fireModelChanged(AnnotationModelEvent event) { - event.markSealed(); - if (!event.isEmpty()) { - for (final IAnnotationModelListener l : annotationModelListeners) { - if (l instanceof IAnnotationModelListenerExtension) { - ((IAnnotationModelListenerExtension) l).modelChanged(event); - } else { - l.modelChanged(this); - } - } - } - } - - @Override - public void connect(IDocument document) { - if (this.document != document) { - throw new IllegalArgumentException("Can't connect to different document."); //$NON-NLS-1$ - } - for (final GcovAnnotation ca : annotations) { - try { - document.addPosition(ca.getPosition()); - } catch (BadLocationException ex) { - } - } - if (openConnections++ == 0) { - document.addDocumentListener(documentListener); - } - } - - @Override - public void disconnect(IDocument document) { - if (this.document != document) { - throw new IllegalArgumentException("Can't disconnect from different document."); //$NON-NLS-1$ - } - for (final GcovAnnotation ca : annotations) { - document.removePosition(ca.getPosition()); - } - if (--openConnections == 0) { - document.removeDocumentListener(documentListener); - } - } - - @Override - public Position getPosition(Annotation annotation) { - return (annotation instanceof GcovAnnotation) ? ((GcovAnnotation) annotation).getPosition() : null; - } - - @Override - public Iterator<?> getAnnotationIterator() { - return annotations.iterator(); - } - - @Override - public void addAnnotation(Annotation annotation, Position position) {} - - @Override - public void removeAnnotation(Annotation annotation) {} + /** Key for identifying our model from other Editor models. */ + private static final Object KEY = new Object(); + + /** List of GcovAnnotation elements */ + private List<GcovAnnotation> annotations = new ArrayList<>(); + + /** List of IAnnotationModelListener */ + private List<IAnnotationModelListener> annotationModelListeners = new ArrayList<>(); + + private final ITextEditor editor; + private final IDocument document; + private int openConnections = 0; + private boolean annotated = false; + + private IDocumentListener documentListener = new IDocumentListener() { + @Override + public void documentChanged(DocumentEvent event) { + updateAnnotations(false); + } + + @Override + public void documentAboutToBeChanged(DocumentEvent event) {} + }; + + private GcovAnnotationModel(ITextEditor editor, IDocument document) { + this.editor = editor; + this.document = document; + updateAnnotations(true); + } + + /** + * Attaches a coverage annotation model for the given editor if the editor + * can be annotated. Does nothing if the model is already attached. + * + * @param editor Editor to which an annotation model should be attached + */ + public static void attach(ITextEditor editor) { + IDocumentProvider provider = editor.getDocumentProvider(); + if (provider == null) { + return; + } + IAnnotationModel model = provider.getAnnotationModel(editor.getEditorInput()); + if (!(model instanceof IAnnotationModelExtension)) { + return; + } + IAnnotationModelExtension modelex = (IAnnotationModelExtension) model; + IDocument document = provider.getDocument(editor.getEditorInput()); + + GcovAnnotationModel coveragemodel = (GcovAnnotationModel) modelex.getAnnotationModel(KEY); + if (coveragemodel == null) { + coveragemodel = new GcovAnnotationModel(editor, document); + modelex.addAnnotationModel(KEY, coveragemodel); + } else { + coveragemodel.updateAnnotations(false); + } + } + + public static void clear (ITextEditor editor) { + IDocumentProvider provider = editor.getDocumentProvider(); + if (provider == null) { + return; + } + IAnnotationModel model = provider.getAnnotationModel(editor.getEditorInput()); + if (!(model instanceof IAnnotationModelExtension)) { + return; + } + IAnnotationModelExtension modelex = (IAnnotationModelExtension) model; + IAnnotationModel coverageModel = modelex.getAnnotationModel(KEY); + if (coverageModel instanceof GcovAnnotationModel) { + ((GcovAnnotationModel) coverageModel).clear(); + } + } + + private void updateAnnotations(boolean force) { + // We do not annotate any editor displaying content of a project not tracked. + ICElement element = CDTUITools.getEditorInputCElement(editor.getEditorInput()); + if (!GcovAnnotationModelTracker.getInstance().containsProject(element.getCProject().getProject())) { + return; + } + + SourceFile coverage = findSourceCoverageForEditor(); + if (coverage != null) { + if (!annotated || force) { + createAnnotations(coverage); + } + } else { + if (annotated) { + clear(); + } + } + } + + private SourceFile findSourceCoverageForEditor() { + if (editor.isDirty()) { + return null; + } + final IEditorInput input = editor.getEditorInput(); + if (input == null) { + return null; + } + ICElement element = CDTUITools.getEditorInputCElement(input); + if (element == null) { + return null; + } + return findSourceCoverageForElement(element); + } + + private SourceFile findSourceCoverageForElement(ICElement element) { + List<SourceFile> sources = new ArrayList<> (); + ICProject cProject = element.getCProject(); + IPath target = GcovAnnotationModelTracker.getInstance().getBinaryPath(cProject.getProject()); + try { + IBinary[] binaries = cProject.getBinaryContainer().getBinaries(); + for (IBinary b : binaries) { + if (b.getResource().getLocation().equals(target)) { + CovManager covManager = new CovManager(b.getResource().getLocation().toOSString()); + covManager.processCovFiles(covManager.getGCDALocations(), null); + sources.addAll(covManager.getAllSrcs()); + } + } + } catch (IOException|CoreException|InterruptedException e) { + } + + for (SourceFile sf : sources) { + IPath sfPath = new Path(sf.getName()); + IFile file = STLink2SourceSupport.sharedInstance.getFileForPath(sfPath, cProject.getProject()); + if (file != null && element.getLocationURI().getPath().equals(file.getLocation().toOSString())) { + return sf; + } + } + + IPath binFolder = target.removeLastSegments(1); + for (SourceFile sf : sources) { + String sfPath = Paths.get(binFolder.toOSString(), sf.getName()).normalize().toString(); + if (sfPath.equals(element.getLocationURI().getPath())) { + return sf; + } + } + + return null; + } + + private void createAnnotations(SourceFile sourceFile) { + AnnotationModelEvent event = new AnnotationModelEvent(this); + clear(event); + List<Line> lines = sourceFile.getLines(); + for (int i = 0; i < lines.size(); i++) { + try { + Line line = lines.get((i+1) % lines.size()); + if (line.exists()) { + GcovAnnotation ca = new GcovAnnotation(document.getLineOffset(i), + document.getLineLength(i), line.getCount()); + annotations.add(ca); + event.annotationAdded(ca); + } + } catch (BadLocationException e) { + } + } + fireModelChanged(event); + annotated = true; + } + + private void clear() { + AnnotationModelEvent event = new AnnotationModelEvent(this); + clear(event); + fireModelChanged(event); + annotated = false; + } + + private void clear(AnnotationModelEvent event) { + for (final GcovAnnotation ca : annotations) { + event.annotationRemoved(ca, ca.getPosition()); + } + annotations.clear(); + } + + @Override + public void addAnnotationModelListener(IAnnotationModelListener listener) { + if (!annotationModelListeners.contains(listener)) { + annotationModelListeners.add(listener); + fireModelChanged(new AnnotationModelEvent(this, true)); + } + } + + @Override + public void removeAnnotationModelListener(IAnnotationModelListener listener) { + annotationModelListeners.remove(listener); + } + + private void fireModelChanged(AnnotationModelEvent event) { + event.markSealed(); + if (!event.isEmpty()) { + for (final IAnnotationModelListener l : annotationModelListeners) { + if (l instanceof IAnnotationModelListenerExtension) { + ((IAnnotationModelListenerExtension) l).modelChanged(event); + } else { + l.modelChanged(this); + } + } + } + } + + @Override + public void connect(IDocument document) { + if (this.document != document) { + throw new IllegalArgumentException("Can't connect to different document."); //$NON-NLS-1$ + } + for (final GcovAnnotation ca : annotations) { + try { + document.addPosition(ca.getPosition()); + } catch (BadLocationException ex) { + } + } + if (openConnections++ == 0) { + document.addDocumentListener(documentListener); + } + } + + @Override + public void disconnect(IDocument document) { + if (this.document != document) { + throw new IllegalArgumentException("Can't disconnect from different document."); //$NON-NLS-1$ + } + for (final GcovAnnotation ca : annotations) { + document.removePosition(ca.getPosition()); + } + if (--openConnections == 0) { + document.removeDocumentListener(documentListener); + } + } + + @Override + public Position getPosition(Annotation annotation) { + return (annotation instanceof GcovAnnotation) ? ((GcovAnnotation) annotation).getPosition() : null; + } + + @Override + public Iterator<?> getAnnotationIterator() { + return annotations.iterator(); + } + + @Override + public void addAnnotation(Annotation annotation, Position position) {} + + @Override + public void removeAnnotation(Annotation annotation) {} } diff --git a/gcov/org.eclipse.linuxtools.gcov.core/src/org/eclipse/linuxtools/internal/gcov/view/annotatedsource/GcovAnnotationModelTracker.java b/gcov/org.eclipse.linuxtools.gcov.core/src/org/eclipse/linuxtools/internal/gcov/view/annotatedsource/GcovAnnotationModelTracker.java index 0eed8fdad5..06d3208623 100644 --- a/gcov/org.eclipse.linuxtools.gcov.core/src/org/eclipse/linuxtools/internal/gcov/view/annotatedsource/GcovAnnotationModelTracker.java +++ b/gcov/org.eclipse.linuxtools.gcov.core/src/org/eclipse/linuxtools/internal/gcov/view/annotatedsource/GcovAnnotationModelTracker.java @@ -39,194 +39,194 @@ import org.eclipse.ui.texteditor.ITextEditor; * Keep track of windows/parts and listen for opened editors to add * the annotation model. */ -public class GcovAnnotationModelTracker { - - private static GcovAnnotationModelTracker single; - private final IWorkbench workbench; - private final Map<IProject, IPath> trackedProjects = new HashMap<>(); - - /** - * Add/Remove a part listener to every window open/closed. - */ - private IWindowListener windowListener = new IWindowListener() { - @Override - public void windowOpened(IWorkbenchWindow window) { - window.getPartService().addPartListener(partListener); - } - - @Override - public void windowClosed(IWorkbenchWindow window) { - window.getPartService().removePartListener(partListener); - } - - @Override - public void windowActivated(IWorkbenchWindow window) {} - - @Override - public void windowDeactivated(IWorkbenchWindow window) {} - }; - - /** - * Add the GcovAnnotationModel to any part that contains an - * instance of ICEditor. - */ - private IPartListener2 partListener = new IPartListener2() { - @Override - public void partOpened(IWorkbenchPartReference partref) { - annotateCEditor(partref); - } - - @Override - public void partActivated(IWorkbenchPartReference partRef) {} - - @Override - public void partBroughtToTop(IWorkbenchPartReference partRef) {} - - @Override - public void partClosed(IWorkbenchPartReference partRef) {} - - @Override - public void partDeactivated(IWorkbenchPartReference partRef) {} - - @Override - public void partHidden(IWorkbenchPartReference partRef) {} - - @Override - public void partVisible(IWorkbenchPartReference partRef) {} - - @Override - public void partInputChanged(IWorkbenchPartReference partRef) {} - }; - - private GcovAnnotationModelTracker (IWorkbench workbench) { - this.workbench = workbench; - - // Add part listener for current windows - for (IWorkbenchWindow w : workbench.getWorkbenchWindows()) { - w.getPartService().addPartListener(partListener); - } - - // Add window listener to workbench for future windows - workbench.addWindowListener(windowListener); - } - - public static GcovAnnotationModelTracker getInstance () { - if (single == null) { - single = new GcovAnnotationModelTracker(PlatformUI.getWorkbench()); - } - return single; - } - - public IPath getBinaryPath (IProject project) { - return trackedProjects.get(project); - } - - public boolean containsProject (IProject project) { - return trackedProjects.containsKey(project); - } - - public void addProject (IProject project, IPath binary) { - trackedProjects.put(project, binary); - } - - public void dispose() { - workbench.removeWindowListener(windowListener); - for (IWorkbenchWindow w : workbench.getWorkbenchWindows()) { - w.getPartService().removePartListener(partListener); - } - } - - public void annotateAllCEditors() { - for (IWorkbenchWindow w : workbench.getWorkbenchWindows()) { - for (IWorkbenchPage p : w.getPages()) { - for (IEditorReference e : p.getEditorReferences()) { - annotateCEditor(e); - } - } - } - } - - private void annotateCEditor(IWorkbenchPartReference partref) { - IWorkbenchPart part = partref.getPart(false); - if (part instanceof ICEditor) { - ICEditor editor = (ICEditor) part; - ICElement element = CDTUITools.getEditorInputCElement(editor.getEditorInput()); - IProject project = element.getCProject().getProject(); - - // Attach our annotation model to any compatible editor. (ICEditor) - GcovAnnotationModel.attach((ITextEditor) part); - // If a user triggers a build we will not render annotations. - ResourcesPlugin.getWorkspace().addResourceChangeListener( - new ProjectBuildListener(project, editor), - IResourceChangeEvent.POST_BUILD); - } - } - - - private class ProjectBuildListener implements IResourceChangeListener { - - // project to keep track of - private IProject project; - private ICEditor editor; - - public ProjectBuildListener(IProject targetProject, ICEditor editor) { - this.editor = editor; - this.project = targetProject; - } - - @Override - public void resourceChanged(IResourceChangeEvent event) { - if (project != null && isPostBuildEvent(event)) { - - // find the project from event delta and delete its markers - IResourceDelta delta = event.getDelta(); - IResourceDelta[] childrenDelta = delta.getAffectedChildren(IResourceDelta.CHANGED); - for (IResourceDelta childDelta : childrenDelta) { - if (isProjectDelta(childDelta, project)) { - - // do not track this project and de-register this listener - GcovAnnotationModel.clear(editor); - ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); - trackedProjects.remove(project); - } - } - } - } - - /** - * Check if {@link IResourceDelta} represents a change in the specified {@link IProject}.. - * - * @param delta IResourceDelta resource delta to check - * @param project IProject project to compare against - * @return boolean true if IResourceDelta is a project and equals the - */ - public boolean isProjectDelta(IResourceDelta delta, IProject project){ - if(delta != null){ - IResource resource = delta.getResource(); - return delta.getKind() == IResourceDelta.CHANGED - && resource != null - && resource.getType() == IResource.PROJECT - && resource.equals(project); - } - return false; - } - - /** - * Check if {@link IResourceChangeEvent} is a post-build event. - * - * @param event IResourceChangeEvent event to check - * @return boolean true if IResourceChangeEvent is a post-build event, false - * otherwise - */ - public boolean isPostBuildEvent(IResourceChangeEvent event) { - if(event != null){ - int buildKind = event.getBuildKind(); - return event.getType() == IResourceChangeEvent.POST_BUILD - && (buildKind == IncrementalProjectBuilder.FULL_BUILD - || buildKind == IncrementalProjectBuilder.INCREMENTAL_BUILD - || buildKind == IncrementalProjectBuilder.CLEAN_BUILD); - } - return false; - } - } +public final class GcovAnnotationModelTracker { + + private static GcovAnnotationModelTracker single; + private final IWorkbench workbench; + private final Map<IProject, IPath> trackedProjects = new HashMap<>(); + + /** + * Add/Remove a part listener to every window open/closed. + */ + private IWindowListener windowListener = new IWindowListener() { + @Override + public void windowOpened(IWorkbenchWindow window) { + window.getPartService().addPartListener(partListener); + } + + @Override + public void windowClosed(IWorkbenchWindow window) { + window.getPartService().removePartListener(partListener); + } + + @Override + public void windowActivated(IWorkbenchWindow window) {} + + @Override + public void windowDeactivated(IWorkbenchWindow window) {} + }; + + /** + * Add the GcovAnnotationModel to any part that contains an + * instance of ICEditor. + */ + private IPartListener2 partListener = new IPartListener2() { + @Override + public void partOpened(IWorkbenchPartReference partref) { + annotateCEditor(partref); + } + + @Override + public void partActivated(IWorkbenchPartReference partRef) {} + + @Override + public void partBroughtToTop(IWorkbenchPartReference partRef) {} + + @Override + public void partClosed(IWorkbenchPartReference partRef) {} + + @Override + public void partDeactivated(IWorkbenchPartReference partRef) {} + + @Override + public void partHidden(IWorkbenchPartReference partRef) {} + + @Override + public void partVisible(IWorkbenchPartReference partRef) {} + + @Override + public void partInputChanged(IWorkbenchPartReference partRef) {} + }; + + private GcovAnnotationModelTracker (IWorkbench workbench) { + this.workbench = workbench; + + // Add part listener for current windows + for (IWorkbenchWindow w : workbench.getWorkbenchWindows()) { + w.getPartService().addPartListener(partListener); + } + + // Add window listener to workbench for future windows + workbench.addWindowListener(windowListener); + } + + public static GcovAnnotationModelTracker getInstance () { + if (single == null) { + single = new GcovAnnotationModelTracker(PlatformUI.getWorkbench()); + } + return single; + } + + public IPath getBinaryPath (IProject project) { + return trackedProjects.get(project); + } + + public boolean containsProject (IProject project) { + return trackedProjects.containsKey(project); + } + + public void addProject (IProject project, IPath binary) { + trackedProjects.put(project, binary); + } + + public void dispose() { + workbench.removeWindowListener(windowListener); + for (IWorkbenchWindow w : workbench.getWorkbenchWindows()) { + w.getPartService().removePartListener(partListener); + } + } + + public void annotateAllCEditors() { + for (IWorkbenchWindow w : workbench.getWorkbenchWindows()) { + for (IWorkbenchPage p : w.getPages()) { + for (IEditorReference e : p.getEditorReferences()) { + annotateCEditor(e); + } + } + } + } + + private void annotateCEditor(IWorkbenchPartReference partref) { + IWorkbenchPart part = partref.getPart(false); + if (part instanceof ICEditor) { + ICEditor editor = (ICEditor) part; + ICElement element = CDTUITools.getEditorInputCElement(editor.getEditorInput()); + IProject project = element.getCProject().getProject(); + + // Attach our annotation model to any compatible editor. (ICEditor) + GcovAnnotationModel.attach((ITextEditor) part); + // If a user triggers a build we will not render annotations. + ResourcesPlugin.getWorkspace().addResourceChangeListener( + new ProjectBuildListener(project, editor), + IResourceChangeEvent.POST_BUILD); + } + } + + + private class ProjectBuildListener implements IResourceChangeListener { + + // project to keep track of + private IProject project; + private ICEditor editor; + + public ProjectBuildListener(IProject targetProject, ICEditor editor) { + this.editor = editor; + this.project = targetProject; + } + + @Override + public void resourceChanged(IResourceChangeEvent event) { + if (project != null && isPostBuildEvent(event)) { + + // find the project from event delta and delete its markers + IResourceDelta delta = event.getDelta(); + IResourceDelta[] childrenDelta = delta.getAffectedChildren(IResourceDelta.CHANGED); + for (IResourceDelta childDelta : childrenDelta) { + if (isProjectDelta(childDelta, project)) { + + // do not track this project and de-register this listener + GcovAnnotationModel.clear(editor); + ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); + trackedProjects.remove(project); + } + } + } + } + + /** + * Check if {@link IResourceDelta} represents a change in the specified {@link IProject}.. + * + * @param delta IResourceDelta resource delta to check + * @param project IProject project to compare against + * @return boolean true if IResourceDelta is a project and equals the + */ + public boolean isProjectDelta(IResourceDelta delta, IProject project){ + if(delta != null){ + IResource resource = delta.getResource(); + return delta.getKind() == IResourceDelta.CHANGED + && resource != null + && resource.getType() == IResource.PROJECT + && resource.equals(project); + } + return false; + } + + /** + * Check if {@link IResourceChangeEvent} is a post-build event. + * + * @param event IResourceChangeEvent event to check + * @return boolean true if IResourceChangeEvent is a post-build event, false + * otherwise + */ + private boolean isPostBuildEvent(IResourceChangeEvent event) { + if(event != null){ + int buildKind = event.getBuildKind(); + return event.getType() == IResourceChangeEvent.POST_BUILD + && (buildKind == IncrementalProjectBuilder.FULL_BUILD + || buildKind == IncrementalProjectBuilder.INCREMENTAL_BUILD + || buildKind == IncrementalProjectBuilder.CLEAN_BUILD); + } + return false; + } + } } diff --git a/gcov/org.eclipse.linuxtools.gcov.launch/src/org/eclipse/linuxtools/internal/gcov/launch/GcovLaunchConfigurationDelegate.java b/gcov/org.eclipse.linuxtools.gcov.launch/src/org/eclipse/linuxtools/internal/gcov/launch/GcovLaunchConfigurationDelegate.java index 010236b5fc..1490a84366 100644 --- a/gcov/org.eclipse.linuxtools.gcov.launch/src/org/eclipse/linuxtools/internal/gcov/launch/GcovLaunchConfigurationDelegate.java +++ b/gcov/org.eclipse.linuxtools.gcov.launch/src/org/eclipse/linuxtools/internal/gcov/launch/GcovLaunchConfigurationDelegate.java @@ -101,7 +101,7 @@ public class GcovLaunchConfigurationDelegate extends ProfileLaunchConfigurationD try { List<String> gcdaPaths = cvrgeMnger.getGCDALocations(); - if (gcdaPaths.size() == 0) { + if (gcdaPaths.isEmpty()) { String title = GcovLaunchMessages.GcovCompilerOptions_msg; String message = GcovLaunchMessages.GcovCompileAgain_msg; Shell parent = PlatformUI.getWorkbench().getDisplay().getActiveShell(); @@ -135,7 +135,7 @@ public class GcovLaunchConfigurationDelegate extends ProfileLaunchConfigurationD /* all these functions exist to be overridden by the test class in order to allow launch testing */ - protected IProject getProject(){ + private IProject getProject(){ try{ return CDebugUtils.verifyCProject(config).getProject(); } catch (CoreException e) { @@ -151,7 +151,7 @@ public class GcovLaunchConfigurationDelegate extends ProfileLaunchConfigurationD * @throws CoreException * @since 1.1 */ - protected IPath getExePath(ILaunchConfiguration config) throws CoreException{ + private static IPath getExePath(ILaunchConfiguration config) throws CoreException{ return CDebugUtils.verifyProgramPath( config ); } |