sonar custom Java plugin: some improvements
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 19 Aug 2016 19:18:21 +0000 (21:18 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 19 Aug 2016 19:18:21 +0000 (21:18 +0200)
Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/ParameterCheck.java
Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomPlugin.java
Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomProfile.java
Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomRulesDefinition.java
Sonar/Plugins/sonar-custom-java-plugin/src/main/resources/de/example/l10n/java/rules/custom/Custom_profile.json

index 914d345..372bbf4 100644 (file)
@@ -35,7 +35,8 @@ public class ParameterCheck extends IssuableSubscriptionVisitor {
                        Type firstParameterType = symbol.parameterTypes().get(0);
                        Type returnType = symbol.returnType().type();
                        if(returnType.is(firstParameterType.fullyQualifiedName())) {
-                               reportIssue(method.simpleName(), "Never do that!");
+                               reportIssue(method.simpleName(),
+                                               "Remove this method");
                        }
                }
                
index 399cfd7..d87bcc5 100644 (file)
@@ -13,7 +13,9 @@ public class CustomPlugin implements Plugin {
     ImmutableList.Builder<Object> builder = ImmutableList.builder();
     builder.add(
                CustomRulesDefinition.class,
-               CustomRulesCheckRegistrar.class);
+               CustomRulesCheckRegistrar.class,
+               CustomProfile.class,
+               CustomSensor.class);
     
     context.addExtensions(builder.build());
   }
index f054f53..270fe70 100644 (file)
@@ -2,69 +2,56 @@ package de.example.plugins.custom.java;
 
 import java.io.IOException;
 import java.net.URL;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import org.sonar.api.profiles.ProfileDefinition;
 import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.api.utils.AnnotationUtils;
+import org.sonar.api.rules.Rule;
 import org.sonar.api.utils.ValidationMessages;
-import org.sonar.java.checks.CheckList;
 import org.sonar.plugins.java.Java;
-import org.sonar.plugins.java.JavaRulesDefinition;
 
 import com.google.common.base.Charsets;
 import com.google.common.io.Resources;
 import com.google.gson.Gson;
 
-public class CustomProfile extends ProfileDefinition {
-
-         private final Gson gson = new Gson();
-         private final RuleFinder ruleFinder;
-         public CustomProfile(RuleFinder ruleFinder) {
-           this.ruleFinder = ruleFinder;
-         }
-
-         @Override
-         public RulesProfile createProfile(ValidationMessages messages) {
-           RulesProfile profile = RulesProfile.create("Custom Java Profile", Java.KEY);
-           URL resource = JavaRulesDefinition.class.getResource("/org/sonar/l10n/java/rules/custom/Custom_profile.json");
-           Profile jsonProfile = gson.fromJson(readResource(resource), Profile.class);
-           Map<String, String> keys = legacyKeys();
-           for (String key : jsonProfile.ruleKeys) {
-             profile.activateRule(ruleFinder.findByKey(CheckList.REPOSITORY_KEY, keys.get(key)), null);
-           }
-           return profile;
-         }
-
-         private static String readResource(URL resource) {
-           try {
-             return Resources.toString(resource, Charsets.UTF_8);
-           } catch (IOException e) {
-             throw new IllegalStateException("Failed to read: " + resource, e);
-           }
-         }
+import de.example.custom.java.checks.CheckList;
 
-         private static Map<String, String> legacyKeys() {
-           Map<String, String> result = new HashMap<>();
-           for (Class checkClass : CheckList.getChecks()) {
-             org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(checkClass, org.sonar.check.Rule.class);
-             String key = ruleAnnotation.key();
-             org.sonar.java.RspecKey rspecKeyAnnotation = AnnotationUtils.getAnnotation(checkClass, org.sonar.java.RspecKey.class);
-             String rspecKey = key;
-             if(rspecKeyAnnotation != null) {
-               rspecKey = rspecKeyAnnotation.value();
-             }
-             result.put(rspecKey, key);
-           }
-           return result;
-         }
+public class CustomProfile extends ProfileDefinition {
+       private final Gson gson = new Gson();
+
+       @Override
+       public RulesProfile createProfile(ValidationMessages messages) {
+               URL resource = CustomRulesDefinition.class.getResource("/de/example/l10n/java/rules/custom/Custom_profile.json");
+               Profile jsonProfile = gson.fromJson(readResource(resource), Profile.class);
+               RulesProfile profile = RulesProfile.create(jsonProfile.getName(), Java.KEY);
+               
+               for (String key : jsonProfile.ruleKeys) {
+                       Rule rule = Rule.create(CheckList.REPOSITORY_KEY, key);
+                       profile.activateRule(rule, null);
+               }
+               
+               return profile;
+       }
 
-         private static class Profile {
-           String name;
-           List<String> ruleKeys;
-         }
+       private String readResource(URL resource) {
+               try {
+                       return Resources.toString(resource, Charsets.UTF_8);
+               } catch (IOException e) {
+                       throw new IllegalStateException("Failed to read: " + resource, e);
+               }
+       }
 
+       private static class Profile {
+               String name;
+               List<String> ruleKeys;
+               
+               public String getName() {
+                       return name;
+               }
+               
+               public List<String> getRuleKeys() {
+                       return ruleKeys;
+               }
        }
+
+}
index dd1fbd3..ac2faf0 100644 (file)
@@ -92,14 +92,14 @@ public class CustomRulesDefinition implements RulesDefinition {
     }
   }
 
-  private static void addHtmlDescription(NewRule rule, String metadataKey) {
+  private void addHtmlDescription(NewRule rule, String metadataKey) {
     URL resource = CustomRulesDefinition.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.html");
     if (resource != null) {
       rule.setHtmlDescription(readResource(resource));
     }
   }
 
-  private static String readResource(URL resource) {
+  private String readResource(URL resource) {
     try {
       return Resources.toString(resource, Charsets.UTF_8);
     } catch (IOException e) {