From bea1b4152f0d2fd10c270a9d3ed474c4968f928d Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Fri, 5 Aug 2016 02:46:06 +0200 Subject: [PATCH] SonarQube, custom Java rules. --- Sonar/Plugins/hello-world-plugin/pom.xml | 173 --------------------- .../de/example/helloworld/checks/CheckList.java | 44 ------ .../example/helloworld/checks/HelloWorldCheck.java | 43 ----- .../plugins/helloworld/HelloWorldPlugin.java | 20 --- .../helloworld/HelloWorldRulesCheckRegistrar.java | 14 -- .../helloworld/HelloWorldRulesDefinition.java | 141 ----------------- .../plugins/helloworld/HelloWorldSensor.java | 21 --- .../rules/helloworld/HELLOWORLD0001_java.html | 18 --- .../rules/helloworld/HELLOWORLD0001_java.json | 12 -- .../src/test/files/HelloWorldCheck.java | 17 -- .../helloworld/checks/HelloWorldCheckTest.java | 12 -- Sonar/Plugins/pom.xml | 27 ++++ .../Plugins/sonar-custom-java-plugin/.pom.xml.swp | Bin 0 -> 20480 bytes Sonar/Plugins/sonar-custom-java-plugin/pom.xml | 172 ++++++++++++++++++++ .../de/example/helloworld/checks/CheckList.java | 45 ++++++ .../example/helloworld/checks/HelloWorldCheck.java | 43 +++++ .../checks/SpringServiceInstanceFieldCheck.java | 51 ++++++ .../plugins/helloworld/HelloWorldPlugin.java | 20 +++ .../plugins/helloworld/HelloWorldProfile.java | 70 +++++++++ .../helloworld/HelloWorldRulesCheckRegistrar.java | 14 ++ .../helloworld/HelloWorldRulesDefinition.java | 141 +++++++++++++++++ .../plugins/helloworld/HelloWorldSensor.java | 21 +++ .../helloworld/rules/helloworld/GU0001_java.html | 18 +++ .../helloworld/rules/helloworld/GU0001_java.json | 12 ++ .../src/test/files/checks/HelloWorldCheck.java | 17 ++ .../checks/SpringServiceInstanceFieldCheck.java | 18 +++ .../helloworld/checks/HelloWorldCheckTest.java | 12 ++ .../SpringServiceInstanceFieldCheckTest.java | 13 ++ 28 files changed, 694 insertions(+), 515 deletions(-) delete mode 100644 Sonar/Plugins/hello-world-plugin/pom.xml delete mode 100644 Sonar/Plugins/hello-world-plugin/src/main/java/de/example/helloworld/checks/CheckList.java delete mode 100644 Sonar/Plugins/hello-world-plugin/src/main/java/de/example/helloworld/checks/HelloWorldCheck.java delete mode 100644 Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldPlugin.java delete mode 100644 Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesCheckRegistrar.java delete mode 100644 Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesDefinition.java delete mode 100644 Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldSensor.java delete mode 100644 Sonar/Plugins/hello-world-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/HELLOWORLD0001_java.html delete mode 100644 Sonar/Plugins/hello-world-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/HELLOWORLD0001_java.json delete mode 100644 Sonar/Plugins/hello-world-plugin/src/test/files/HelloWorldCheck.java delete mode 100644 Sonar/Plugins/hello-world-plugin/src/test/java/de/example/helloworld/checks/HelloWorldCheckTest.java create mode 100644 Sonar/Plugins/pom.xml create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/.pom.xml.swp create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/pom.xml create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/helloworld/checks/CheckList.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/helloworld/checks/HelloWorldCheck.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/helloworld/checks/SpringServiceInstanceFieldCheck.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldPlugin.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldProfile.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesCheckRegistrar.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesDefinition.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldSensor.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/GU0001_java.html create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/GU0001_java.json create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/test/files/checks/HelloWorldCheck.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/test/files/checks/SpringServiceInstanceFieldCheck.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/helloworld/checks/HelloWorldCheckTest.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/helloworld/checks/SpringServiceInstanceFieldCheckTest.java diff --git a/Sonar/Plugins/hello-world-plugin/pom.xml b/Sonar/Plugins/hello-world-plugin/pom.xml deleted file mode 100644 index 0a00508..0000000 --- a/Sonar/Plugins/hello-world-plugin/pom.xml +++ /dev/null @@ -1,173 +0,0 @@ - - - 4.0.0 - - de.example.plugins - helloworld-plugin - sonar-plugin - 0.1-SNAPSHOT - - HelloWorld - HelloWorld example plugin for SonarQube - https://gumartinm.name/ - - Gustavo Martin Morcuende - https://gumartinm.name/ - - - - UTF-8 - 5.6.1 - 4.0 - 1.8 - helloworld - - - - - org.sonarsource.sonarqube - sonar-plugin-api - ${sonar.apiVersion} - provided - - - org.sonarsource.java - sonar-java-plugin - sonar-plugin - ${java.plugin.version} - provided - - - com.google.code.gson - gson - 2.6.2 - compile - - - com.google.guava - guava - 19.0 - - - commons-lang - commons-lang - 2.6 - - - org.sonarsource.sslr-squid-bridge - sslr-squid-bridge - 2.6.1 - - - org.codehaus.sonar.sslr - sslr-core - - - org.codehaus.sonar - sonar-plugin-api - - - org.codehaus.sonar.sslr - sslr-xpath - - - org.slf4j - jcl-over-slf4j - - - - - org.sonarsource.java - java-frontend - 4.0 - - - - - - - - org.sonarsource.sonarqube - sonar-testing-harness - ${sonar.apiVersion} - test - - - org.sonarsource.java - java-checks-testkit - ${java.plugin.version} - test - - - org.easytesting - fest-assert - 1.4 - test - - - junit - junit - 4.11 - test - - - - org.apache.logging.log4j - log4j-slf4j-impl - 2.6.1 - test - - - - org.apache.logging.log4j - log4j-core - 2.6.1 - test - - - - org.slf4j - jcl-over-slf4j - 1.7.21 - test - - - - - - - - org.sonarsource.sonar-packaging-maven-plugin - sonar-packaging-maven-plugin - 1.17 - true - - de.example.plugins.helloworld.HelloWorldPlugin - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.5.1 - - ${jdk.min.version} - ${jdk.min.version} - ${project.build.sourceEncoding} - - - - - - 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 deleted file mode 100644 index 226f174..0000000 --- a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/helloworld/checks/CheckList.java +++ /dev/null @@ -1,44 +0,0 @@ -package de.example.helloworld.checks; - -import java.util.List; - -import org.sonar.plugins.java.api.JavaCheck; - -import com.google.common.collect.ImmutableList; - -public final class CheckList { - public static final String REPOSITORY_KEY = "helloworld"; - public static final String REPOSITORY_NAME = "Hello World"; - - private CheckList() { - } - - public static List getChecks() { - return ImmutableList.builder() - .addAll(getJavaChecks()) - .addAll(getJavaTestChecks()) - .addAll(getXmlChecks()) - .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 deleted file mode 100644 index 3d4f2e2..0000000 --- a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/helloworld/checks/HelloWorldCheck.java +++ /dev/null @@ -1,43 +0,0 @@ -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; -import org.sonar.plugins.java.api.semantic.Type; -import org.sonar.plugins.java.api.tree.MethodTree; -import org.sonar.plugins.java.api.tree.Tree; -import org.sonar.plugins.java.api.tree.Tree.Kind; - -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() { - return ImmutableList.of(Kind.METHOD); - } - - @Override - public void visitNode(Tree tree) { - LOG.info("Visiting Node"); - - MethodTree method = (MethodTree) tree; - - if (method.parameters().size() == 1) { - MethodSymbol symbol = method.symbol(); - Type firstParameterType = symbol.parameterTypes().get(0); - Type returnType = symbol.returnType().type(); - if(returnType.is(firstParameterType.fullyQualifiedName())) { - reportIssue(method.simpleName(), "Never do that!"); - } - } - - } -} 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 deleted file mode 100644 index 1c431ac..0000000 --- a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldPlugin.java +++ /dev/null @@ -1,20 +0,0 @@ -package de.example.plugins.helloworld; - -import org.sonar.api.Plugin; - -import com.google.common.collect.ImmutableList; - - -public class HelloWorldPlugin implements Plugin { - - @Override - public void define(Context context) { - - ImmutableList.Builder builder = ImmutableList.builder(); - builder.add( - 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 deleted file mode 100644 index c8e3700..0000000 --- a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesCheckRegistrar.java +++ /dev/null @@ -1,14 +0,0 @@ -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/HelloWorldRulesDefinition.java b/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesDefinition.java deleted file mode 100644 index e88f017..0000000 --- a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesDefinition.java +++ /dev/null @@ -1,141 +0,0 @@ -package de.example.plugins.helloworld; - -import java.io.IOException; -import java.net.URL; -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; -import org.sonar.api.server.rule.RulesDefinitionAnnotationLoader; -import org.sonar.api.utils.AnnotationUtils; -import org.sonar.check.Cardinality; -import org.sonar.plugins.java.Java; -import org.sonar.squidbridge.annotations.RuleTemplate; -import org.sonar.squidbridge.rules.ExternalDescriptionLoader; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Charsets; -import com.google.common.collect.Iterables; -import com.google.common.io.Resources; -import com.google.gson.Gson; - -import de.example.helloworld.checks.CheckList; - -/** - * Definition of rules. - */ -public class HelloWorldRulesDefinition implements RulesDefinition { - private static final String RESOURCE_BASE_PATH = "/de/example/l10n/helloworld/rules/helloworld"; - - private final Gson gson = new Gson(); - - @Override - public void define(Context context) { - NewRepository repository = context - .createRepository(CheckList.REPOSITORY_KEY, Java.KEY) - .setName(CheckList.REPOSITORY_NAME); - List checks = CheckList.getChecks(); - new RulesDefinitionAnnotationLoader().load(repository, Iterables.toArray(checks, Class.class)); - for (Class ruleClass : checks) { - newRule(ruleClass, repository); - } - repository.done(); - } - - @VisibleForTesting - protected void newRule(Class ruleClass, NewRepository repository) { - - org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); - if (ruleAnnotation == null) { - throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); - } - String ruleKey = ruleAnnotation.key(); - if (StringUtils.isEmpty(ruleKey)) { - throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); - } - NewRule rule = repository.rule(ruleKey); - if (rule == null) { - throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key()); - } - rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null); - if (ruleAnnotation.cardinality() == Cardinality.MULTIPLE) { - throw new IllegalArgumentException("Cardinality is not supported, use the RuleTemplate annotation instead for " + ruleClass); - } - ruleMetadata(ruleClass, rule); - } - - 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); - - } - - private void addMetadata(NewRule rule, String metadataKey) { - URL resource = ExternalDescriptionLoader.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.json"); - if (resource != null) { - RuleMetatada metatada = gson.fromJson(readResource(resource), RuleMetatada.class); - rule.setSeverity(metatada.defaultSeverity.toUpperCase()); - rule.setName(metatada.title); - rule.addTags(metatada.tags); - rule.setStatus(RuleStatus.valueOf(metatada.status.toUpperCase())); - if(metatada.remediation != null) { - rule.setDebtRemediationFunction(metatada.remediation.remediationFunction(rule.debtRemediationFunctions())); - rule.setGapDescription(metatada.remediation.linearDesc); - } - } - } - - private static void addHtmlDescription(NewRule rule, String metadataKey) { - URL resource = HelloWorldRulesDefinition.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.html"); - if (resource != null) { - rule.setHtmlDescription(readResource(resource)); - } - } - - 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); - } - } - - private static class RuleMetatada { - String title; - String status; - @Nullable - Remediation remediation; - - String[] tags; - String defaultSeverity; - } - - private static class Remediation { - String func; - String constantCost; - String linearDesc; - String linearOffset; - String linearFactor; - - public DebtRemediationFunction remediationFunction(DebtRemediationFunctions drf) { - if(func.startsWith("Constant")) { - return drf.constantPerIssue(constantCost.replace("mn", "min")); - } - if("Linear".equals(func)) { - return drf.linear(linearFactor.replace("mn", "min")); - } - return drf.linearWithOffset(linearFactor.replace("mn", "min"), linearOffset.replace("mn", "min")); - } - } - -} 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 deleted file mode 100644 index 4019e1b..0000000 --- a/Sonar/Plugins/hello-world-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldSensor.java +++ /dev/null @@ -1,21 +0,0 @@ -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) { - - } - -} diff --git a/Sonar/Plugins/hello-world-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/HELLOWORLD0001_java.html b/Sonar/Plugins/hello-world-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/HELLOWORLD0001_java.html deleted file mode 100644 index d0f8e3f..0000000 --- a/Sonar/Plugins/hello-world-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/HELLOWORLD0001_java.html +++ /dev/null @@ -1,18 +0,0 @@ -

HelloWorld rule description.

-

Noncompliant Code Example

-
-class MyClass {
-	
-	int foo1(int value) { return 0; }
-		
-	MyClass foo2(MyClass value) { return null; }
-	
-	...
- 
-}
-
-

See

-
    -
  • CERT, MSC11-J. - Do not let session information leak within a servlet
  • -
- diff --git a/Sonar/Plugins/hello-world-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/HELLOWORLD0001_java.json b/Sonar/Plugins/hello-world-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/HELLOWORLD0001_java.json deleted file mode 100644 index 4421cd9..0000000 --- a/Sonar/Plugins/hello-world-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/HELLOWORLD0001_java.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "title": "HelloWorld SonarQube rule", - "status": "ready", - "remediation": { - "func": "Constant\/Issue", - "constantCost": "5min" - }, - "tags": [ - "bad-practice" - ], - "defaultSeverity": "Major" -} diff --git a/Sonar/Plugins/hello-world-plugin/src/test/files/HelloWorldCheck.java b/Sonar/Plugins/hello-world-plugin/src/test/files/HelloWorldCheck.java deleted file mode 100644 index 488a749..0000000 --- a/Sonar/Plugins/hello-world-plugin/src/test/files/HelloWorldCheck.java +++ /dev/null @@ -1,17 +0,0 @@ -class MyClass { - - MyClass(MyClass mc) { } - - int foo1() { return 0; } - - void foo2(int value) { } - - // Noncompliant@+1 [[startColumn=6;endLine=+0;endColumn=10;effortToFix=4]] {{Never do that!}} - int foo3(int value) { return 0; } - - Object foo4(int value) { return null; } - - // Noncompliant@+1 [[startColumn=10;endLine=+0;endColumn=14;effortToFix=4]] {{Never do that!}} - MyClass foo5(MyClass value) { return null; } - -} diff --git a/Sonar/Plugins/hello-world-plugin/src/test/java/de/example/helloworld/checks/HelloWorldCheckTest.java b/Sonar/Plugins/hello-world-plugin/src/test/java/de/example/helloworld/checks/HelloWorldCheckTest.java deleted file mode 100644 index d941f61..0000000 --- a/Sonar/Plugins/hello-world-plugin/src/test/java/de/example/helloworld/checks/HelloWorldCheckTest.java +++ /dev/null @@ -1,12 +0,0 @@ -package de.example.helloworld.checks; - -import org.junit.Test; -import org.sonar.java.checks.verifier.JavaCheckVerifier; - -public class HelloWorldCheckTest { - - @Test - public void test() { - JavaCheckVerifier.verify("src/test/files/HelloWorldCheck.java", new HelloWorldCheck()); - } -} diff --git a/Sonar/Plugins/pom.xml b/Sonar/Plugins/pom.xml new file mode 100644 index 0000000..dd1430d --- /dev/null +++ b/Sonar/Plugins/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + de.example.plugins + custom-sonar-plugins + 0.1-SNAPSHOT + pom + Custom SonarQube Plugins + Making custom SonarQube plugins + https://gumartinm.name + + Gustavo Martin Morcuende + https://gumartinm.name/ + + + + scm:git:https://git.gumartinm.name/JavaForFun + https://git.gumartinm.name/JavaForFun + + + + sonar-custom-java-plugin + + + + diff --git a/Sonar/Plugins/sonar-custom-java-plugin/.pom.xml.swp b/Sonar/Plugins/sonar-custom-java-plugin/.pom.xml.swp new file mode 100644 index 0000000000000000000000000000000000000000..4c402d5e3f8aca6aba46e06a410970b2855aa00f GIT binary patch literal 20480 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 + + 4.0.0 + + + de.example.plugins + custom-sonar-plugins + 0.1-SNAPSHOT + + + sonar-custom-java-plugin + sonar-plugin + + SonarQube custom Java plugin + Java plugin for SonarQube + + + UTF-8 + 5.6.1 + 4.0 + 1.8 + helloworld + + + + + org.sonarsource.sonarqube + sonar-plugin-api + ${sonar.apiVersion} + provided + + + org.sonarsource.java + sonar-java-plugin + sonar-plugin + ${java.plugin.version} + provided + + + com.google.code.gson + gson + 2.6.2 + compile + + + com.google.guava + guava + 19.0 + + + commons-lang + commons-lang + 2.6 + + + org.sonarsource.sslr-squid-bridge + sslr-squid-bridge + 2.6.1 + + + org.codehaus.sonar.sslr + sslr-core + + + org.codehaus.sonar + sonar-plugin-api + + + org.codehaus.sonar.sslr + sslr-xpath + + + org.slf4j + jcl-over-slf4j + + + + + org.sonarsource.java + java-frontend + 4.0 + + + + + + + + org.sonarsource.sonarqube + sonar-testing-harness + ${sonar.apiVersion} + test + + + org.sonarsource.java + java-checks-testkit + ${java.plugin.version} + test + + + org.easytesting + fest-assert + 1.4 + test + + + junit + junit + 4.11 + test + + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.6.1 + test + + + + org.apache.logging.log4j + log4j-core + 2.6.1 + test + + + + org.slf4j + jcl-over-slf4j + 1.7.21 + test + + + + + + + + org.sonarsource.sonar-packaging-maven-plugin + sonar-packaging-maven-plugin + 1.17 + true + + de.example.plugins.helloworld.HelloWorldPlugin + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + ${jdk.min.version} + ${jdk.min.version} + ${project.build.sourceEncoding} + + + + + + diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/helloworld/checks/CheckList.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/helloworld/checks/CheckList.java new file mode 100644 index 0000000..f9053a8 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/helloworld/checks/CheckList.java @@ -0,0 +1,45 @@ +package de.example.helloworld.checks; + +import java.util.List; + +import org.sonar.plugins.java.api.JavaCheck; + +import com.google.common.collect.ImmutableList; + +public final class CheckList { + public static final String REPOSITORY_KEY = "helloworld"; + public static final String REPOSITORY_NAME = "Hello World"; + + private CheckList() { + } + + public static List getChecks() { + return ImmutableList.builder() + .addAll(getJavaChecks()) + .addAll(getJavaTestChecks()) + .addAll(getXmlChecks()) + .build(); + } + + public static List> getJavaChecks() { + return ImmutableList.>builder() + .add(HelloWorldCheck.class) + .add(SpringServiceInstanceFieldCheck.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/sonar-custom-java-plugin/src/main/java/de/example/helloworld/checks/HelloWorldCheck.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/helloworld/checks/HelloWorldCheck.java new file mode 100644 index 0000000..40eedf2 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/helloworld/checks/HelloWorldCheck.java @@ -0,0 +1,43 @@ +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; +import org.sonar.plugins.java.api.semantic.Type; +import org.sonar.plugins.java.api.tree.MethodTree; +import org.sonar.plugins.java.api.tree.Tree; +import org.sonar.plugins.java.api.tree.Tree.Kind; + +import com.google.common.collect.ImmutableList; + +@Rule(key = "GU0001") +public class HelloWorldCheck extends IssuableSubscriptionVisitor { + private static final Logger LOG = Loggers.get(HelloWorldCheck.class); + + + @Override + public List nodesToVisit() { + return ImmutableList.of(Kind.METHOD); + } + + @Override + public void visitNode(Tree tree) { + LOG.info("Visiting Node"); + + MethodTree method = (MethodTree) tree; + + if (method.parameters().size() == 1) { + MethodSymbol symbol = method.symbol(); + Type firstParameterType = symbol.parameterTypes().get(0); + Type returnType = symbol.returnType().type(); + if(returnType.is(firstParameterType.fullyQualifiedName())) { + reportIssue(method.simpleName(), "Never do that!"); + } + } + + } +} diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/helloworld/checks/SpringServiceInstanceFieldCheck.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/helloworld/checks/SpringServiceInstanceFieldCheck.java new file mode 100644 index 0000000..db779fe --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/helloworld/checks/SpringServiceInstanceFieldCheck.java @@ -0,0 +1,51 @@ +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.JavaFileScannerContext; +import org.sonar.plugins.java.api.semantic.Symbol; +import org.sonar.plugins.java.api.tree.ClassTree; +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.collect.ImmutableList; + +@Rule(key = "GU0002") +public class SpringServiceInstanceFieldCheck extends IssuableSubscriptionVisitor { + private static final Logger LOG = Loggers.get(SpringServiceInstanceFieldCheck.class); + + private JavaFileScannerContext context; + + + @Override + public List nodesToVisit() { + return ImmutableList.of(Kind.CLASS, Kind.VARIABLE); + } + + @Override + public void visitNode(Tree tree) { + if (tree.is(Kind.CLASS) && isSpringService((ClassTree) tree)) { + + } + + } + + + private static boolean isOwnedByASpringService(VariableTree variable) { + Symbol owner = variable.symbol().owner(); + return owner.isTypeSymbol() && (owner.type().isSubtypeOf("javax.servlet.http.HttpServlet") || owner.type().isSubtypeOf("org.apache.struts.action.Action")); + } + + private static boolean isSpringService(ClassTree tree) { + tree.symbol().metadata().isAnnotatedWith("javax.inject.Inject"); + return true; + + } + + +} diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldPlugin.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldPlugin.java new file mode 100644 index 0000000..1c431ac --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldPlugin.java @@ -0,0 +1,20 @@ +package de.example.plugins.helloworld; + +import org.sonar.api.Plugin; + +import com.google.common.collect.ImmutableList; + + +public class HelloWorldPlugin implements Plugin { + + @Override + public void define(Context context) { + + ImmutableList.Builder builder = ImmutableList.builder(); + builder.add( + HelloWorldRulesDefinition.class, + HelloWorldRulesCheckRegistrar.class); + + context.addExtensions(builder.build()); + } +} diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldProfile.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldProfile.java new file mode 100644 index 0000000..ac02c42 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldProfile.java @@ -0,0 +1,70 @@ +package de.example.plugins.helloworld; + +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.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 HelloWorldProfile extends ProfileDefinition { + + private final Gson gson = new Gson(); + private final RuleFinder ruleFinder; + public HelloWorldProfile(RuleFinder ruleFinder) { + this.ruleFinder = ruleFinder; + } + + @Override + public RulesProfile createProfile(ValidationMessages messages) { + RulesProfile profile = RulesProfile.create("Sonar way", Java.KEY); + URL resource = JavaRulesDefinition.class.getResource("/org/sonar/l10n/java/rules/squid/Sonar_way_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); + } + } + + 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; + } + + private static class Profile { + String name; + List ruleKeys; + } + + } diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesCheckRegistrar.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesCheckRegistrar.java new file mode 100644 index 0000000..c8e3700 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-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/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesDefinition.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesDefinition.java new file mode 100644 index 0000000..e88f017 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldRulesDefinition.java @@ -0,0 +1,141 @@ +package de.example.plugins.helloworld; + +import java.io.IOException; +import java.net.URL; +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; +import org.sonar.api.server.rule.RulesDefinitionAnnotationLoader; +import org.sonar.api.utils.AnnotationUtils; +import org.sonar.check.Cardinality; +import org.sonar.plugins.java.Java; +import org.sonar.squidbridge.annotations.RuleTemplate; +import org.sonar.squidbridge.rules.ExternalDescriptionLoader; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Charsets; +import com.google.common.collect.Iterables; +import com.google.common.io.Resources; +import com.google.gson.Gson; + +import de.example.helloworld.checks.CheckList; + +/** + * Definition of rules. + */ +public class HelloWorldRulesDefinition implements RulesDefinition { + private static final String RESOURCE_BASE_PATH = "/de/example/l10n/helloworld/rules/helloworld"; + + private final Gson gson = new Gson(); + + @Override + public void define(Context context) { + NewRepository repository = context + .createRepository(CheckList.REPOSITORY_KEY, Java.KEY) + .setName(CheckList.REPOSITORY_NAME); + List checks = CheckList.getChecks(); + new RulesDefinitionAnnotationLoader().load(repository, Iterables.toArray(checks, Class.class)); + for (Class ruleClass : checks) { + newRule(ruleClass, repository); + } + repository.done(); + } + + @VisibleForTesting + protected void newRule(Class ruleClass, NewRepository repository) { + + org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class); + if (ruleAnnotation == null) { + throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass); + } + String ruleKey = ruleAnnotation.key(); + if (StringUtils.isEmpty(ruleKey)) { + throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass); + } + NewRule rule = repository.rule(ruleKey); + if (rule == null) { + throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key()); + } + rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null); + if (ruleAnnotation.cardinality() == Cardinality.MULTIPLE) { + throw new IllegalArgumentException("Cardinality is not supported, use the RuleTemplate annotation instead for " + ruleClass); + } + ruleMetadata(ruleClass, rule); + } + + 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); + + } + + private void addMetadata(NewRule rule, String metadataKey) { + URL resource = ExternalDescriptionLoader.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.json"); + if (resource != null) { + RuleMetatada metatada = gson.fromJson(readResource(resource), RuleMetatada.class); + rule.setSeverity(metatada.defaultSeverity.toUpperCase()); + rule.setName(metatada.title); + rule.addTags(metatada.tags); + rule.setStatus(RuleStatus.valueOf(metatada.status.toUpperCase())); + if(metatada.remediation != null) { + rule.setDebtRemediationFunction(metatada.remediation.remediationFunction(rule.debtRemediationFunctions())); + rule.setGapDescription(metatada.remediation.linearDesc); + } + } + } + + private static void addHtmlDescription(NewRule rule, String metadataKey) { + URL resource = HelloWorldRulesDefinition.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.html"); + if (resource != null) { + rule.setHtmlDescription(readResource(resource)); + } + } + + 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); + } + } + + private static class RuleMetatada { + String title; + String status; + @Nullable + Remediation remediation; + + String[] tags; + String defaultSeverity; + } + + private static class Remediation { + String func; + String constantCost; + String linearDesc; + String linearOffset; + String linearFactor; + + public DebtRemediationFunction remediationFunction(DebtRemediationFunctions drf) { + if(func.startsWith("Constant")) { + return drf.constantPerIssue(constantCost.replace("mn", "min")); + } + if("Linear".equals(func)) { + return drf.linear(linearFactor.replace("mn", "min")); + } + return drf.linearWithOffset(linearFactor.replace("mn", "min"), linearOffset.replace("mn", "min")); + } + } + +} diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldSensor.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/helloworld/HelloWorldSensor.java new file mode 100644 index 0000000..4019e1b --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-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) { + + } + +} diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/GU0001_java.html b/Sonar/Plugins/sonar-custom-java-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/GU0001_java.html new file mode 100644 index 0000000..d0f8e3f --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/GU0001_java.html @@ -0,0 +1,18 @@ +

HelloWorld rule description.

+

Noncompliant Code Example

+
+class MyClass {
+	
+	int foo1(int value) { return 0; }
+		
+	MyClass foo2(MyClass value) { return null; }
+	
+	...
+ 
+}
+
+

See

+
    +
  • CERT, MSC11-J. - Do not let session information leak within a servlet
  • +
+ diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/GU0001_java.json b/Sonar/Plugins/sonar-custom-java-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/GU0001_java.json new file mode 100644 index 0000000..4421cd9 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/resources/de/example/l10n/helloworld/rules/helloworld/GU0001_java.json @@ -0,0 +1,12 @@ +{ + "title": "HelloWorld SonarQube rule", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "bad-practice" + ], + "defaultSeverity": "Major" +} diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/test/files/checks/HelloWorldCheck.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/files/checks/HelloWorldCheck.java new file mode 100644 index 0000000..488a749 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/files/checks/HelloWorldCheck.java @@ -0,0 +1,17 @@ +class MyClass { + + MyClass(MyClass mc) { } + + int foo1() { return 0; } + + void foo2(int value) { } + + // Noncompliant@+1 [[startColumn=6;endLine=+0;endColumn=10;effortToFix=4]] {{Never do that!}} + int foo3(int value) { return 0; } + + Object foo4(int value) { return null; } + + // Noncompliant@+1 [[startColumn=10;endLine=+0;endColumn=14;effortToFix=4]] {{Never do that!}} + MyClass foo5(MyClass value) { return null; } + +} diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/test/files/checks/SpringServiceInstanceFieldCheck.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/files/checks/SpringServiceInstanceFieldCheck.java new file mode 100644 index 0000000..16a7c80 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/files/checks/SpringServiceInstanceFieldCheck.java @@ -0,0 +1,18 @@ +import javax.inject.Named; + +@Named("aService") +public class AService { + private static final Integer FIELD1; + + private final Integer field2; + + private Integer field3; + + public static final Integer field4; + + public final Integer field5; + + // Noncompliant@+1 [[startColumn=6;endLine=+0;endColumn=10;effortToFix=4]] {{Remove this misleading mutable service instance fields or make it \"static\" and/or \"final\"}} + public Integer field6; + +} diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/helloworld/checks/HelloWorldCheckTest.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/helloworld/checks/HelloWorldCheckTest.java new file mode 100644 index 0000000..34885a8 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/helloworld/checks/HelloWorldCheckTest.java @@ -0,0 +1,12 @@ +package de.example.helloworld.checks; + +import org.junit.Test; +import org.sonar.java.checks.verifier.JavaCheckVerifier; + +public class HelloWorldCheckTest { + + @Test + public void test() { + JavaCheckVerifier.verify("src/test/files/checks/HelloWorldCheck.java", new HelloWorldCheck()); + } +} diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/helloworld/checks/SpringServiceInstanceFieldCheckTest.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/helloworld/checks/SpringServiceInstanceFieldCheckTest.java new file mode 100644 index 0000000..4b0e3a4 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/helloworld/checks/SpringServiceInstanceFieldCheckTest.java @@ -0,0 +1,13 @@ +package de.example.helloworld.checks; + +import org.junit.Test; +import org.sonar.java.checks.verifier.JavaCheckVerifier; + +public class SpringServiceInstanceFieldCheckTest { + private static final String FILENAME = "src/test/files/checks/SpringServiceInstanceFieldCheck.java"; + + @Test + public void test() { + JavaCheckVerifier.verify(FILENAME, new SpringServiceInstanceFieldCheck()); + } +} -- 2.1.4