import org.sonar.check.RuleProperty;
import javax.annotation.CheckForNull;
+
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
Field field = getField(check, param.getKey());
if (field == null) {
throw new IllegalStateException(
- String.format("The field '%s' does not exist or is not annotated with @RuleProperty in the class %s", param.getKey(), check.getClass().getName())
- );
+ String.format("The field '%s' does not exist or is not annotated with @RuleProperty in the class %s", param.getKey(), check.getClass().getName()));
}
if (StringUtils.isNotBlank(param.getValue())) {
configureField(check, field, param.getValue());
*/
package org.sonar.api.server.rule;
-import com.google.common.collect.*;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSortedSet;
+import com.google.common.collect.ListMultimap;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.sonar.api.ServerExtension;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
+
import java.io.InputStream;
import java.util.List;
import java.util.Map;
interface NewExtendedRepository {
NewRule newRule(String ruleKey);
+ /**
+ * Reads definition of rule from the annotations provided by the library sonar-check-api.
+ */
+ NewRule loadAnnotatedClass(Class clazz);
+
/**
* Reads definitions of rules from the annotations provided by the library sonar-check-api.
*/
interface NewRepository extends NewExtendedRepository {
NewRepository setName(String s);
+
+ @CheckForNull
+ NewRule rule(String ruleKey);
}
class NewRepositoryImpl implements NewRepository {
return newRule;
}
+ @CheckForNull
+ @Override
+ public NewRule rule(String ruleKey) {
+ return newRules.get(ruleKey);
+ }
+
@Override
public NewRepositoryImpl loadAnnotatedClasses(Class... classes) {
new RuleDefinitionsFromAnnotations().loadRules(this, classes);
return this;
}
+ @Override
+ public RuleDefinitions.NewRule loadAnnotatedClass(Class clazz) {
+ return new RuleDefinitionsFromAnnotations().loadRule(this, clazz);
+ }
+
@Override
public NewRepositoryImpl loadXml(InputStream xmlInput, String encoding) {
new RuleDefinitionsFromXml().loadRules(this, xmlInput, encoding);
}
}
-
class NewRule {
private final String repoKey, key;
private String name, htmlDescription, engineKey, severity = Severity.MAJOR;
this.key = key;
}
+ public String key() {
+ return this.key;
+ }
+
public NewRule setName(String s) {
// TODO remove newlines
this.name = s;
return param;
}
+ @CheckForNull
+ public NewParam param(String paramKey) {
+ return paramsByKey.get(paramKey);
+ }
+
/**
* @see RuleTagFormat
*/
}
}
-
class NewParam {
private final String key;
private String name, description, defaultValue;
import org.sonar.api.utils.FieldUtils2;
import org.sonar.check.Cardinality;
+import javax.annotation.CheckForNull;
+
import java.lang.reflect.Field;
import java.util.List;
}
}
- private void loadRule(RuleDefinitions.NewRepository repo, Class clazz) {
+ @CheckForNull
+ RuleDefinitions.NewRule loadRule(RuleDefinitions.NewRepository repo, Class clazz) {
org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(clazz, org.sonar.check.Rule.class);
if (ruleAnnotation != null) {
- loadRule(repo, clazz, ruleAnnotation);
+ return loadRule(repo, clazz, ruleAnnotation);
} else {
LOG.warn("The class " + clazz.getCanonicalName() + " should be annotated with " + org.sonar.check.Rule.class);
+ return null;
}
}
- private void loadRule(RuleDefinitions.NewRepository repo, Class clazz, org.sonar.check.Rule ruleAnnotation) {
+ private RuleDefinitions.NewRule loadRule(RuleDefinitions.NewRepository 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);
for (Field field : fields) {
loadParameters(rule, field);
}
+
+ return rule;
}
private void loadParameters(RuleDefinitions.NewRule rule, Field field) {