From: Gustavo Martin Morcuende Date: Fri, 19 Aug 2016 19:18:21 +0000 (+0200) Subject: sonar custom Java plugin: some improvements X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=6b001008ffc83ca9b08ff1082bdb85a0b7131f4c;p=JavaForFun sonar custom Java plugin: some improvements --- diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/ParameterCheck.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/ParameterCheck.java index 914d345..372bbf4 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/ParameterCheck.java +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/ParameterCheck.java @@ -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"); } } diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomPlugin.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomPlugin.java index 399cfd7..d87bcc5 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomPlugin.java +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomPlugin.java @@ -13,7 +13,9 @@ public class CustomPlugin implements Plugin { ImmutableList.Builder builder = ImmutableList.builder(); builder.add( CustomRulesDefinition.class, - CustomRulesCheckRegistrar.class); + CustomRulesCheckRegistrar.class, + CustomProfile.class, + CustomSensor.class); context.addExtensions(builder.build()); } diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomProfile.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomProfile.java index f054f53..270fe70 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomProfile.java +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomProfile.java @@ -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 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 legacyKeys() { - Map 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 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 ruleKeys; + + public String getName() { + return name; + } + + public List getRuleKeys() { + return ruleKeys; + } } + +} diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomRulesDefinition.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomRulesDefinition.java index dd1fbd3..ac2faf0 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomRulesDefinition.java +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomRulesDefinition.java @@ -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) { diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/resources/de/example/l10n/java/rules/custom/Custom_profile.json b/Sonar/Plugins/sonar-custom-java-plugin/src/main/resources/de/example/l10n/java/rules/custom/Custom_profile.json index e4debbe..8dcf3fb 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/main/resources/de/example/l10n/java/rules/custom/Custom_profile.json +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/resources/de/example/l10n/java/rules/custom/Custom_profile.json @@ -1,5 +1,5 @@ { - "name": "Custom profile", + "name": "Custom Java Profile", "ruleKeys": [ "GUJ0001", "GUJ0002"