@VisibleForTesting NewRule newRule(Class<?> ruleClass, boolean failIfNoExplicitKey) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); if (Strings.isNullOrEmpty(ruleKey)) { if (failIfNoExplicitKey) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } ruleKey = ruleClass.getCanonicalName(); } NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository); } return rule; }
@VisibleForTesting NewRule newRule(Class<?> ruleClass, boolean failIfNoExplicitKey) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); if (Strings.isNullOrEmpty(ruleKey)) { if (failIfNoExplicitKey) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } ruleKey = ruleClass.getCanonicalName(); } NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository); } return rule; }
private NewRule newRule(Class<?> ruleClass, boolean failIfNoExplicitKey) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); if (ruleKey.isEmpty()) { if (failIfNoExplicitKey) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } ruleKey = ruleClass.getCanonicalName(); } return repository.rule(ruleKey); }
NewRule rule = repository.rule(ruleKey); if (rule != null) { rule.setDebtSubCharacteristic(subCharName);
/** * Loads HTML descriptions and meta data from JSON file to checks. * * @param ruleClass Class type of given rule. * @param repository current repository. */ @VisibleForTesting protected void newRule(Class<?> ruleClass, NewRepository repository) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException( String.format("No Rule annotation was found on %s", ruleClass)); } String ruleKey = ruleAnnotation.key(); if (StringUtils.isEmpty(ruleKey)) { throw new IllegalArgumentException( String.format("No key is defined in Rule annotation of %s", ruleClass)); } NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException( String.format("No rule was created for %s in %s", ruleClass, repository.key())); } addHtmlDescription(rule, rule.key()); addMetadata(rule, rule.key()); }
@VisibleForTesting protected void newRule(Class<?> ruleClass, NewRepository repository, JavaSonarWayProfile.Profile profile) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); if (StringUtils.isEmpty(ruleKey)) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key()); } String rspecKey = rspecKey(ruleClass, rule); RuleMetadata ruleMetadata = readRuleMetadata(rspecKey); addMetadata(rule, ruleMetadata); String ruleHtmlDescription = readRuleHtmlDescription(rspecKey); if (ruleHtmlDescription != null) { rule.setHtmlDescription(ruleHtmlDescription); } // 'setActivatedByDefault' is used by SonarLint standalone, to define which rules will be active boolean activatedInProfile = profile.ruleKeys.contains(ruleKey) || profile.ruleKeys.contains(rspecKey); boolean isSecurityHotspot = ruleMetadata != null && ruleMetadata.isSecurityHotspot(); rule.setActivatedByDefault(activatedInProfile && !isSecurityHotspot); rule.setTemplate(TEMPLATE_RULE_KEY.contains(ruleKey)); }
@VisibleForTesting NewRule newRule(Class<?> ruleClass, boolean failIfNoExplicitKey) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); if (StringUtils.isEmpty(ruleKey)) { if (failIfNoExplicitKey) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } ruleKey = ruleClass.getCanonicalName(); } NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository); } if (ruleAnnotation.cardinality() == Cardinality.MULTIPLE) { throw new IllegalArgumentException("Cardinality is not supported, use the RuleTemplate annotation instead"); } return rule; }
@VisibleForTesting protected void newRule(Class<?> ruleClass, NewRepository repository) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); if (StringUtils.isEmpty(ruleKey)) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key()); } ruleMetadata(ruleClass, rule); rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null); if (ruleAnnotation.cardinality() == Cardinality.MULTIPLE) { throw new IllegalArgumentException("Cardinality is not supported, use the RuleTemplate annotation instead for " + ruleClass); } }
@VisibleForTesting protected void newRule(Class<?> ruleClass, NewRepository repository) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); if (StringUtils.isEmpty(ruleKey)) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key()); } ruleMetadata(rule); rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null); if (ruleAnnotation.cardinality() == Cardinality.MULTIPLE) { throw new IllegalArgumentException("Cardinality is not supported, use the RuleTemplate annotation instead for " + ruleClass); } }
private static NewRule addAnnotatedRule(NewRepository repository, Class<?> ruleClass) { org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); if (ruleAnnotation == null) { throw new IllegalStateException("No Rule annotation was found on " + ruleClass.getName()); } String ruleKey = ruleAnnotation.key(); if (ruleKey.length() == 0) { throw new IllegalStateException("Empty key"); } new RulesDefinitionAnnotationLoader().load(repository, ruleClass); NewRule rule = repository.rule(ruleKey); if (rule == null) { throw new IllegalStateException("Rule not found: " + ruleKey); } return rule; }
@Override public void define(Context context) { RulesDefinition.NewRepository repository = context.createRepository(repositoryKey(), "js") .setName("MyCompany Custom Repository"); // this will load metadata from @Rule annotation new RulesDefinitionAnnotationLoader().load(repository, checkClasses().toArray(new Class[] {})); NewRule rule = repository.rule("S1"); // description is loaded from html file in resources directory rule.setHtmlDescription(loadRuleDescription(rule.key())); // remediation function sets how much rule violation contributes to technical debt rule.setDebtRemediationFunction(rule.debtRemediationFunctions().linear("5min")); repository.done(); }
@Override public void define(Context context) { NewRepository repository = context .createRepository(CheckList.REPOSITORY_KEY, JavaScriptLanguage.KEY) .setName(CheckList.REPOSITORY_NAME); RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(METADATA_LOCATION, SonarWayProfile.PATH_TO_JSON); ruleMetadataLoader.addRulesByAnnotatedClass(repository, CheckList.getChecks()); NewRule commentRegularExpression = repository.rule("CommentRegularExpression"); commentRegularExpression.setTemplate(true); repository.done(); }
@Override public void define(Context context) { NewRepository repo = context.createRepository(keyForLanguage(language), language) .setName("Common SonarQube"); AnnotationBasedRulesDefinition.load(repo, language, enabledChecks); setParamValue(repo.rule(RULE_INSUFFICIENT_BRANCH_COVERAGE), PARAM_MIN_BRANCH_COVERAGE, minimumBranchCoverageRatio); setParamValue(repo.rule(RULE_INSUFFICIENT_LINE_COVERAGE), PARAM_MIN_LINE_COVERAGE, minimumLineCoverageRatio); setParamValue(repo.rule(RULE_INSUFFICIENT_COMMENT_DENSITY), PARAM_MIN_COMMENT_DENSITY, minimumCommentDensity); repo.done(); }
@Override public void define(Context context) { NewRepository repository = context .createRepository(CheckList.REPOSITORY_KEY, JavaScriptLanguage.KEY) .setName(CheckList.REPOSITORY_NAME); RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(METADATA_LOCATION, SonarWayProfile.PATH_TO_JSON); ruleMetadataLoader.addRulesByAnnotatedClass(repository, CheckList.getChecks()); NewRule commentRegularExpression = repository.rule("CommentRegularExpression"); commentRegularExpression.setTemplate(true); repository.done(); }
@Override public void define(Context context) { NewRepository repository = context.createRepository(Xml.REPOSITORY_KEY, Xml.KEY).setName(Xml.REPOSITORY_NAME); RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(Xml.XML_RESOURCE_PATH, Xml.SONAR_WAY_PATH, sonarRuntime); // add the new checks ruleMetadataLoader.addRulesByAnnotatedClass(repository, CheckList.getCheckClasses()); repository.rule("XPathCheck").setTemplate(true); repository.done(); } }
@Override public void define(Context context) { NewRepository repository = context .createRepository(CheckList.REPOSITORY_KEY, Flex.KEY) .setName(REPOSITORY_NAME); RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH, FlexProfile.SONAR_WAY_PROFILE_PATH); ruleMetadataLoader.addRulesByAnnotatedClass(repository, CheckList.getChecks()); TEMPLATE_RULE_KEYS.forEach(key -> repository.rule(key).setTemplate(true)); repository.done(); } }