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-rules-specifications` module
  • Loading branch information
jycr committed Jul 9, 2023
1 parent cdcb3f2 commit a8d9f34
Show file tree
Hide file tree
Showing 46 changed files with 156 additions and 118 deletions.
23 changes: 23 additions & 0 deletions ecocode-rules-specifications/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/java">
<fileset dir="${project.build.directory}/rules">
<include name="*/java/EC*.html"/>
<include name="*/EC*.json"/>
</fileset>
<flattenmapper/>
</copy>
</target>
</configuration>
</execution>
</executions>
</plugin>
Expand All @@ -129,6 +140,18 @@
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.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
18 changes: 18 additions & 0 deletions ecocode-rules-specifications/src/main/assembly/java.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<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/*.*</include>
</includes>
<outputDirectory/>
</fileSet>
</fileSets>
</assembly>
13 changes: 8 additions & 5 deletions java-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@
<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.analyzer-commons</groupId>
<artifactId>sonar-analyzer-commons</artifactId>
<scope>provided</scope>
</dependency>

<!-- for security on regex patterns : https://github.com/google/re2j -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,28 @@
*/
package fr.greencodeinitiative.java;

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

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.plugins.java.api.CheckRegistrar;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonarsource.api.sonarlint.SonarLintSide;
Expand All @@ -30,6 +50,27 @@
*/
@SonarLintSide
public class JavaCheckRegistrar implements CheckRegistrar {
private 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
);

/**
* Register the classes that will be used to instantiate checks during analysis.
Expand All @@ -44,13 +85,13 @@ public void register(RegistrarContext registrarContext) {
* 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 Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,22 @@
*/
package fr.greencodeinitiative.java;

import java.util.ArrayList;

import org.sonar.api.SonarRuntime;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonarsource.analyzer.commons.RuleMetadataLoader;

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

/**
* Declare rule metadata in server repository of rules.
* That allows to list the rules in the page "Rules".
*/
public class JavaRulesDefinition implements RulesDefinition {
private static final String RESOURCE_BASE_PATH = "io/ecocode/rules/java";

// 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";
private static final String NAME = "ecoCode";
private static final String LANGUAGE = "java";
static final String REPOSITORY_KEY = "ecocode-java";

private final SonarRuntime sonarRuntime;

Expand All @@ -53,17 +45,11 @@ public void define(Context context) {

RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH, sonarRuntime);

ruleMetadataLoader.addRulesByAnnotatedClass(repository, new ArrayList<>(RulesList.getChecks()));

setTemplates(repository);

ruleMetadataLoader.addRulesByAnnotatedClass(repository, new ArrayList<>(JavaCheckRegistrar.checkClasses()));
repository.done();
}

private static void setTemplates(NewRepository repository) {
RULE_TEMPLATES_KEY.stream()
.map(repository::rule)
.filter(Objects::nonNull)
.forEach(rule -> rule.setTemplate(true));
public String repositoryKey() {
return REPOSITORY_KEY;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package fr.greencodeinitiative.java;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.sonar.api.Plugin;
import org.sonar.api.SonarRuntime;
Expand All @@ -24,14 +25,17 @@
import static org.mockito.Mockito.mock;

class JavaPluginTest {
private Plugin.Context context;

@Test
void testName() {
@BeforeEach
void init() {
SonarRuntime sonarRuntime = mock(SonarRuntime.class);
Plugin.Context context = new Plugin.Context(sonarRuntime);

context = new Plugin.Context(sonarRuntime);
new JavaPlugin().define(context);
}

@Test
void test() {
assertThat(context.getExtensions()).hasSize(2);
}

Expand Down
Loading

0 comments on commit a8d9f34

Please sign in to comment.