@Override public void scanFile(JavaFileScannerContext context) { sonarFile = fs.inputFile(fs.predicates().is(context.getFile())); classTrees.clear(); methods = 0; complexityInMethods = 0; accessors = 0; classes = 0; PublicApiChecker publicApiChecker = PublicApiChecker.newInstanceWithAccessorsHandledAsMethods(); if (separateAccessorsFromMethods) { publicApiChecker = PublicApiChecker.newInstanceWithAccessorsSeparatedFromMethods(); } publicApiChecker.scan(context.getTree()); methodComplexityDistribution = new RangeDistributionBuilder( CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, LIMITS_COMPLEXITY_METHODS); CommentLinesVisitor commentLinesVisitor = new CommentLinesVisitor(); commentLinesVisitor.analyzeCommentLines(context.getTree()); noSonarFilter.addComponent( sensorContext.getResource(sonarFile).getEffectiveKey(), commentLinesVisitor.noSonarLines()); super.scanFile(context); // leave file. int fileComplexity = context.getComplexityNodes(context.getTree()).size(); saveMetricOnFile(CoreMetrics.CLASSES, classes); saveMetricOnFile(CoreMetrics.FUNCTIONS, methods); saveMetricOnFile(CoreMetrics.ACCESSORS, accessors); saveMetricOnFile(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, complexityInMethods); saveMetricOnFile(CoreMetrics.COMPLEXITY_IN_CLASSES, fileComplexity); saveMetricOnFile(CoreMetrics.COMPLEXITY, fileComplexity); saveMetricOnFile(CoreMetrics.PUBLIC_API, publicApiChecker.getPublicApi()); saveMetricOnFile( CoreMetrics.PUBLIC_DOCUMENTED_API_DENSITY, publicApiChecker.getDocumentedPublicApiDensity()); saveMetricOnFile( CoreMetrics.PUBLIC_UNDOCUMENTED_API, publicApiChecker.getUndocumentedPublicApi()); saveMetricOnFile(CoreMetrics.COMMENT_LINES, commentLinesVisitor.commentLinesMetric()); saveMetricOnFile( CoreMetrics.STATEMENTS, new StatementVisitor().numberOfStatements(context.getTree())); saveMetricOnFile(CoreMetrics.NCLOC, new LinesOfCodeVisitor().linesOfCode(context.getTree())); sensorContext.saveMeasure( sonarFile, methodComplexityDistribution.build(true).setPersistenceMode(PersistenceMode.MEMORY)); RangeDistributionBuilder fileComplexityDistribution = new RangeDistributionBuilder( CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION, LIMITS_COMPLEXITY_FILES); sensorContext.saveMeasure( sonarFile, fileComplexityDistribution .add(fileComplexity) .build(true) .setPersistenceMode(PersistenceMode.MEMORY)); saveLinesMetric(); }
public void saveDesign(Project sonarProject) { Collection<Resource> directories = resourceMapping.directories(); TimeProfiler profiler = new TimeProfiler(LOG).start("Package design analysis"); LOG.debug("{} packages to analyze", directories.size()); IncrementalCyclesAndFESSolver<Resource> cyclesAndFESSolver = new IncrementalCyclesAndFESSolver<>(graph, directories); LOG.debug("{} cycles", cyclesAndFESSolver.getCycles().size()); Set<Edge> feedbackEdges = cyclesAndFESSolver.getFeedbackEdgeSet(); LOG.debug("{} feedback edges", feedbackEdges.size()); int tangles = cyclesAndFESSolver.getWeightOfFeedbackEdgeSet(); saveIssues(feedbackEdges); saveDependencies(); savePositiveMeasure( sonarProject, CoreMetrics.PACKAGE_CYCLES, cyclesAndFESSolver.getCycles().size()); savePositiveMeasure(sonarProject, CoreMetrics.PACKAGE_FEEDBACK_EDGES, feedbackEdges.size()); savePositiveMeasure(sonarProject, CoreMetrics.PACKAGE_TANGLES, tangles); savePositiveMeasure( sonarProject, CoreMetrics.PACKAGE_EDGES_WEIGHT, getEdgesWeight(directories)); String dsmJson = serializeDsm(graph, directories, feedbackEdges); Measure dsmMeasure = new Measure(CoreMetrics.DEPENDENCY_MATRIX, dsmJson) .setPersistenceMode(PersistenceMode.DATABASE); context.saveMeasure(sonarProject, dsmMeasure); profiler.stop(); for (Resource sonarPackage : directories) { onPackage(sonarPackage); } }
public void analyse(Project project, SensorContext context) { this.project = project; this.context = context; Collection<SquidAstVisitor<LexerlessGrammar>> squidChecks = annotationCheckFactory.getChecks(); List<SquidAstVisitor<LexerlessGrammar>> visitors = Lists.newArrayList(squidChecks); visitors.add(new FileLinesVisitor(project, fileLinesContextFactory)); this.scanner = FlexAstScanner.create( createConfiguration(project), visitors.toArray(new SquidAstVisitor[visitors.size()])); Collection<java.io.File> files = InputFileUtils.toFiles(project.getFileSystem().mainFiles(Flex.KEY)); files = ImmutableList.copyOf(Collections2.filter(files, Predicates.not(MXML_FILTER))); scanner.scanFiles(files); Collection<SourceCode> squidSourceFiles = scanner.getIndex().search(new QueryByType(SourceFile.class)); save(squidSourceFiles); Collection<SourceCode> squidPackages = scanner.getIndex().search(new QueryByType(FlexSquidPackage.class)); for (SourceCode pkg : squidPackages) { String packageName = pkg.getKey(); if (!"".equals(packageName)) { Directory directory = resourceBridge.findDirectory(packageName); context.saveMeasure(directory, CoreMetrics.PACKAGES, 1.0); } } }
private void saveFilesComplexityDistribution(File sonarFile, SourceFile squidFile) { RangeDistributionBuilder complexityDistribution = new RangeDistributionBuilder( CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION, FILES_DISTRIB_BOTTOM_LIMITS); complexityDistribution.add(squidFile.getDouble(FlexMetric.COMPLEXITY)); context.saveMeasure( sonarFile, complexityDistribution.build().setPersistenceMode(PersistenceMode.MEMORY)); }
private void saveFunctionsComplexityDistribution(File sonarFile, SourceFile squidFile) { Collection<SourceCode> squidFunctionsInFile = scanner .getIndex() .search(new QueryByParent(squidFile), new QueryByType(SourceFunction.class)); RangeDistributionBuilder complexityDistribution = new RangeDistributionBuilder( CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, FUNCTIONS_DISTRIB_BOTTOM_LIMITS); for (SourceCode squidFunction : squidFunctionsInFile) { complexityDistribution.add(squidFunction.getDouble(FlexMetric.COMPLEXITY)); } context.saveMeasure( sonarFile, complexityDistribution.build().setPersistenceMode(PersistenceMode.MEMORY)); }
private void saveCoverageData(CoverageFileData data) { if (data == null) { return; } Measure<Double> overallCoverage = new Measure<Double>(CoreMetrics.COVERAGE, data.getCoverage()); Measure<Double> lineCoverage = new Measure<Double>(CoreMetrics.LINE_COVERAGE, data.getCoverage()); Measure<Double> linesToCover = new Measure<Double>(CoreMetrics.LINES_TO_COVER, data.getTotalLines()); Measure<Double> uncoveredLines = new Measure<Double>(CoreMetrics.UNCOVERED_LINES, data.getUncoveredLines()); Measure<?> lineHits = data.getLineHitsBuilder().build().setPersistenceMode(PersistenceMode.DATABASE); try { context.saveMeasure(data.getResource(), overallCoverage); context.saveMeasure(data.getResource(), lineCoverage); context.saveMeasure(data.getResource(), linesToCover); context.saveMeasure(data.getResource(), uncoveredLines); context.saveMeasure(data.getResource(), lineHits); DelphiUtils.LOG.debug("Saving coverage to: " + data.getResource().absolutePath()); } catch (Exception e) { DelphiUtils.LOG.error("Error saving coverage measure.", e); } }
public void analyse(Project project, SensorContext context) { List<QProfileWithId> profiles = Lists.newArrayList(); for (String language : fs.languages()) { QProfileWithId qProfile = (QProfileWithId) moduleQProfiles.findByLanguage(language); if (qProfile != null) { dao.updateUsedColumn(qProfile.id(), true); profiles.add(qProfile); } } UsedQProfiles used = UsedQProfiles.fromProfiles(profiles); Measure detailsMeasure = new Measure(CoreMetrics.QUALITY_PROFILES, used.toJSON()); context.saveMeasure(detailsMeasure); // For backward compatibility if (profiles.size() == 1) { QProfileWithId qProfile = profiles.get(0); Measure measure = new Measure(CoreMetrics.PROFILE, qProfile.name()).setValue((double) qProfile.id()); Measure measureVersion = new Measure(CoreMetrics.PROFILE_VERSION, qProfile.version().doubleValue()); context.saveMeasure(measure); context.saveMeasure(measureVersion); } }
private static void collectPackageMeasures(SMInputCursor pack, SensorContext context) throws XMLStreamException { while (pack.getNext() != null) { Map<String, CoverageMeasuresBuilder> builderByFilename = Maps.newHashMap(); collectFileMeasures(pack.descendantElementCursor("class"), builderByFilename); for (Map.Entry<String, CoverageMeasuresBuilder> entry : builderByFilename.entrySet()) { org.sonar.api.resources.File file = new org.sonar.api.resources.File(entry.getKey()); if (fileExists(context, file)) { for (Measure measure : entry.getValue().createMeasures()) { context.saveMeasure(file, measure); } } } } }
private void saveMeasures(File sonarFile, SourceFile squidFile) { context.saveMeasure(sonarFile, CoreMetrics.FILES, squidFile.getDouble(FlexMetric.FILES)); context.saveMeasure(sonarFile, CoreMetrics.LINES, squidFile.getDouble(FlexMetric.LINES)); context.saveMeasure( sonarFile, CoreMetrics.NCLOC, squidFile.getDouble(FlexMetric.LINES_OF_CODE)); context.saveMeasure( sonarFile, CoreMetrics.COMMENT_LINES, squidFile.getDouble(FlexMetric.COMMENT_LINES)); context.saveMeasure(sonarFile, CoreMetrics.CLASSES, squidFile.getDouble(FlexMetric.CLASSES)); context.saveMeasure( sonarFile, CoreMetrics.FUNCTIONS, squidFile.getDouble(FlexMetric.FUNCTIONS)); context.saveMeasure( sonarFile, CoreMetrics.STATEMENTS, squidFile.getDouble(FlexMetric.STATEMENTS)); context.saveMeasure( sonarFile, CoreMetrics.COMPLEXITY, squidFile.getDouble(FlexMetric.COMPLEXITY)); }
private void onPackage(Resource sonarPackage) { Collection<Resource> squidFiles = getResourcesForDirectory(sonarPackage); if (squidFiles != null && !squidFiles.isEmpty()) { IncrementalCyclesAndFESSolver<Resource> cycleDetector = new IncrementalCyclesAndFESSolver<>(graph, squidFiles); Set<Cycle> cycles = cycleDetector.getCycles(); MinimumFeedbackEdgeSetSolver solver = new MinimumFeedbackEdgeSetSolver(cycles); Set<Edge> feedbackEdges = solver.getEdges(); int tangles = solver.getWeightOfFeedbackEdgeSet(); savePositiveMeasure(sonarPackage, CoreMetrics.FILE_CYCLES, cycles.size()); savePositiveMeasure(sonarPackage, CoreMetrics.FILE_FEEDBACK_EDGES, feedbackEdges.size()); savePositiveMeasure(sonarPackage, CoreMetrics.FILE_TANGLES, tangles); savePositiveMeasure(sonarPackage, CoreMetrics.FILE_EDGES_WEIGHT, getEdgesWeight(squidFiles)); String dsmJson = serializeDsm(graph, squidFiles, feedbackEdges); context.saveMeasure(sonarPackage, new Measure(CoreMetrics.DEPENDENCY_MATRIX, dsmJson)); } }
public void analyse(Project project, SensorContext context) { DroolsPlugin.configureSourceDir(project); Language drools = new Drools(project); ProjectFileSystem fileSystem = project.getFileSystem(); Map<String, DroolsPackage> packageMap = new HashMap<String, DroolsPackage>(); VerifierBuilder verifierBuilder = VerifierBuilderFactory.newVerifierBuilder(); for (File file : fileSystem.getSourceFiles(drools)) { Verifier verifier = verifierBuilder.newVerifier(); try { DroolsFile resource = DroolsFile.fromIOFile(file, false); Source source = analyseSourceCode(file); if (source != null) { context.saveMeasure( resource, CoreMetrics.LINES, (double) source.getMeasure(Metric.LINES)); context.saveMeasure( resource, CoreMetrics.NCLOC, (double) source.getMeasure(Metric.LINES_OF_CODE)); context.saveMeasure( resource, CoreMetrics.COMMENT_LINES, (double) source.getMeasure(Metric.COMMENT_LINES)); } context.saveMeasure(resource, CoreMetrics.FILES, 1.0); context.saveMeasure( resource, CoreMetrics.CLASSES, (double) (resource.getPackageDescr().getRules().size() + resource.getPackageDescr().getFunctions().size())); packageMap.put(resource.getParent().getKey(), resource.getParent()); verifier.addResourcesToVerify(new FileSystemResource(file), ResourceType.DRL); verifier.fireAnalysis(); saveViolations(resource, context, verifier.getResult()); } catch (Throwable e) { DroolsPlugin.LOG.error( "error while verifier analyzing '" + file.getAbsolutePath() + "'", e); } finally { verifier.dispose(); } } for (DroolsPackage droolsPackage : packageMap.values()) { context.saveMeasure(droolsPackage, CoreMetrics.PACKAGES, 1.0); } }
public void saveMeasures(final Map<String, List<Measure>> measures) { if (measures == null) { return; } for (Map.Entry<String, List<Measure>> entry : measures.entrySet()) { final org.sonar.api.resources.File objcfile = org.sonar.api.resources.File.fromIOFile( new File(project.getFileSystem().getBasedir(), entry.getKey()), project); if (fileExists(sensorContext, objcfile)) { for (Measure measure : entry.getValue()) { try { LoggerFactory.getLogger(getClass()) .debug("Save measure {} for file {}", measure.getMetric().getName(), objcfile); sensorContext.saveMeasure(objcfile, measure); } catch (Exception e) { LoggerFactory.getLogger(getClass()) .error(" Exception -> {} -> {}", entry.getKey(), measure.getMetric().getName()); } } } } }
private void saveMetricOnFile(Metric metric, double value) { sensorContext.saveMeasure(sonarFile, new Measure(metric, value)); }
private void saveMeasure(SensorContext context, Measure measure) { context.saveMeasure(resource, measure.setPersistenceMode(PersistenceMode.DATABASE)); }
private void savePositiveMeasure(Resource sonarResource, Metric metric, double value) { if (value >= 0.0) { context.saveMeasure(sonarResource, metric, value); } }