public static AnnotationCheckFactory create(RulesProfile profile, String repositoryKey, Collection checkClasses) { AnnotationCheckFactory factory = new AnnotationCheckFactory(profile, repositoryKey, checkClasses); factory.init(); return factory; }
private AnnotationCheckFactory(RulesProfile profile, String repositoryKey, Collection checks) { super(profile, repositoryKey); groupByKey(checks); }
private void groupByKey(Collection checks) { for (Object check : checks) { String key = getRuleKey(check); if (key != null) { checksByKey.put(key, check); } } }
@Override public boolean shouldExecuteOnProject(Project project) { boolean hasLangFiles = !fs.mainFiles(language).isEmpty() || !fs.testFiles(language).isEmpty(); if (hasLangFiles) { checkFactory = AnnotationCheckFactory.create(qProfile, DefaultCommonRulesRepository.keyForLanguage(language), CommonRulesConstants.CLASSES); activeChecks = checkFactory.getChecks(); } return !activeChecks.isEmpty(); }
@Override public void endDocument() { if (noSonarLines != null && !noSonarLines.isEmpty()) { noSonarFilter.addResource(getWebSourceCode().getResource(), noSonarLines); } }
/** * Create PageScanner with Visitors. */ private HtmlAstScanner setupScanner() { HtmlAstScanner scanner = new HtmlAstScanner(ImmutableList.of(new PageCountLines(), new ComplexityVisitor(), new NoSonarScanner(noSonarFilter))); for (AbstractPageCheck check : (Collection<AbstractPageCheck>) annotationCheckFactory.getChecks()) { scanner.addVisitor(check); check.setRule(annotationCheckFactory.getActiveRule(check).getRule()); } return scanner; }
public WebSensor(Web web, RulesProfile profile, NoSonarFilter noSonarFilter, ModuleFileSystem fileSystem, FileLinesContextFactory fileLinesContextFactory) { this.web = web; this.noSonarFilter = noSonarFilter; this.annotationCheckFactory = AnnotationCheckFactory.create(profile, WebRulesRepository.REPOSITORY_KEY, CheckClasses.getCheckClasses()); this.fileSystem = fileSystem; this.fileLinesContextFactory = fileLinesContextFactory; }
@SuppressWarnings({"rawtypes", "unchecked"}) public void decorate(Resource resource, DecoratorContext context) { // assume that all checks relate to files, not directories nor modules if (ResourceUtils.isEntity(resource) && resource.getLanguage() != null && resource.getLanguage().getKey().equals(language)) { for (CommonCheck check : activeChecks) { check.checkResource(resource, context, checkFactory.getActiveRule(check).getRule()); } } }
public void analyse(Project project, SensorContext context) { this.project = project; this.context = context; Collection<SquidCheck> squidChecks = annotationCheckFactory.getChecks(); this.scanner = ObjectiveCAstScanner.create(createConfiguration(project), squidChecks.toArray(new SquidCheck[squidChecks.size()])); scanner.scanFiles(InputFileUtils.toFiles(project.getFileSystem().mainFiles(ObjectiveC.KEY))); Collection<SourceCode> squidSourceFiles = scanner.getIndex().search(new QueryByType(SourceFile.class)); save(squidSourceFiles); }
private void configureFields(ActiveRule activeRule, Object check) { for (ActiveRuleParam param : activeRule.getActiveRuleParams()) { Field field = getField(check, param.getKey()); if (field == null) { throw new SonarException("The field " + param.getKey() + " does not exist or is not annotated with @RuleProperty in the class " + check.getClass().getName()); } if (StringUtils.isNotBlank(param.getValue())) { configureField(check, field, param.getValue()); } } }
@Override public Object createCheck(ActiveRule activeRule) { Object object = checksByKey.get(activeRule.getConfigKey()); if (object != null) { return instantiate(activeRule, object); } return null; }
protected void init() { checkByActiveRule.clear(); activeRuleByCheck.clear(); for (ActiveRule activeRule : profile.getActiveRulesByRepository(repositoryKey)) { C check = createCheck(activeRule); checkByActiveRule.put(activeRule, check); activeRuleByCheck.put(check, activeRule); } }
private Object instantiate(ActiveRule activeRule, Object checkClassOrInstance) { try { Object check = checkClassOrInstance; if (check instanceof Class) { check = ((Class) checkClassOrInstance).newInstance(); } configureFields(activeRule, check); return check; } catch (InstantiationException e) { throw new SonarException(CAN_NOT_INSTANTIATE_THE_CHECK_RELATED_TO_THE_RULE + activeRule, e); } catch (IllegalAccessException e) { throw new SonarException(CAN_NOT_INSTANTIATE_THE_CHECK_RELATED_TO_THE_RULE + activeRule, e); } }
public void analyse(Project prj, SensorContext context) { List<File> srcFiles = VisualUtils.buildCsFileList(prj); for (File srcFile : srcFiles) { CSharpFile cSharpFile = fileLocator.locate(prj, srcFile, false); if (cSharpFile == null) { continue; } Source source = analyseSourceCode(srcFile); if (source != null) { // TODO HACK for SONARPLUGINS-662 noSonarFilter.addResource(cSharpFile, source.getNoSonarTagLines()); context.saveMeasure(cSharpFile, CoreMetrics.COMMENTED_OUT_CODE_LINES, (double) source.getMeasure(Metric.COMMENTED_OUT_CODE_LINES)); } } }
public XmlSensor(RulesProfile profile, ModuleFileSystem fileSystem, ResourcePerspectives resourcePerspectives) { this.annotationCheckFactory = AnnotationCheckFactory.create(profile, CheckRepository.REPOSITORY_KEY, CheckRepository.getCheckClasses()); this.fileSystem = fileSystem; this.resourcePerspectives = resourcePerspectives; }
private void saveViolations(SourceCode squidFile, File sonarFile) { Set<CheckMessage> messages = squidFile.getCheckMessages(); if (messages != null) { for (CheckMessage message : messages) { @SuppressWarnings("unchecked") Violation violation = Violation.create(annotationCheckFactory.getActiveRule(message.getChecker()), sonarFile); violation.setLineId(message.getLine()); violation.setMessage(message.getText(Locale.ENGLISH)); context.saveViolation(violation); } } }
/** * @see org.sonar.api.batch.Sensor#analyse(org.sonar.api.resources.Project, org.sonar.api.batch.SensorContext) */ public void analyse(Project project, SensorContext context) { ProjectFileSystem fileSystem = project.getFileSystem(); List<File> sourceFiles = fileSystem.getSourceFiles(PHP); List<File> directories = fileSystem.getSourceDirs(); for (File file : sourceFiles) { PhpFile phpFile = PhpFile.getInstance(project).fromIOFile(file, directories, false); if (phpFile != null) { Source source = analyseSourceCode(file); if (source != null) { filter.addResource(phpFile, source.getNoSonarTagLines()); double measure = source.getMeasure(Metric.COMMENTED_OUT_CODE_LINES); context.saveMeasure(phpFile, CoreMetrics.COMMENTED_OUT_CODE_LINES, measure); } } } }
public ObjectiveCSquidSensor(RulesProfile profile) { this.annotationCheckFactory = AnnotationCheckFactory.create(profile, CheckList.REPOSITORY_KEY, CheckList.getChecks()); }
private void saveViolations(File sonarFile, SourceFile squidFile) { Collection<CheckMessage> messages = squidFile.getCheckMessages(); if (messages != null) { for (CheckMessage message : messages) { Violation violation = Violation.create(annotationCheckFactory.getActiveRule(message.getChecker()), sonarFile) .setLineId(message.getLine()) .setMessage(message.getText(Locale.ENGLISH)); context.saveViolation(violation); } } }
public CSharpSquidSensor(CSharp cSharp, CSharpResourcesBridge cSharpResourcesBridge, ResourceCreationLock resourceCreationLock, MicrosoftWindowsEnvironment microsoftWindowsEnvironment, RulesProfile profile, NoSonarFilter noSonarFilter, FileLinesContextFactory fileLinesContextFactory, CSharpCheck[] cSharpChecks) { super(microsoftWindowsEnvironment); this.cSharp = cSharp; this.cSharpResourcesBridge = cSharpResourcesBridge; this.resourceCreationLock = resourceCreationLock; this.noSonarFilter = noSonarFilter; this.fileLinesContextFactory = fileLinesContextFactory; Collection<Class> allChecks = CSharpCheck.toCollection(cSharpChecks); allChecks.addAll(CheckList.getChecks()); this.annotationCheckFactory = AnnotationCheckFactory.create(profile, CSharpSquidConstants.REPOSITORY_KEY, allChecks); }