@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(); }
public static AnnotationCheckFactory create(RulesProfile profile, String repositoryKey, Collection checkClasses) { AnnotationCheckFactory factory = new AnnotationCheckFactory(profile, repositoryKey, checkClasses); factory.init(); return factory; }
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); } }
/** * 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 ObjectiveCSquidSensor(RulesProfile profile) { this.annotationCheckFactory = AnnotationCheckFactory.create(profile, CheckList.REPOSITORY_KEY, CheckList.getChecks()); }
@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()); } } }
@Override @SuppressWarnings("unchecked") public void analyse(Project project, SensorContext context) { this.project = project; this.context = context; Collection<SquidAstVisitor<CSharpGrammar>> squidChecks = annotationCheckFactory.getChecks(); List<SquidAstVisitor<CSharpGrammar>> visitors = Lists.newArrayList(squidChecks); // TODO: remove the following line & class once SSLR Squid bridge computes NCLOC_DATA_KEY & COMMENT_LINES_DATA_KEY visitors.add(new CSharpFileLinesVisitor(project, fileLinesContextFactory)); scanner = CSharpAstScanner.create(createParserConfiguration(project), (SquidAstVisitor<CSharpGrammar>[]) visitors.toArray(new SquidAstVisitor[visitors.size()])); scanner.scanFiles(getFilesToAnalyse(project)); Collection<SourceCode> squidSourceFiles = scanner.getIndex().search(new QueryByType(SourceFile.class)); saveMeasures(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()); } } }
/** * Analyze the XML files. */ public void analyse(Project project, SensorContext sensorContext) { Collection<AbstractXmlCheck> checks = annotationCheckFactory.getChecks(); for (java.io.File file : fileSystem.files(FileQuery.onSource().onLanguage(Xml.KEY))) { try { File resource = File.fromIOFile(file, project); XmlSourceCode sourceCode = new XmlSourceCode(resource, file); // Do not execute any XML rule when an XML file is corrupted (SONARXML-13) if (sourceCode.parseSource(fileSystem)) { for (AbstractXmlCheck check : checks) { check.setRule(annotationCheckFactory.getActiveRule(check).getRule()); check.validate(sourceCode); } saveIssue(sourceCode); } } catch (Exception e) { LOG.error("Could not analyze the file " + file.getAbsolutePath(), e); } } }
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; }
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); } } }
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); }
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(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); }