Search in sources :

Example 1 with MarkerHandler

use of org.eclipse.titanium.markers.handler.MarkerHandler in project titan.EclipsePlug-ins by eclipse.

the class Analyzer method analyzeModule.

/**
 * Analyze a single module.
 * <p>
 * Executes the configured code smell spotters on the given module (and if
 * the <code>Analyzer</code> uses project-scoped code smell spotters, then
 * those are executed, too, on the project of the module). Locking the
 * project to prevent modification of the AST is handled internally.
 *
 * @param monitor
 *            shows progress and makes it interruptable
 * @param module
 *            the ttcn3 module to analyze
 *
 * @return the code smells found in the given module
 */
public MarkerHandler analyzeModule(final IProgressMonitor monitor, final Module module) {
    final SubMonitor progress = SubMonitor.convert(monitor, 100);
    final IResource res = module.getLocation().getFile();
    final Map<IResource, List<Marker>> markers = new HashMap<IResource, List<Marker>>();
    markers.put(res, internalAnalyzeModule(module));
    progress.worked(80);
    if (progress.isCanceled()) {
        throw new OperationCanceledException();
    }
    final IProject project = module.getProject();
    markers.put(project, internalAnalyzeProject(project));
    progress.worked(20);
    return new MarkerHandler(markers);
}
Also used : HashMap(java.util.HashMap) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) ArrayList(java.util.ArrayList) List(java.util.List) Marker(org.eclipse.titanium.markers.handler.Marker) MarkerHandler(org.eclipse.titanium.markers.handler.MarkerHandler) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject)

Example 2 with MarkerHandler

use of org.eclipse.titanium.markers.handler.MarkerHandler in project titan.EclipsePlug-ins by eclipse.

the class CustomExpansionListener method performOk.

@Override
public boolean performOk() {
    final boolean result = super.performOk();
    // the others should be checked too.
    if (changed) {
        ErrorReporter.parallelWarningDisplayInMessageDialog("Code smell markers", "Settings of the code smell analyzer have changed," + " the known projects have to be re-analyzed completly.\nThis might take some time.");
        final IProject[] projs = ResourcesPlugin.getWorkspace().getRoot().getProjects();
        final Analyzer analyzer = AnalyzerCache.withPreference();
        for (final IProject project : projs) {
            if (TITANNature.hasTITANNature(project)) {
                final WorkspaceJob op = new WorkspaceJob("Code smells") {

                    @Override
                    public IStatus runInWorkspace(final IProgressMonitor monitor) {
                        MarkerHandler mh;
                        synchronized (project) {
                            mh = analyzer.analyzeProject(monitor, project);
                        }
                        mh.showAll(project);
                        return Status.OK_STATUS;
                    }
                };
                op.setPriority(Job.SHORT);
                op.setSystem(false);
                op.setUser(true);
                op.schedule();
            }
        }
    }
    return result;
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) Analyzer(org.eclipse.titanium.markers.utils.Analyzer) MarkerHandler(org.eclipse.titanium.markers.handler.MarkerHandler) IProject(org.eclipse.core.resources.IProject)

Example 3 with MarkerHandler

use of org.eclipse.titanium.markers.handler.MarkerHandler in project titan.EclipsePlug-ins by eclipse.

the class CsvProblemExporter method exportMarkers.

/**
 * Export the code smells of a project to CSV files.
 * <p>
 * There is always a CSV with the name summary, which contains the names of
 * all code smells, and their occurrence in the actual project. And for each
 * code smell, where at least 1 occurrence was present a separate CSV is
 * created too. In this CSV the reported error messages, the path of the
 * resources and the line number in which the smell was found is listed.
 *
 * Note: All code smell types are used in the analysis and are written in
 * the output. Some code smells use external settings, which can be fine
 * tuned on the preference page.
 *
 * @param filenamePrefix
 *            the filename prefix to be used in creating the CSV files.
 * @param date
 *            the time stamp to be used (currently unused)
 *
 * @throws IOException
 *             when writing the file fails
 */
@Override
public void exportMarkers(final IProgressMonitor monitor, final String filenamePrefix, final Date date) throws IOException {
    final SubMonitor progress = SubMonitor.convert(monitor, 100);
    final PrintWriter summaryFile = new PrintWriter(new FileWriter(filenamePrefix + "_summary.csv"));
    final PrintWriter timesFile = new PrintWriter(new FileWriter(filenamePrefix + "_times.csv"));
    try {
        summaryFile.println("Smell name" + SEPARATOR + "Amount");
        timesFile.println("Smell name" + SEPARATOR + "Minimal repair time" + SEPARATOR + "Average repair time" + SEPARATOR + "Maximal repair time");
        PrintWriter actualSmellFile = null;
        final Map<TaskType, List<IMarker>> markers = collectMarkers();
        for (final TaskType actSmell : TaskType.values()) {
            int row = 0;
            if (!markers.get(actSmell).isEmpty()) {
                actualSmellFile = new PrintWriter(new FileWriter(filenamePrefix + "_" + actSmell.getHumanReadableName() + ".csv"));
                actualSmellFile.println("Message" + SEPARATOR + "Smell name" + SEPARATOR + "Line number");
                for (final IMarker m : markers.get(actSmell)) {
                    actualSmellFile.println(m.getAttribute(IMarker.MESSAGE).toString() + SEPARATOR + m.getResource().getName() + SEPARATOR + m.getAttribute(IMarker.LINE_NUMBER));
                    ++row;
                }
                actualSmellFile.close();
                actualSmellFile = null;
            }
            summaryFile.println(actSmell.getHumanReadableName() + SEPARATOR + row);
            timesFile.println(actSmell.getHumanReadableName() + SEPARATOR + row * actSmell.getMinRepairTime() + SEPARATOR + row * actSmell.getAvgRepairTime() + SEPARATOR + row * actSmell.getMaxRepairTime());
        }
        progress.worked(20);
        final MarkerHandler mh = AnalyzerCache.withAll().analyzeProject(progress.newChild(30), project);
        progress.setWorkRemaining(CodeSmellType.values().length);
        for (final CodeSmellType actSmell : CodeSmellType.values()) {
            int row = 0;
            if (!mh.get(actSmell).isEmpty()) {
                actualSmellFile = new PrintWriter(new FileWriter(filenamePrefix + "_" + actSmell.name() + ".csv"));
                actualSmellFile.println("Description; Resurce; Location");
                for (final Marker m : mh.get(actSmell)) {
                    if (m.getLine() == -1 || m.getResource() == null) {
                        continue;
                    }
                    actualSmellFile.println(m.getMessage() + SEPARATOR + m.getResource().getName() + SEPARATOR + m.getLine());
                    ++row;
                }
                actualSmellFile.close();
                actualSmellFile = null;
            }
            summaryFile.println(actSmell.getHumanReadableName() + SEPARATOR + row + SEPARATOR + row * actSmell.getMinRepairTime() + SEPARATOR + row * actSmell.getAvgRepairTime() + SEPARATOR + row * actSmell.getMaxRepairTime());
            timesFile.println(actSmell.getHumanReadableName() + SEPARATOR + row * actSmell.getMinRepairTime() + SEPARATOR + row * actSmell.getAvgRepairTime() + SEPARATOR + row * actSmell.getMaxRepairTime());
            progress.worked(1);
        }
    } catch (CoreException e) {
        ErrorReporter.logExceptionStackTrace("Error while exporting to csv", e);
    } finally {
        summaryFile.close();
        timesFile.close();
    }
}
Also used : CodeSmellType(org.eclipse.titanium.markers.types.CodeSmellType) CoreException(org.eclipse.core.runtime.CoreException) FileWriter(java.io.FileWriter) TaskType(org.eclipse.titanium.markers.types.TaskType) SubMonitor(org.eclipse.core.runtime.SubMonitor) List(java.util.List) IMarker(org.eclipse.core.resources.IMarker) Marker(org.eclipse.titanium.markers.handler.Marker) IMarker(org.eclipse.core.resources.IMarker) MarkerHandler(org.eclipse.titanium.markers.handler.MarkerHandler) PrintWriter(java.io.PrintWriter)

Example 4 with MarkerHandler

use of org.eclipse.titanium.markers.handler.MarkerHandler in project titan.EclipsePlug-ins by eclipse.

the class Expectation method runTest.

public void runTest() {
    Map<String, List<Integer>> actualMarkers = new HashMap<String, List<Integer>>();
    // analyze the modules, and collect the related markers
    IProject project = WorkspaceHandlingLibrary.getWorkspace().getRoot().getProject(CustomConfigurable.PROJECT_TO_USE);
    ProjectSourceParser parser = GlobalParser.getProjectSourceParser(project);
    for (String modName : expectedMarkers.keySet()) {
        Module mod = parser.getModuleByName(modName);
        IResource file = mod.getLocation().getFile();
        MarkerHandler mh = Analyzer.builder().addProblem(type).build().analyzeModule(new NullProgressMonitor(), mod);
        List<Integer> lines = new ArrayList<Integer>();
        for (Marker m : mh.get(file)) {
            if (m.getProblemType() == type && m.getLine() != -1) {
                lines.add(m.getLine());
            }
        }
        // save the line number of markers that were from our problem type
        actualMarkers.put(modName, lines);
    }
    // check whether the reality complies our expectations
    for (String modName : expectedMarkers.keySet()) {
        for (Integer ln : expectedMarkers.get(modName)) {
            if (!actualMarkers.get(modName).remove(ln)) {
                fail("We expected a marker in " + modName + " at line " + ln + ", but have not found it");
            }
        }
    }
    // Check whether we managed to consume all markers that showed up during the analysis
    for (String modName : actualMarkers.keySet()) {
        for (Integer ln : actualMarkers.get(modName)) {
            fail("Unexpected marker in " + modName + " at line " + ln);
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Marker(org.eclipse.titanium.markers.handler.Marker) IMarker(org.eclipse.core.resources.IMarker) IProject(org.eclipse.core.resources.IProject) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) ArrayList(java.util.ArrayList) List(java.util.List) Module(org.eclipse.titan.designer.AST.Module) MarkerHandler(org.eclipse.titanium.markers.handler.MarkerHandler) IResource(org.eclipse.core.resources.IResource)

Example 5 with MarkerHandler

use of org.eclipse.titanium.markers.handler.MarkerHandler in project titan.EclipsePlug-ins by eclipse.

the class Analyzer method analyzeProject.

/**
 * Analyze a whole project.
 * <p>
 * Executes the configured code smell spotters on the given project. Locking
 * the project to prevent modification of the AST is handled internally.
 *
 * @param monitor
 *            shows progress and makes it interruptable
 * @param module
 *            the ttcn3 project to analyze
 *
 * @return the code smells found in the given project
 */
public MarkerHandler analyzeProject(final IProgressMonitor monitor, final IProject project) {
    final ProjectSourceParser projectSourceParser = GlobalParser.getProjectSourceParser(project);
    final Set<String> knownModuleNames = projectSourceParser.getKnownModuleNames();
    final SubMonitor progress = SubMonitor.convert(monitor, 1 + knownModuleNames.size());
    progress.subTask("Project level analysis");
    final Map<IResource, List<Marker>> markers = new HashMap<IResource, List<Marker>>();
    markers.put(project, internalAnalyzeProject(project));
    progress.worked(1);
    for (final String moduleName : knownModuleNames) {
        if (progress.isCanceled()) {
            throw new OperationCanceledException();
        }
        final Module mod = projectSourceParser.getModuleByName(moduleName);
        progress.subTask("Analyzing module " + mod.getName());
        final IResource moduleResource = mod.getLocation().getFile();
        markers.put(moduleResource, internalAnalyzeModule(mod));
        progress.worked(1);
    }
    return new MarkerHandler(markers);
}
Also used : HashMap(java.util.HashMap) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) ArrayList(java.util.ArrayList) List(java.util.List) Marker(org.eclipse.titanium.markers.handler.Marker) Module(org.eclipse.titan.designer.AST.Module) MarkerHandler(org.eclipse.titanium.markers.handler.MarkerHandler) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) IResource(org.eclipse.core.resources.IResource)

Aggregations

MarkerHandler (org.eclipse.titanium.markers.handler.MarkerHandler)8 List (java.util.List)6 SubMonitor (org.eclipse.core.runtime.SubMonitor)5 Marker (org.eclipse.titanium.markers.handler.Marker)5 HashMap (java.util.HashMap)4 IProject (org.eclipse.core.resources.IProject)4 IResource (org.eclipse.core.resources.IResource)4 ArrayList (java.util.ArrayList)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 IMarker (org.eclipse.core.resources.IMarker)2 CoreException (org.eclipse.core.runtime.CoreException)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 Module (org.eclipse.titan.designer.AST.Module)2 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)2 CodeSmellType (org.eclipse.titanium.markers.types.CodeSmellType)2 TaskType (org.eclipse.titanium.markers.types.TaskType)2 BufferedWriter (java.io.BufferedWriter)1 FileInputStream (java.io.FileInputStream)1