本文整理了Java中org.sonar.check.Rule
类的一些代码示例,展示了Rule
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Rule
类的具体详情如下:
包路径:org.sonar.check.Rule
类名称:Rule
暂无
代码示例来源:origin: SonarSource/sonarqube
@Rule(key = TemplateRuleCheck.RULE_KEY, cardinality = Cardinality.MULTIPLE, name = "Template rule", description = "Sample template rule")
public class TemplateRuleCheck implements Check {
public static final String RULE_KEY = "TemplateRule";
@RuleProperty(key = "line")
private int line;
@Override
public void execute(SensorContext sensorContext, InputFile file, RuleKey ruleKey) {
NewIssue newIssue = sensorContext.newIssue();
newIssue
.forRule(ruleKey)
.at(newIssue.newLocation()
.on(file)
.at(file.selectLine(line)))
.save();
}
}
代码示例来源:origin: SonarSource/sonarqube
private static Rule toRule(String repositoryKey, Class clazz, org.sonar.check.Rule ruleAnnotation) {
String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName());
String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null);
String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null);
Rule rule = Rule.create(repositoryKey, ruleKey, ruleName);
rule.setDescription(description);
rule.setSeverity(RulePriority.fromCheckPriority(ruleAnnotation.priority()));
rule.setCardinality(ruleAnnotation.cardinality());
rule.setStatus(ruleAnnotation.status());
rule.setTags(ruleAnnotation.tags());
List<Field> fields = FieldUtils2.getFields(clazz, true);
for (Field field : fields) {
addRuleProperty(rule, field);
}
return rule;
}
代码示例来源:origin: SonarSource/sonarqube
@org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", priority = Priority.BLOCKER, status = "BETA")
static class RuleWithProperty {
@org.sonar.check.RuleProperty(description = "Ignore ?", defaultValue = "false")
private String property;
}
代码示例来源:origin: org.codehaus.sonar-plugins/sonar-web-plugin
public static Rule createRule(String repositoryKey, Class clazz, org.sonar.check.Rule ruleAnnotation, @Nullable RuleTags ruleTagsAnnotation) {
String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName());
String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null);
String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null);
Rule rule = Rule.create(repositoryKey, ruleKey, ruleName);
rule.setDescription(description);
rule.setSeverity(RulePriority.fromCheckPriority(ruleAnnotation.priority()));
rule.setCardinality(ruleAnnotation.cardinality());
setTags(rule, ruleTagsAnnotation);
Field[] fields = clazz.getDeclaredFields();
if (fields != null) {
for (Field field : fields) {
addRuleProperty(rule, field);
}
}
return rule;
}
代码示例来源:origin: SonarSource/sonarqube
private static String annotatedEngineKey(Object annotatedClassOrObject) {
String key = null;
org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClassOrObject, org.sonar.check.Rule.class);
if (ruleAnnotation != null) {
key = ruleAnnotation.key();
}
Class clazz = annotatedClassOrObject.getClass();
if (annotatedClassOrObject instanceof Class) {
clazz = (Class) annotatedClassOrObject;
}
return StringUtils.defaultIfEmpty(key, clazz.getCanonicalName());
}
代码示例来源:origin: Cognifide/AEM-Rules-for-SonarQube
private RulesDefinition.NewRule createRule(RulesDefinition.NewExtendedRepository repo, Class clazz, Rule ruleAnnotation) {
String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName());
String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null);
String description = StringUtils.defaultIfEmpty(loadDescription(ruleKey), "No description yet.");
RulesDefinition.NewRule rule = repo.createRule(ruleKey);
rule.setName(ruleName).setMarkdownDescription(description);
rule.setSeverity(ruleAnnotation.priority().name());
rule.setStatus(RuleStatus.valueOf(ruleAnnotation.status()));
rule.setTags(ruleAnnotation.tags());
setMetadata(rule, clazz);
List<Field> fields = FieldUtils2.getFields(clazz, true);
for (Field field : fields) {
loadParameters(rule, field);
}
return rule;
}
代码示例来源:origin: org.codehaus.sonar-plugins.css/css-checks
public static String paramsErrorMessage(Class<? extends CssCheck> clazz, String repository, String message) {
return "Check " + repository + ":" + clazz.getAnnotation(Rule.class).key()
+ " (" + clazz.getAnnotation(Rule.class).name() + "): "
+ message;
}
代码示例来源:origin: org.codehaus.sonar.sslr-squid-bridge/sslr-squid-bridge
@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;
}
代码示例来源:origin: SonarSource/sonarqube
@org.sonar.check.Rule(key = "overriding_foo", name = "Overriding Foo", description = "Desc of Overriding Foo")
static class OverridingRule extends RuleWithProperty {
@org.sonar.check.RuleProperty
private String additionalProperty;
}
代码示例来源:origin: SonarSource/sonarqube
public static String getRuleKey(Class annotatedClass) {
String key = null;
org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(annotatedClass, org.sonar.check.Rule.class);
if (ruleAnnotation != null) {
key = ruleAnnotation.key();
}
return StringUtils.defaultIfEmpty(key, annotatedClass.getCanonicalName());
}
}
代码示例来源:origin: org.codehaus.sonar-plugins.json/json-checks
public static String paramsErrorMessage(Class<? extends JSONCheck> clazz, String message) {
return "Check json:" + clazz.getAnnotation(Rule.class).key()
+ " (" + clazz.getAnnotation(Rule.class).name() + "): "
+ message;
}
代码示例来源:origin: DarLiner/vjtools
@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);
}
}
代码示例来源:origin: SonarSource/sonarqube
@Rule(priority = Priority.CRITICAL)
public class CheckWithStringProperty {
@RuleProperty
private String pattern;
public String getPattern() {
return pattern;
}
}
代码示例来源:origin: SonarSource/sonarqube
@org.sonar.check.Rule(key = "foo", name = "bar", description = "Foo Bar", priority = Priority.BLOCKER)
static class RuleWithInvalidPropertyType {
@org.sonar.check.RuleProperty(description = "text", defaultValue = "Long text", type = "INVALID")
public String property;
}
代码示例来源:origin: SonarSource/sonarqube
private static RulesDefinition.NewRule loadRule(RulesDefinition.NewExtendedRepository repo, Class clazz, org.sonar.check.Rule ruleAnnotation) {
String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName());
String ruleName = StringUtils.defaultIfEmpty(ruleAnnotation.name(), null);
String description = StringUtils.defaultIfEmpty(ruleAnnotation.description(), null);
RulesDefinition.NewRule rule = repo.createRule(ruleKey);
rule.setName(ruleName).setHtmlDescription(description);
rule.setSeverity(ruleAnnotation.priority().name());
rule.setTemplate(ruleAnnotation.cardinality() == Cardinality.MULTIPLE);
rule.setStatus(RuleStatus.valueOf(ruleAnnotation.status()));
rule.setTags(ruleAnnotation.tags());
List<Field> fields = FieldUtils2.getFields(clazz, true);
for (Field field : fields) {
loadParameters(rule, field);
}
return rule;
}
代码示例来源:origin: SonarSource/sonar-java
private static Map<Class<? extends JavaCheck>, String> rulesKeysByRulesClass(Set<Class<? extends JavaCheck>> rules) {
Map<Class<? extends JavaCheck>, String> results = Maps.newHashMap();
for (Class<? extends JavaCheck> ruleClass : rules) {
Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, Rule.class);
if (ruleAnnotation != null) {
results.put(ruleClass, ruleAnnotation.key());
}
}
return results;
}
代码示例来源:origin: racodond/sonar-css-plugin
public static String paramsErrorMessage(Class<? extends CssCheck> clazz, String repository, String message) {
return "Check " + repository + ":" + clazz.getAnnotation(Rule.class).key()
+ " (" + clazz.getAnnotation(Rule.class).name() + "): "
+ message;
}
代码示例来源:origin: SonarSource/sonar-custom-rules-examples
@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);
}
}
代码示例来源:origin: SonarSource/sonarqube
@Rule(priority = Priority.CRITICAL)
public class CheckWithOverriddenPropertyKey{
@RuleProperty(key = "maximum")
private int max = 50;
public int getMax() {
return max;
}
}
代码示例来源:origin: SonarSource/sonarqube
@org.sonar.check.Rule(key = "overriding_foo", name = "Overriding Foo")
static class OverridingRule extends RuleWithProperty {
@org.sonar.check.RuleProperty
private String additionalProperty;
}
内容来源于网络,如有侵权,请联系作者删除!