Skip to content

Commit

Permalink
green-code-initiative#92 refactor(rule/java): moves Java rules into `…
Browse files Browse the repository at this point in the history
…ecocode-rule-specification` module
  • Loading branch information
jycr committed Apr 12, 2023
1 parent 2bc395c commit 182aa39
Show file tree
Hide file tree
Showing 46 changed files with 164 additions and 130 deletions.
22 changes: 22 additions & 0 deletions ecocode-rule-specification/src/main/assembly/java.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
<id>java</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>io/ecocode/rules/java/*.class</include>
</includes>
<outputDirectory/>
</fileSet>
<fileSet>
<directory>${project.build.directory}/java</directory>
<outputDirectory>io/ecocode/rules/java/specifications</outputDirectory>
</fileSet>
</fileSets>
</assembly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.ecocode.rules.java;

public class JavaRulesRepository {
public static final String REPOSITORY_KEY = "ecocode-java";
public static final String NAME = "ecoCode";
public static final String LANGUAGE = "java";
public static final String RESOURCE_BASE_PATH = JavaRulesRepository.class.getPackageName().replace('.', '/') + "/specifications";
}
29 changes: 29 additions & 0 deletions ecocode-rules-specifications/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,42 @@
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<target>
<copy todir="${project.build.directory}/java">
<fileset dir="${project.build.directory}/rules">
<include name="*/java/EC*.html"/>
<include name="*/EC*.json"/>
</fileset>
<flattenmapper/>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<id>assembly-java</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/java.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
Expand Down
20 changes: 15 additions & 5 deletions java-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,30 @@
<url>https://github.com/green-code-initiative/ecoCode/tree/main/java-plugin</url>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>ecocode-rules-specifications</artifactId>
<version>${project.version}</version>
<classifier>java</classifier>
</dependency>

<dependency>
<groupId>org.sonarsource.java</groupId>
<artifactId>sonar-java-plugin</artifactId>
<type>sonar-plugin</type>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api</artifactId>
</dependency>

<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api-impl</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.sonarsource.analyzer-commons</groupId>
<artifactId>sonar-analyzer-commons</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
Expand Down Expand Up @@ -133,6 +137,12 @@
<exclude>META-INF/**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:ecocode-rules-specifications</artifact>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@

import java.util.List;

import io.ecocode.rules.java.JavaRulesRepository;
import org.sonar.plugins.java.api.CheckRegistrar;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonarsource.api.sonarlint.SonarLintSide;

import static fr.greencodeinitiative.java.JavaRulesDefinition.ANNOTATED_RULE_CLASSES;
import static fr.greencodeinitiative.java.JavaRulesDefinition.ANNOTATED_RULE_TEST_CLASSES;

/**
* Provide the "checks" (implementations of rules) classes that are going be executed during
* source code analysis.
Expand All @@ -40,20 +44,20 @@ public class JavaCheckRegistrar implements CheckRegistrar {
@Override
public void register(RegistrarContext registrarContext) {
// Call to registerClassesForRepository to associate the classes with the correct repository key
registrarContext.registerClassesForRepository(JavaRulesDefinition.REPOSITORY_KEY, checkClasses(), testCheckClasses());
registrarContext.registerClassesForRepository(JavaRulesRepository.REPOSITORY_KEY, checkClasses(), testCheckClasses());
}

/**
* Lists all the main checks provided by the plugin
*/
public static List<Class<? extends JavaCheck>> checkClasses() {
return RulesList.getJavaChecks();
return ANNOTATED_RULE_CLASSES;
}

/**
* Lists all the test checks provided by the plugin
*/
public static List<Class<? extends JavaCheck>> testCheckClasses() {
return RulesList.getJavaTestChecks();
return ANNOTATED_RULE_TEST_CLASSES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,99 @@
*/
package fr.greencodeinitiative.java;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;

import fr.greencodeinitiative.java.checks.ArrayCopyCheck;
import fr.greencodeinitiative.java.checks.AvoidConcatenateStringsInLoop;
import fr.greencodeinitiative.java.checks.AvoidFullSQLRequest;
import fr.greencodeinitiative.java.checks.AvoidGettingSizeCollectionInLoop;
import fr.greencodeinitiative.java.checks.AvoidMultipleIfElseStatement;
import fr.greencodeinitiative.java.checks.AvoidRegexPatternNotStatic;
import fr.greencodeinitiative.java.checks.AvoidSQLRequestInLoop;
import fr.greencodeinitiative.java.checks.AvoidSetConstantInBatchUpdate;
import fr.greencodeinitiative.java.checks.AvoidSpringRepositoryCallInLoopCheck;
import fr.greencodeinitiative.java.checks.AvoidStatementForDMLQueries;
import fr.greencodeinitiative.java.checks.AvoidUsageOfStaticCollections;
import fr.greencodeinitiative.java.checks.AvoidUsingGlobalVariablesCheck;
import fr.greencodeinitiative.java.checks.FreeResourcesOfAutoCloseableInterface;
import fr.greencodeinitiative.java.checks.IncrementCheck;
import fr.greencodeinitiative.java.checks.InitializeBufferWithAppropriateSize;
import fr.greencodeinitiative.java.checks.NoFunctionCallWhenDeclaringForLoop;
import fr.greencodeinitiative.java.checks.OptimizeReadFileExceptions;
import fr.greencodeinitiative.java.checks.UnnecessarilyAssignValuesToVariables;
import fr.greencodeinitiative.java.checks.UseCorrectForLoop;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarProduct;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.internal.SonarRuntimeImpl;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.Version;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonarsource.analyzer.commons.RuleMetadataLoader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static io.ecocode.rules.java.JavaRulesRepository.LANGUAGE;
import static io.ecocode.rules.java.JavaRulesRepository.NAME;
import static io.ecocode.rules.java.JavaRulesRepository.REPOSITORY_KEY;
import static io.ecocode.rules.java.JavaRulesRepository.RESOURCE_BASE_PATH;

/**
* Declare rule metadata in server repository of rules.
* That allows to list the rules in the page "Rules".
*/
public class JavaRulesDefinition implements RulesDefinition {
static final List<Class<? extends JavaCheck>> ANNOTATED_RULE_CLASSES = List.of(
ArrayCopyCheck.class,
IncrementCheck.class,
AvoidConcatenateStringsInLoop.class,
AvoidUsageOfStaticCollections.class,
AvoidGettingSizeCollectionInLoop.class,
AvoidRegexPatternNotStatic.class,
NoFunctionCallWhenDeclaringForLoop.class,
AvoidStatementForDMLQueries.class,
AvoidSpringRepositoryCallInLoopCheck.class,
AvoidSQLRequestInLoop.class,
AvoidFullSQLRequest.class,
UseCorrectForLoop.class,
UnnecessarilyAssignValuesToVariables.class,
OptimizeReadFileExceptions.class,
InitializeBufferWithAppropriateSize.class,
AvoidUsingGlobalVariablesCheck.class,
AvoidSetConstantInBatchUpdate.class,
FreeResourcesOfAutoCloseableInterface.class,
AvoidMultipleIfElseStatement.class
);

// don't change that because the path is hard coded in CheckVerifier
private static final String RESOURCE_BASE_PATH = "fr/greencodeinitiative/l10n/java/rules/java";


// Add the rule keys of the rules which need to be considered as template-rules
private static final Set<String> RULE_TEMPLATES_KEY = Collections.emptySet();
public static final String NAME = "ecoCode";
public static final String LANGUAGE = "java";
public static final String REPOSITORY_KEY = "ecocode-java";

static final List<Class<? extends JavaCheck>> ANNOTATED_RULE_TEST_CLASSES = Collections.emptyList();
private static final Version SONARQUBE_RUNTIME_VERSION = Version.create(9, 8);
private static final SonarRuntime SONARQUBE_RUNTIME = new SonarRuntime() {
@Override
public void define(Context context) {
NewRepository repository = context.createRepository(REPOSITORY_KEY, LANGUAGE).setName(NAME);

SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.create(9, 8), SonarQubeSide.SCANNER, SonarEdition.DEVELOPER);

RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH, sonarRuntime);

ruleMetadataLoader.addRulesByAnnotatedClass(repository, new ArrayList<>(RulesList.getChecks()));
public Version getApiVersion() {
return SONARQUBE_RUNTIME_VERSION;
}

setTemplates(repository);
@Override
public SonarProduct getProduct() {
return SonarProduct.SONARQUBE;
}

repository.done();
@Override
public SonarQubeSide getSonarQubeSide() {
return SonarQubeSide.SCANNER;
}

private static void setTemplates(NewRepository repository) {
RULE_TEMPLATES_KEY.stream()
.map(repository::rule)
.filter(Objects::nonNull)
.forEach(rule -> rule.setTemplate(true));
@Override
public SonarEdition getEdition() {
return SonarEdition.COMMUNITY;
}
};

@Override
public void define(Context context) {
NewRepository repository = context.createRepository(REPOSITORY_KEY, LANGUAGE).setName(NAME);
RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH, SONARQUBE_RUNTIME);
ruleMetadataLoader.addRulesByAnnotatedClass(repository, new ArrayList<>(ANNOTATED_RULE_CLASSES));
repository.done();
}
}

This file was deleted.

Loading

0 comments on commit 182aa39

Please sign in to comment.