From 356d640ff7e658195081010a0e7525fd33050b1f Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Thu, 4 Aug 2016 20:58:02 +0200 Subject: [PATCH] Sonarqube simple rule: missing required CheckRegistrar --- .../de/example/helloworld/checks/CheckList.java | 29 +++++++++++++++++----- .../example/helloworld/checks/HelloWorldCheck.java | 6 +++++ .../plugins/helloworld/HelloWorldPlugin.java | 5 ++-- .../helloworld/HelloWorldRulesCheckRegistrar.java | 14 +++++++++++ .../plugins/helloworld/HelloWorldSensor.java | 21 ++++++++++++++++ 5 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesCheckRegistrar.java create mode 100644 Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldSensor.java diff --git a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/helloworld/checks/CheckList.java b/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/helloworld/checks/CheckList.java index 673e836..226f174 100644 --- a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/helloworld/checks/CheckList.java +++ b/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/helloworld/checks/CheckList.java @@ -14,14 +14,31 @@ public final class CheckList { } public static List getChecks() { - return ImmutableList.builder().addAll(getJavaChecks()) + return ImmutableList.builder() + .addAll(getJavaChecks()) + .addAll(getJavaTestChecks()) + .addAll(getXmlChecks()) .build(); } - public static List> getJavaChecks() { - return ImmutableList.>builder() - .add(HelloWorldCheck.class) - .build(); - } + public static List> getJavaChecks() { + return ImmutableList.>builder() + .add(HelloWorldCheck.class) + .build(); + } + + public static List> getJavaTestChecks() { + return ImmutableList.>builder() + .build(); + } + public static List> getXmlChecks() { + return ImmutableList.>builder() + .build(); + } + + private static List> getMavenChecks() { + return ImmutableList.>builder() + .build(); + } } diff --git a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/helloworld/checks/HelloWorldCheck.java b/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/helloworld/checks/HelloWorldCheck.java index 554b672..3d4f2e2 100644 --- a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/helloworld/checks/HelloWorldCheck.java +++ b/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/helloworld/checks/HelloWorldCheck.java @@ -2,6 +2,8 @@ package de.example.helloworld.checks; 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.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.semantic.Symbol.MethodSymbol; @@ -14,6 +16,8 @@ import com.google.common.collect.ImmutableList; @Rule(key = "HELLOWORLD0001") public class HelloWorldCheck extends IssuableSubscriptionVisitor { + private static final Logger LOG = Loggers.get(HelloWorldCheck.class); + @Override public List nodesToVisit() { @@ -22,6 +26,8 @@ public class HelloWorldCheck extends IssuableSubscriptionVisitor { @Override public void visitNode(Tree tree) { + LOG.info("Visiting Node"); + MethodTree method = (MethodTree) tree; if (method.parameters().size() == 1) { diff --git a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldPlugin.java b/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldPlugin.java index b8edfcd..1c431ac 100644 --- a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldPlugin.java +++ b/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldPlugin.java @@ -12,8 +12,9 @@ public class HelloWorldPlugin implements Plugin { ImmutableList.Builder builder = ImmutableList.builder(); builder.add( - HelloWorldRulesDefinition.class); + HelloWorldRulesDefinition.class, + HelloWorldRulesCheckRegistrar.class); + context.addExtensions(builder.build()); - } } diff --git a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesCheckRegistrar.java b/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesCheckRegistrar.java new file mode 100644 index 0000000..c8e3700 --- /dev/null +++ b/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesCheckRegistrar.java @@ -0,0 +1,14 @@ +package de.example.plugins.helloworld; + +import org.sonar.plugins.java.api.CheckRegistrar; + +import de.example.helloworld.checks.CheckList; + +public class HelloWorldRulesCheckRegistrar implements CheckRegistrar { + + @Override + public void register(RegistrarContext registrarContext) { + registrarContext.registerClassesForRepository(CheckList.REPOSITORY_KEY, CheckList.getJavaChecks(), CheckList.getJavaTestChecks()); + } + +} diff --git a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldSensor.java b/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldSensor.java new file mode 100644 index 0000000..4019e1b --- /dev/null +++ b/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldSensor.java @@ -0,0 +1,21 @@ +package de.example.plugins.helloworld; + +import org.sonar.api.batch.sensor.Sensor; +import org.sonar.api.batch.sensor.SensorContext; +import org.sonar.api.batch.sensor.SensorDescriptor; +import org.sonar.plugins.java.Java; + +public class HelloWorldSensor implements Sensor { + + @Override + public void describe(SensorDescriptor descriptor) { + descriptor.onlyOnLanguage(Java.KEY); + descriptor.name("HelloWorld Sensor"); + } + + @Override + public void execute(SensorContext context) { + + } + +} -- 2.1.4