From 78e8750f6805649e8777e8c26534b09c83529e11 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 21 Aug 2016 18:33:13 +0200 Subject: [PATCH] sonar Java plugin: multiple improvements --- Sonar/Plugins/sonar-custom-java-plugin/pom.xml | 57 ++++++++++++---------- .../checks/SpringServiceInstanceFieldCheck.java | 32 ++++++++---- .../plugins/custom/java/CustomRulesDefinition.java | 9 +--- 3 files changed, 57 insertions(+), 41 deletions(-) diff --git a/Sonar/Plugins/sonar-custom-java-plugin/pom.xml b/Sonar/Plugins/sonar-custom-java-plugin/pom.xml index 2514114..7f222a8 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/pom.xml +++ b/Sonar/Plugins/sonar-custom-java-plugin/pom.xml @@ -23,41 +23,48 @@ - - org.sonarsource.sonarqube - sonar-plugin-api - ${sonar.apiVersion} - provided - - - org.sonarsource.java - sonar-java-plugin - sonar-plugin - ${java.plugin.version} - provided - + + org.sonarsource.sonarqube + sonar-plugin-api + ${sonar.apiVersion} + provided + + + org.sonarsource.java + sonar-java-plugin + sonar-plugin + ${java.plugin.version} + provided + + + + com.google.guava + guava + + + + + org.sonarsource.sslr-squid-bridge + sslr-squid-bridge + 2.6.1 + + + + com.google.guava + guava + + + com.google.code.gson gson 2.6.2 - compile com.google.guava guava 19.0 - - commons-lang - commons-lang - 2.6 - - - - org.sonarsource.java - java-frontend - ${java.plugin.version} - diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/SpringServiceInstanceFieldCheck.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/SpringServiceInstanceFieldCheck.java index eb3de5f..a5f23e6 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/SpringServiceInstanceFieldCheck.java +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/SpringServiceInstanceFieldCheck.java @@ -6,6 +6,7 @@ import java.util.List; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.check.Rule; +import org.sonar.check.RuleProperty; import org.sonar.java.model.ModifiersUtils; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.JavaFileScannerContext; @@ -16,15 +17,24 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonar.plugins.java.api.tree.Tree.Kind; import org.sonar.plugins.java.api.tree.VariableTree; +import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; @Rule(key = "GUJ0002") public class SpringServiceInstanceFieldCheck extends IssuableSubscriptionVisitor { private static final Logger LOG = Loggers.get(SpringServiceInstanceFieldCheck.class); + private static final Splitter ANNOTATIONS_SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings(); + private static final String DEFAULT = "javax.inject.Named, org.springframework.stereotype.Service"; private final List issuableVariables = new ArrayList<>(); private boolean isSpringService = false; + + @RuleProperty( + key = "annotations", + description = "Annotations to be checked. Multiple values comma separated.", + defaultValue = "" + DEFAULT) + public String annotations = DEFAULT; @Override public void scanFile(JavaFileScannerContext context) { @@ -58,25 +68,29 @@ public class SpringServiceInstanceFieldCheck extends IssuableSubscriptionVisitor } - private static boolean isOwnedByASpringService(VariableTree variable) { - if (variable.symbol().owner().metadata().isAnnotatedWith("javax.inject.Named") || - variable.symbol().owner().metadata().isAnnotatedWith("org.springframework.stereotype.Service")) { - return true; + private boolean isOwnedByASpringService(VariableTree variable) { + List annotationsToBeChecked = ANNOTATIONS_SPLITTER.splitToList(annotations); + for (String annotation : annotationsToBeChecked) { + if (variable.symbol().owner().metadata().isAnnotatedWith(annotation)) { + return true; + } } return false; } - private static boolean isSpringService(ClassTree tree) { - if (tree.symbol().metadata().isAnnotatedWith("javax.inject.Named") || - tree.symbol().metadata().isAnnotatedWith("org.springframework.stereotype.Service")) { - return true; + private boolean isSpringService(ClassTree tree) { + List annotationsToBeChecked = ANNOTATIONS_SPLITTER.splitToList(annotations); + for (String annotation : annotationsToBeChecked) { + if (tree.symbol().metadata().isAnnotatedWith(annotation)) { + return true; + } } return false; } - private static boolean isStaticOrFinal(VariableTree variable) { + private boolean isStaticOrFinal(VariableTree variable) { ModifiersTree modifiers = variable.modifiers(); return ModifiersUtils.hasModifier(modifiers, Modifier.STATIC) || ModifiersUtils.hasModifier(modifiers, Modifier.FINAL); 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 ac2faf0..bcb4f95 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 @@ -6,7 +6,6 @@ import java.util.List; import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; import org.sonar.api.rule.RuleStatus; import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.api.server.rule.RulesDefinition; @@ -17,6 +16,7 @@ import org.sonar.squidbridge.annotations.RuleTemplate; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Charsets; +import com.google.common.base.Strings; import com.google.common.collect.Iterables; import com.google.common.io.Resources; import com.google.gson.Gson; @@ -52,7 +52,7 @@ public class CustomRulesDefinition implements RulesDefinition { throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); } String ruleKey = ruleAnnotation.key(); - if (StringUtils.isEmpty(ruleKey)) { + if (Strings.isNullOrEmpty(ruleKey)) { throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); } NewRule rule = repository.rule(ruleKey); @@ -67,11 +67,6 @@ public class CustomRulesDefinition implements RulesDefinition { private void ruleMetadata(Class ruleClass, NewRule rule) { String metadataKey = rule.key(); - org.sonar.java.RspecKey rspecKeyAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.java.RspecKey.class); - if (rspecKeyAnnotation != null) { - metadataKey = rspecKeyAnnotation.value(); - rule.setInternalKey(metadataKey); - } addHtmlDescription(rule, metadataKey); addMetadata(rule, metadataKey); -- 2.1.4