1 package de.example.plugins.helloworld;
3 import java.io.IOException;
5 import java.util.HashMap;
9 import org.sonar.api.profiles.ProfileDefinition;
10 import org.sonar.api.profiles.RulesProfile;
11 import org.sonar.api.rules.RuleFinder;
12 import org.sonar.api.utils.AnnotationUtils;
13 import org.sonar.api.utils.ValidationMessages;
14 import org.sonar.java.checks.CheckList;
15 import org.sonar.plugins.java.Java;
16 import org.sonar.plugins.java.JavaRulesDefinition;
18 import com.google.common.base.Charsets;
19 import com.google.common.io.Resources;
20 import com.google.gson.Gson;
22 public class HelloWorldProfile extends ProfileDefinition {
24 private final Gson gson = new Gson();
25 private final RuleFinder ruleFinder;
26 public HelloWorldProfile(RuleFinder ruleFinder) {
27 this.ruleFinder = ruleFinder;
31 public RulesProfile createProfile(ValidationMessages messages) {
32 RulesProfile profile = RulesProfile.create("Sonar way", Java.KEY);
33 URL resource = JavaRulesDefinition.class.getResource("/org/sonar/l10n/java/rules/squid/Sonar_way_profile.json");
34 Profile jsonProfile = gson.fromJson(readResource(resource), Profile.class);
35 Map<String, String> keys = legacyKeys();
36 for (String key : jsonProfile.ruleKeys) {
37 profile.activateRule(ruleFinder.findByKey(CheckList.REPOSITORY_KEY, keys.get(key)), null);
42 private static String readResource(URL resource) {
44 return Resources.toString(resource, Charsets.UTF_8);
45 } catch (IOException e) {
46 throw new IllegalStateException("Failed to read: " + resource, e);
50 private static Map<String, String> legacyKeys() {
51 Map<String, String> result = new HashMap<>();
52 for (Class checkClass : CheckList.getChecks()) {
53 org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(checkClass, org.sonar.check.Rule.class);
54 String key = ruleAnnotation.key();
55 org.sonar.java.RspecKey rspecKeyAnnotation = AnnotationUtils.getAnnotation(checkClass, org.sonar.java.RspecKey.class);
56 String rspecKey = key;
57 if(rspecKeyAnnotation != null) {
58 rspecKey = rspecKeyAnnotation.value();
60 result.put(rspecKey, key);
65 private static class Profile {
67 List<String> ruleKeys;