]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4908 Expose some new methods when dealing with RuleDefinition for the use of...
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 27 Jan 2014 22:26:03 +0000 (23:26 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 27 Jan 2014 22:26:03 +0000 (23:26 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Checks.java
sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RuleDefinitions.java
sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RuleDefinitionsFromAnnotations.java

index 7c8bbc8f14dd7798de7a54f17c8fdf5aa624d810..a52d4d345e83973ed68fedfd2946392348bfb29e 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.api.utils.SonarException;
 import org.sonar.check.RuleProperty;
 
 import javax.annotation.CheckForNull;
+
 import java.lang.reflect.Field;
 import java.util.Arrays;
 import java.util.Collection;
@@ -121,8 +122,7 @@ public class Checks<C> {
       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());
index 42b17cee7319a85f34d1b0d310d918e779997426..6ccc0452ee0be6043aeb053981af98e25fdafe73 100644 (file)
  */
 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;
@@ -29,6 +35,7 @@ import org.sonar.api.rule.Severity;
 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;
@@ -91,6 +98,11 @@ public interface RuleDefinitions extends ServerExtension {
   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.
      */
@@ -140,6 +152,9 @@ public interface RuleDefinitions extends ServerExtension {
 
   interface NewRepository extends NewExtendedRepository {
     NewRepository setName(String s);
+
+    @CheckForNull
+    NewRule rule(String ruleKey);
   }
 
   class NewRepositoryImpl implements NewRepository {
@@ -178,12 +193,23 @@ public interface RuleDefinitions extends ServerExtension {
       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);
@@ -279,7 +305,6 @@ public interface RuleDefinitions extends ServerExtension {
     }
   }
 
-
   class NewRule {
     private final String repoKey, key;
     private String name, htmlDescription, engineKey, severity = Severity.MAJOR;
@@ -293,6 +318,10 @@ public interface RuleDefinitions extends ServerExtension {
       this.key = key;
     }
 
+    public String key() {
+      return this.key;
+    }
+
     public NewRule setName(String s) {
       // TODO remove newlines
       this.name = s;
@@ -334,6 +363,11 @@ public interface RuleDefinitions extends ServerExtension {
       return param;
     }
 
+    @CheckForNull
+    public NewParam param(String paramKey) {
+      return paramsByKey.get(paramKey);
+    }
+
     /**
      * @see RuleTagFormat
      */
@@ -481,7 +515,6 @@ public interface RuleDefinitions extends ServerExtension {
     }
   }
 
-
   class NewParam {
     private final String key;
     private String name, description, defaultValue;
index 0d05edc3842dd7410eb8bd6e6f2f4746b21c3cdc..9533d32f3b2d970cbe93276f7242b37afbb5951a 100644 (file)
@@ -31,6 +31,8 @@ import org.sonar.api.utils.AnnotationUtils;
 import org.sonar.api.utils.FieldUtils2;
 import org.sonar.check.Cardinality;
 
+import javax.annotation.CheckForNull;
+
 import java.lang.reflect.Field;
 import java.util.List;
 
@@ -52,16 +54,18 @@ class RuleDefinitionsFromAnnotations {
     }
   }
 
-  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);
@@ -76,6 +80,8 @@ class RuleDefinitionsFromAnnotations {
     for (Field field : fields) {
       loadParameters(rule, field);
     }
+
+    return rule;
   }
 
   private void loadParameters(RuleDefinitions.NewRule rule, Field field) {