From 68886520f88fac7eec82a471450bd9428d936684 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sat, 28 Jan 2023 23:07:27 +0100 Subject: [PATCH] refacto 'cnumr' to 'greencodeinitiative' --- INSTALL.md | 36 +-- _TO_CHECK_ecolinter-plugin/pom.xml | 4 +- .../cnumr/ecolinter/MyEcoLintRulesPlugin.java | 2 +- .../ecolinter/MyEslintRulesDefinition.java | 4 +- .../ecolinter/MyStylelintRulesDefinition.java | 4 +- .../l10n/ecolint/rules/eslint/greenit.json | 0 .../l10n/ecolint/rules/stylelint/greenit.json | 0 .../ecolinter/MyEcoLintRulesPluginTest.java | 2 +- .../MyEslintRulesDefinitionTest.java | 2 +- .../MyStylelintRulesDefinitionTest.java | 2 +- .../AvoidSQLRequestInLoopCheckTest.java | 2 +- _TO_CHECK_xml-plugin/pom.xml | 13 +- .../main/java/fr/cnumr/xml/MyXmlRules.java | 30 +- .../fr/cnumr/xml/XMLCustomRulesPlugin.java | 8 +- .../java/fr/cnumr/xml/checks/CheckList.java | 2 +- .../xml/checks/UseUnoptimizedVectorImage.java | 4 +- .../l10n/xml/rules/custom/S99.html | 0 .../l10n/xml/rules/custom/S99.json | 0 .../java/fr/cnumr/xml/MyXmlRulesTest.java | 4 +- .../checks/UseUnoptimizedVectorImageTest.java | 2 +- java-plugin/pom.xml | 2 +- .../fr/cnumr/java/checks/ArrayCopyCheck.java | 284 ------------------ .../checks/AvoidSetConstantInBatchUpdate.java | 75 ----- .../java/checks/ConstOrLiteralDeclare.java | 198 ------------ .../fr/cnumr/java/utils/PrinterVisitor.java | 71 ----- .../java/JavaCheckRegistrar.java | 2 +- .../java/JavaPlugin.java | 2 +- .../java/JavaRulesDefinition.java | 6 +- .../java/RulesList.java | 28 +- .../java/checks/ArrayCopyCheck.java | 284 ++++++++++++++++++ .../checks/AvoidConcatenateStringsInLoop.java | 17 +- .../java/checks/AvoidFullSQLRequest.java | 25 +- .../AvoidGettingSizeCollectionInLoop.java | 2 +- .../checks/AvoidMultipleIfElseStatement.java | 2 +- .../checks/AvoidRegexPatternNotStatic.java | 13 +- .../java/checks/AvoidSQLRequestInLoop.java | 51 ++-- .../checks/AvoidSetConstantInBatchUpdate.java | 74 +++++ .../AvoidSpringRepositoryCallInLoopCheck.java | 14 +- .../checks/AvoidStatementForDMLQueries.java | 22 +- .../checks/AvoidUsageOfStaticCollections.java | 13 +- .../AvoidUsingGlobalVariablesCheck.java | 2 +- .../java/checks/ConstOrLiteralDeclare.java | 212 +++++++++++++ ...FreeResourcesOfAutoCloseableInterface.java | 15 +- .../java/checks/IncrementCheck.java | 11 +- .../InitializeBufferWithAppropriateSize.java | 10 +- .../NoFunctionCallWhenDeclaringForLoop.java | 2 +- .../checks/OptimizeReadFileExceptions.java | 2 +- .../UnnecessarilyAssignValuesToVariables.java | 2 +- .../java/checks/UseCorrectForLoop.java | 17 +- .../java/checks}/package-info.java | 2 +- .../java}/package-info.java | 2 +- .../java/utils/PrinterVisitor.java | 73 +++++ .../java/utils/StringUtils.java | 26 +- .../java/utils}/package-info.java | 2 +- .../l10n/java/rules/java/AMIES.html | 0 .../l10n/java/rules/java/AMIES.json | 0 .../l10n/java/rules/java/D4.html | 0 .../l10n/java/rules/java/D4.json | 0 .../l10n/java/rules/java/GRC1.html | 0 .../l10n/java/rules/java/GRC1.json | 0 .../l10n/java/rules/java/GRPS0027.html | 0 .../l10n/java/rules/java/GRPS0027.json | 0 .../l10n/java/rules/java/GRSP0028.html | 0 .../l10n/java/rules/java/GRSP0028.json | 0 .../l10n/java/rules/java/GRSP0032.html | 0 .../l10n/java/rules/java/GRSP0032.json | 0 .../l10n/java/rules/java/GSCIL.html | 0 .../l10n/java/rules/java/GSCIL.json | 0 .../l10n/java/rules/java/S53.html | 0 .../l10n/java/rules/java/S53.json | 0 .../l10n/java/rules/java/S63.html | 0 .../l10n/java/rules/java/S63.json | 0 .../l10n/java/rules/java/S67.html | 0 .../l10n/java/rules/java/S67.json | 0 .../l10n/java/rules/java/S69.html | 0 .../l10n/java/rules/java/S69.json | 0 .../l10n/java/rules/java/S72.html | 0 .../l10n/java/rules/java/S72.json | 0 .../l10n/java/rules/java/S74.html | 0 .../l10n/java/rules/java/S74.json | 0 .../l10n/java/rules/java/S75.html | 0 .../l10n/java/rules/java/S75.json | 0 .../l10n/java/rules/java/S76.html | 0 .../l10n/java/rules/java/S76.json | 0 .../l10n/java/rules/java/S77.html | 0 .../l10n/java/rules/java/S77.json | 0 .../l10n/java/rules/java/S78.html | 0 .../l10n/java/rules/java/S78.json | 0 .../l10n/java/rules/java/S79.html | 0 .../l10n/java/rules/java/S79.json | 0 .../l10n/java/rules/java/SDMLQ1.html | 0 .../l10n/java/rules/java/SDMLQ1.json | 0 .../files/AvoidConcatenateStringsInLoop.java | 2 +- .../test/files/AvoidFullSQLRequestCheck.java | 48 +-- ...ingSizeCollectionInForEachLoopIgnored.java | 2 +- ...voidGettingSizeCollectionInForLoopBad.java | 2 +- ...oidGettingSizeCollectionInForLoopGood.java | 2 +- ...GettingSizeCollectionInForLoopIgnored.java | 2 +- ...idGettingSizeCollectionInWhileLoopBad.java | 2 +- ...dGettingSizeCollectionInWhileLoopGood.java | 2 +- ...ttingSizeCollectionInWhileLoopIgnored.java | 2 +- .../files/AvoidMultipleIfElseStatement.java | 4 +- .../AvoidMultipleIfElseStatementNoIssue.java | 4 +- .../files/AvoidRegexPatternNotStatic.java | 2 +- .../files/AvoidSQLRequestInLoopCheck.java | 246 +++++++-------- .../AvoidSetConstantInBatchUpdateCheck.java | 264 ++++++++-------- .../AvoidSpringRepositoryCallInLoopCheck.java | 13 +- .../files/AvoidStatementForDMLQueries.java | 3 +- .../files/AvoidUsageOfStaticCollections.java | 2 +- ...FreeResourcesOfAutoCloseableInterface.java | 5 +- .../files/GoodUsageOfStaticCollections.java | 2 +- .../files/GoodWayConcatenateStringsLoop.java | 2 +- .../InitializeBufferWithAppropriateSize.java | 2 +- .../files/OptimizeReadFileExceptionCheck.java | 3 +- .../OptimizeReadFileExceptionCheck2.java | 2 +- .../OptimizeReadFileExceptionCheck3.java | 2 +- .../OptimizeReadFileExceptionCheck4.java | 2 +- .../OptimizeReadFileExceptionCheck5.java | 2 +- ...arilyAssignValuesToVariablesTestCheck.java | 130 ++++---- ...esToVariablesTestCheckWithEmptyReturn.java | 2 +- .../test/files/UseCorrectForLoopCheck.java | 28 +- .../src/test/files/ValidRegexPattern.java | 2 +- .../src/test/files/ValidRegexPattern2.java | 2 +- .../src/test/files/ValidRegexPattern3.java | 2 +- .../cnumr/java/checks/ArrayCopyCheckTest.java | 19 -- .../java/fr/cnumr/java/utils/FilesUtils.java | 93 ------ .../java/JavaCheckRegistrarTest.java | 2 +- .../java/JavaPluginTest.java | 2 +- .../java/JavaRulesDefinitionTest.java | 2 +- .../java/checks/ArrayCopyCheckTest.java | 19 ++ .../AvoidConcatenateStringsInLoopTest.java | 2 +- .../checks/AvoidFullSQLRequestCheckTest.java | 2 +- .../AvoidGettingSizeCollectionInLoopTest.java | 2 +- .../AvoidMultipleIfElseStatementTest.java | 2 +- .../AvoidRegexPatternNotStaticTest.java | 2 +- .../AvoidSQLRequestInLoopCheckTest.java | 2 +- .../AvoidSetConstantInBatchInsertTest.java | 2 +- ...idSpringRepositoryCallInLoopCheckTest.java | 5 +- .../AvoidStatementForDMLQueriesTest.java | 3 +- .../AvoidUsageOfStaticCollectionsTests.java | 2 +- ...oidUsingGlobalVariablesCheckCheckTest.java | 2 +- ...ResourcesOfAutoCloseableInterfaceTest.java | 2 +- .../java/checks/IncrementCheckTest.java | 2 +- ...itializeBufferWithAppropriateSizeTest.java | 2 +- ...oFunctionCallWhenDeclaringForLoopTest.java | 2 +- .../OptimizeReadFileExceptionCheckTest.java | 2 +- ...ecessarilyAssignValuesToVariablesTest.java | 2 +- .../java/checks/UseCorrectLoopCheckTest.java | 2 +- .../java/utils/FilesUtils.java | 93 ++++++ .../java/utils/StringUtilsTest.java | 33 +- php-plugin/pom.xml | 2 +- .../php/checks/AvoidFullSQLRequestCheck.java | 39 --- .../php/PHPPlugin.java | 2 +- .../php/PhpRulesDefinition.java | 6 +- .../php/RulesList.java | 23 +- .../php/checks/AvoidDoubleQuoteCheck.java | 11 +- .../php/checks/AvoidFullSQLRequestCheck.java | 39 +++ .../checks/AvoidSQLRequestInLoopCheck.java | 10 +- ...inallyCheck_NOK_failsAllTryStatements.java | 8 +- .../AvoidUsingGlobalVariablesCheck.java | 12 +- .../php/checks/IncrementCheck.java | 8 +- .../NoFunctionCallWhenDeclaringForLoop.java | 8 +- .../UseOfMethodsForBasicOperations.java | 32 +- .../l10n/php/rules/custom/D2.html | 0 .../l10n/php/rules/custom/D2.json | 0 .../l10n/php/rules/custom/D4.html | 0 .../l10n/php/rules/custom/D4.json | 0 .../l10n/php/rules/custom/S34.html | 0 .../l10n/php/rules/custom/S34.json | 0 .../l10n/php/rules/custom/S66.html | 0 .../l10n/php/rules/custom/S66.json | 0 .../l10n/php/rules/custom/S67.html | 0 .../l10n/php/rules/custom/S67.json | 0 .../l10n/php/rules/custom/S69.html | 0 .../l10n/php/rules/custom/S69.json | 0 .../l10n/php/rules/custom/S72.html | 0 .../l10n/php/rules/custom/S72.json | 0 .../l10n/php/rules/custom/S74.html | 0 .../l10n/php/rules/custom/S74.json | 0 .../checks/AvoidFullSQLRequestCheckTest.java | 15 - .../AvoidSQLRequestInLoopCheckTest.java | 15 - ...allyCheckNOKfailsAllTryStatementsTest.java | 16 - .../php/PhpRulesDefinitionTest.java | 2 +- .../php/checks/AvoidDoubleQuoteCheckTest.java | 6 +- .../checks/AvoidFullSQLRequestCheckTest.java | 15 + .../AvoidSQLRequestInLoopCheckTest.java | 15 + ...allyCheckNOKfailsAllTryStatementsTest.java | 16 + .../AvoidUsingGlobalVariablesCheckTest.java | 14 +- .../php/checks/IncrementCheckTest.java | 14 +- ...oFunctionCallWhenDeclaringForLoopTest.java | 6 +- .../UseOfMethodsForBasicOperationsTest.java | 6 +- python-plugin/pom.xml | 2 +- .../python/PythonPlugin.java | 2 +- .../python/PythonRulesDefinition.java | 6 +- .../python/RulesList.java | 13 +- .../python/checks/AvoidFullSQLRequest.java | 12 +- .../python/checks/AvoidGettersAndSetters.java | 39 +-- .../AvoidGlobalVariableInFunctionCheck.java | 2 +- .../python/checks/AvoidSQLRequestInLoop.java | 21 +- .../checks/AvoidTryCatchFinallyCheck.java | 2 +- .../NoFunctionCallWhenDeclaringForLoop.java | 2 +- .../python/checks/package-info.java | 2 +- .../python/package-info.java | 2 +- .../l10n/python/rules/python/D4.html | 0 .../l10n/python/rules/python/D4.json | 0 .../l10n/python/rules/python/D7.html | 0 .../l10n/python/rules/python/D7.json | 0 .../l10n/python/rules/python/S34.html | 0 .../l10n/python/rules/python/S34.json | 0 .../l10n/python/rules/python/S69.html | 0 .../l10n/python/rules/python/S69.json | 0 .../l10n/python/rules/python/S72.html | 0 .../l10n/python/rules/python/S72.json | 0 .../l10n/python/rules/python/S74.html | 0 .../l10n/python/rules/python/S74.json | 0 .../python/PythonPluginTest.java | 2 +- .../python/PythonRulesDefinitionTest.java | 4 +- .../checks/AvoidFullSQLRequestTest.java | 2 +- .../checks/AvoidGettersAndSettersTest.java | 4 +- ...voidGlobalVariableInFunctionCheckTest.java | 2 +- .../checks/AvoidSQLRequestInLoopTest.java | 2 +- .../checks/AvoidTryCatchFinallyCheckTest.java | 2 +- ...oFunctionCallWhenDeclaringForLoopTest.java | 2 +- 223 files changed, 1667 insertions(+), 1575 deletions(-) rename _TO_CHECK_ecolinter-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/ecolint/rules/eslint/greenit.json (100%) rename _TO_CHECK_ecolinter-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/ecolint/rules/stylelint/greenit.json (100%) rename _TO_CHECK_xml-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/xml/rules/custom/S99.html (100%) rename _TO_CHECK_xml-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/xml/rules/custom/S99.json (100%) delete mode 100644 java-plugin/src/main/java/fr/cnumr/java/checks/ArrayCopyCheck.java delete mode 100644 java-plugin/src/main/java/fr/cnumr/java/checks/AvoidSetConstantInBatchUpdate.java delete mode 100644 java-plugin/src/main/java/fr/cnumr/java/checks/ConstOrLiteralDeclare.java delete mode 100644 java-plugin/src/main/java/fr/cnumr/java/utils/PrinterVisitor.java rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/JavaCheckRegistrar.java (98%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/JavaPlugin.java (97%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/JavaRulesDefinition.java (92%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/RulesList.java (64%) create mode 100644 java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ArrayCopyCheck.java rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidConcatenateStringsInLoop.java (85%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidFullSQLRequest.java (66%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidGettingSizeCollectionInLoop.java (98%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidMultipleIfElseStatement.java (98%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidRegexPatternNotStatic.java (97%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidSQLRequestInLoop.java (76%) create mode 100644 java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchUpdate.java rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidSpringRepositoryCallInLoopCheck.java (87%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidStatementForDMLQueries.java (81%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidUsageOfStaticCollections.java (97%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidUsingGlobalVariablesCheck.java (97%) create mode 100644 java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ConstOrLiteralDeclare.java rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/FreeResourcesOfAutoCloseableInterface.java (92%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/IncrementCheck.java (86%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/InitializeBufferWithAppropriateSize.java (87%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/NoFunctionCallWhenDeclaringForLoop.java (99%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/OptimizeReadFileExceptions.java (98%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/UnnecessarilyAssignValuesToVariables.java (99%) rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/checks/UseCorrectForLoop.java (75%) rename java-plugin/src/main/java/fr/{cnumr/java => greencodeinitiative/java/checks}/package-info.java (95%) rename java-plugin/src/main/java/fr/{cnumr/java/utils => greencodeinitiative/java}/package-info.java (95%) create mode 100644 java-plugin/src/main/java/fr/greencodeinitiative/java/utils/PrinterVisitor.java rename java-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/java/utils/StringUtils.java (68%) rename java-plugin/src/main/java/fr/{cnumr/java/checks => greencodeinitiative/java/utils}/package-info.java (95%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/AMIES.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/AMIES.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/D4.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/D4.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/GRC1.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/GRC1.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/GRPS0027.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/GRPS0027.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/GRSP0028.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/GRSP0028.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/GRSP0032.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/GRSP0032.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/GSCIL.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/GSCIL.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S53.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S53.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S63.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S63.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S67.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S67.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S69.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S69.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S72.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S72.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S74.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S74.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S75.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S75.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S76.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S76.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S77.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S77.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S78.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S78.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S79.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/S79.json (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/SDMLQ1.html (100%) rename java-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/java/rules/java/SDMLQ1.json (100%) delete mode 100644 java-plugin/src/test/java/fr/cnumr/java/checks/ArrayCopyCheckTest.java delete mode 100644 java-plugin/src/test/java/fr/cnumr/java/utils/FilesUtils.java rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/JavaCheckRegistrarTest.java (97%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/JavaPluginTest.java (98%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/JavaRulesDefinitionTest.java (98%) create mode 100644 java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ArrayCopyCheckTest.java rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidConcatenateStringsInLoopTest.java (93%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidFullSQLRequestCheckTest.java (89%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidGettingSizeCollectionInLoopTest.java (97%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidMultipleIfElseStatementTest.java (93%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidRegexPatternNotStaticTest.java (94%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidSQLRequestInLoopCheckTest.java (89%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidSetConstantInBatchInsertTest.java (89%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java (83%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidStatementForDMLQueriesTest.java (89%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidUsageOfStaticCollectionsTests.java (93%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java (89%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java (94%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/IncrementCheckTest.java (88%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/InitializeBufferWithAppropriateSizeTest.java (90%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java (89%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/OptimizeReadFileExceptionCheckTest.java (96%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/UnnecessarilyAssignValuesToVariablesTest.java (94%) rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/checks/UseCorrectLoopCheckTest.java (88%) create mode 100644 java-plugin/src/test/java/fr/greencodeinitiative/java/utils/FilesUtils.java rename java-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/java/utils/StringUtilsTest.java (64%) delete mode 100644 php-plugin/src/main/java/fr/cnumr/php/checks/AvoidFullSQLRequestCheck.java rename php-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/php/PHPPlugin.java (96%) rename php-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/php/PhpRulesDefinition.java (92%) rename php-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/php/RulesList.java (65%) rename php-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/php/checks/AvoidDoubleQuoteCheck.java (91%) create mode 100644 php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidFullSQLRequestCheck.java rename php-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/php/checks/AvoidSQLRequestInLoopCheck.java (92%) rename php-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/php/checks/AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.java (95%) rename php-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/php/checks/AvoidUsingGlobalVariablesCheck.java (84%) rename php-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/php/checks/IncrementCheck.java (97%) rename php-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/php/checks/NoFunctionCallWhenDeclaringForLoop.java (97%) rename php-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/php/checks/UseOfMethodsForBasicOperations.java (82%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/D2.html (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/D2.json (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/D4.html (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/D4.json (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/S34.html (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/S34.json (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/S66.html (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/S66.json (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/S67.html (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/S67.json (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/S69.html (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/S69.json (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/S72.html (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/S72.json (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/S74.html (100%) rename php-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/php/rules/custom/S74.json (100%) delete mode 100644 php-plugin/src/test/java/fr/cnumr/php/checks/AvoidFullSQLRequestCheckTest.java delete mode 100644 php-plugin/src/test/java/fr/cnumr/php/checks/AvoidSQLRequestInLoopCheckTest.java delete mode 100644 php-plugin/src/test/java/fr/cnumr/php/checks/AvoidTryCatchFinallyCheckNOKfailsAllTryStatementsTest.java rename php-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/php/PhpRulesDefinitionTest.java (94%) rename php-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/php/checks/AvoidDoubleQuoteCheckTest.java (90%) create mode 100644 php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidFullSQLRequestCheckTest.java create mode 100644 php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidSQLRequestInLoopCheckTest.java create mode 100644 php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidTryCatchFinallyCheckNOKfailsAllTryStatementsTest.java rename php-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/php/checks/AvoidUsingGlobalVariablesCheckTest.java (81%) rename php-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/php/checks/IncrementCheckTest.java (83%) rename php-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/php/checks/NoFunctionCallWhenDeclaringForLoopTest.java (90%) rename php-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/php/checks/UseOfMethodsForBasicOperationsTest.java (90%) rename python-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/python/PythonPlugin.java (96%) rename python-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/python/PythonRulesDefinition.java (91%) rename python-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/python/RulesList.java (80%) rename python-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/python/checks/AvoidFullSQLRequest.java (89%) rename python-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/python/checks/AvoidGettersAndSetters.java (67%) rename python-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/python/checks/AvoidGlobalVariableInFunctionCheck.java (99%) rename python-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/python/checks/AvoidSQLRequestInLoop.java (81%) rename python-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/python/checks/AvoidTryCatchFinallyCheck.java (95%) rename python-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/python/checks/NoFunctionCallWhenDeclaringForLoop.java (95%) rename python-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/python/checks/package-info.java (95%) rename python-plugin/src/main/java/fr/{cnumr => greencodeinitiative}/python/package-info.java (96%) rename python-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/python/rules/python/D4.html (100%) rename python-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/python/rules/python/D4.json (100%) rename python-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/python/rules/python/D7.html (100%) rename python-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/python/rules/python/D7.json (100%) rename python-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/python/rules/python/S34.html (100%) rename python-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/python/rules/python/S34.json (100%) rename python-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/python/rules/python/S69.html (100%) rename python-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/python/rules/python/S69.json (100%) rename python-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/python/rules/python/S72.html (100%) rename python-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/python/rules/python/S72.json (100%) rename python-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/python/rules/python/S74.html (100%) rename python-plugin/src/main/resources/fr/{cnumr => greencodeinitiative}/l10n/python/rules/python/S74.json (100%) rename python-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/python/PythonPluginTest.java (97%) rename python-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/python/PythonRulesDefinitionTest.java (92%) rename python-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/python/checks/AvoidFullSQLRequestTest.java (85%) rename python-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/python/checks/AvoidGettersAndSettersTest.java (86%) rename python-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/python/checks/AvoidGlobalVariableInFunctionCheckTest.java (87%) rename python-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/python/checks/AvoidSQLRequestInLoopTest.java (90%) rename python-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/python/checks/AvoidTryCatchFinallyCheckTest.java (86%) rename python-plugin/src/test/java/fr/{cnumr => greencodeinitiative}/python/checks/NoFunctionCallWhenDeclaringForLoopTest.java (87%) diff --git a/INSTALL.md b/INSTALL.md index bc2e72a3b..e213ca643 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -2,20 +2,20 @@ Installation documentation ========================== - [Installation documentation](#installation-documentation) - - [Requirements](#requirements) - - [Project structure](#project-structure) - - [Howto build the SonarQube ecoCode plugins](#howto-build-the-sonarqube-ecocode-plugins) - - [Requirements](#requirements-1) - - [Build the code](#build-the-code) - - [Howto install SonarQube dev environment](#howto-install-sonarqube-dev-environment) - - [Requirements](#requirements-2) - - [Start SonarQube (if first time)](#start-sonarqube-if-first-time) - - [Reinstall SonarQube (if needed)](#reinstall-sonarqube-if-needed) - - [Howto install Plugin Ecocode](#howto-install-plugin-ecocode) - - [Howto start or stop service (already installed)](#howto-start-or-stop-service-already-installed) - - [Howto create a release](#howto-create-a-release) - - [Howto debug a rule (with logs)](#howto-debug-a-rule-with-logs) - - [Links](#links) + - [Requirements](#requirements) + - [Project structure](#project-structure) + - [Howto build the SonarQube ecoCode plugins](#howto-build-the-sonarqube-ecocode-plugins) + - [Requirements](#requirements-1) + - [Build the code](#build-the-code) + - [Howto install SonarQube dev environment](#howto-install-sonarqube-dev-environment) + - [Requirements](#requirements-2) + - [Start SonarQube (if first time)](#start-sonarqube-if-first-time) + - [Reinstall SonarQube (if needed)](#reinstall-sonarqube-if-needed) + - [Howto install Plugin Ecocode](#howto-install-plugin-ecocode) + - [Howto start or stop service (already installed)](#howto-start-or-stop-service-already-installed) + - [Howto create a release](#howto-create-a-release) + - [Howto debug a rule (with logs)](#howto-debug-a-rule-with-logs) + - [Links](#links) Requirements ------------ @@ -174,7 +174,8 @@ Howto create a release 2 new commits and a tag) 3. if all is ok, execute `tool_release_2_branch.sh` to create and push a new branch with that release and SNAPSHOT 4. if all is ok, on github, create a PR based on this new branch to `main` branch -5. wait that automatic check on the new branch are OK, then check modifications and finally merge it with `Create a merge commit` option +5. wait that automatic check on the new branch are OK, then check modifications and finally merge it + with `Create a merge commit` option 6. if PR merge is OK, then delete the branch as mentionned when PR merged 7. wait that automatic check on the `main` branch are OK, and then if all is ok, upgrade your local source code from remote, and go to `main` branch and finally check locally if the @@ -187,10 +188,11 @@ Howto create a release Howto debug a rule (with logs) ------------------------------ -1. Add logs like in `java-plugin/src/main/java/fr/cnumr/java/checks/OptimizeReadFileExceptions` class file +1. Add logs like in `java-plugin/src/main/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptions` class file 2. Build plugin JARs with `tool_build.sh` 3. Launch local Sonar with `tool_docker_init.sh` -4. Launch a sonar scanner on an exemple project with `mvn verify` command (only the first time), followed by `mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar -Dsonar.login=***** -Dsonar.password=***** -X` +4. Launch a sonar scanner on an exemple project with `mvn verify` command (only the first time), followed + by `mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar -Dsonar.login=***** -Dsonar.password=***** -X` 5. logs will appear in console (debug logs will appear if `-X` option is given like above) Links diff --git a/_TO_CHECK_ecolinter-plugin/pom.xml b/_TO_CHECK_ecolinter-plugin/pom.xml index 7ead6c61b..5d077863a 100644 --- a/_TO_CHECK_ecolinter-plugin/pom.xml +++ b/_TO_CHECK_ecolinter-plugin/pom.xml @@ -13,7 +13,7 @@ ecocode-ecolinter-plugin ecoCode EcoLinter Sonar Plugin sonar-plugin - + org.sonarsource.sonarqube @@ -54,7 +54,7 @@ ${project.artifactId} ${project.name} - fr.cnumr.ecolinter.MyEcoLintRulesPlugin + fr.greencodeinitiative.ecolinter.MyEcoLintRulesPlugin true ${sonarqube.version} diff --git a/_TO_CHECK_ecolinter-plugin/src/main/java/fr/cnumr/ecolinter/MyEcoLintRulesPlugin.java b/_TO_CHECK_ecolinter-plugin/src/main/java/fr/cnumr/ecolinter/MyEcoLintRulesPlugin.java index 7eaff0914..14f1456c1 100644 --- a/_TO_CHECK_ecolinter-plugin/src/main/java/fr/cnumr/ecolinter/MyEcoLintRulesPlugin.java +++ b/_TO_CHECK_ecolinter-plugin/src/main/java/fr/cnumr/ecolinter/MyEcoLintRulesPlugin.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.ecolinter; +package fr.greencodeinitiative.ecolinter; import org.sonar.api.Plugin; diff --git a/_TO_CHECK_ecolinter-plugin/src/main/java/fr/cnumr/ecolinter/MyEslintRulesDefinition.java b/_TO_CHECK_ecolinter-plugin/src/main/java/fr/cnumr/ecolinter/MyEslintRulesDefinition.java index 225de14b5..c69ded960 100644 --- a/_TO_CHECK_ecolinter-plugin/src/main/java/fr/cnumr/ecolinter/MyEslintRulesDefinition.java +++ b/_TO_CHECK_ecolinter-plugin/src/main/java/fr/cnumr/ecolinter/MyEslintRulesDefinition.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.ecolinter; +package fr.greencodeinitiative.ecolinter; import org.sonar.api.server.rule.RulesDefinition; import org.sonarsource.analyzer.commons.ExternalRuleLoader; @@ -29,7 +29,7 @@ public class MyEslintRulesDefinition implements RulesDefinition { // don't change that because the path is hard coded in CheckVerifier - private static final String RESOURCE_BASE_PATH = "fr/cnumr/l10n/ecolint/rules/eslint/"; + private static final String RESOURCE_BASE_PATH = "fr/greencodeinitiative/l10n/ecolint/rules/eslint/"; public static final String LINTER_NAME = "ESLint"; diff --git a/_TO_CHECK_ecolinter-plugin/src/main/java/fr/cnumr/ecolinter/MyStylelintRulesDefinition.java b/_TO_CHECK_ecolinter-plugin/src/main/java/fr/cnumr/ecolinter/MyStylelintRulesDefinition.java index 2ccb9a5d4..f68f72ffc 100644 --- a/_TO_CHECK_ecolinter-plugin/src/main/java/fr/cnumr/ecolinter/MyStylelintRulesDefinition.java +++ b/_TO_CHECK_ecolinter-plugin/src/main/java/fr/cnumr/ecolinter/MyStylelintRulesDefinition.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.ecolinter; +package fr.greencodeinitiative.ecolinter; import org.sonar.api.server.rule.RulesDefinition; import org.sonarsource.analyzer.commons.ExternalRuleLoader; @@ -29,7 +29,7 @@ public class MyStylelintRulesDefinition implements RulesDefinition { // don't change that because the path is hard coded in CheckVerifier - private static final String RESOURCE_BASE_PATH = "fr/cnumr/l10n/ecolint/rules/stylelint/"; + private static final String RESOURCE_BASE_PATH = "fr/greencodeinitiative/l10n/ecolint/rules/stylelint/"; public static final String LINTER_NAME = "STYLELINT"; diff --git a/_TO_CHECK_ecolinter-plugin/src/main/resources/fr/cnumr/l10n/ecolint/rules/eslint/greenit.json b/_TO_CHECK_ecolinter-plugin/src/main/resources/fr/greencodeinitiative/l10n/ecolint/rules/eslint/greenit.json similarity index 100% rename from _TO_CHECK_ecolinter-plugin/src/main/resources/fr/cnumr/l10n/ecolint/rules/eslint/greenit.json rename to _TO_CHECK_ecolinter-plugin/src/main/resources/fr/greencodeinitiative/l10n/ecolint/rules/eslint/greenit.json diff --git a/_TO_CHECK_ecolinter-plugin/src/main/resources/fr/cnumr/l10n/ecolint/rules/stylelint/greenit.json b/_TO_CHECK_ecolinter-plugin/src/main/resources/fr/greencodeinitiative/l10n/ecolint/rules/stylelint/greenit.json similarity index 100% rename from _TO_CHECK_ecolinter-plugin/src/main/resources/fr/cnumr/l10n/ecolint/rules/stylelint/greenit.json rename to _TO_CHECK_ecolinter-plugin/src/main/resources/fr/greencodeinitiative/l10n/ecolint/rules/stylelint/greenit.json diff --git a/_TO_CHECK_ecolinter-plugin/src/test/java/fr/cnumr/ecolinter/MyEcoLintRulesPluginTest.java b/_TO_CHECK_ecolinter-plugin/src/test/java/fr/cnumr/ecolinter/MyEcoLintRulesPluginTest.java index 07fc74aae..7278570cb 100644 --- a/_TO_CHECK_ecolinter-plugin/src/test/java/fr/cnumr/ecolinter/MyEcoLintRulesPluginTest.java +++ b/_TO_CHECK_ecolinter-plugin/src/test/java/fr/cnumr/ecolinter/MyEcoLintRulesPluginTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.ecolinter; +package fr.greencodeinitiative.ecolinter; import org.junit.jupiter.api.Test; import org.sonar.api.Plugin; diff --git a/_TO_CHECK_ecolinter-plugin/src/test/java/fr/cnumr/ecolinter/MyEslintRulesDefinitionTest.java b/_TO_CHECK_ecolinter-plugin/src/test/java/fr/cnumr/ecolinter/MyEslintRulesDefinitionTest.java index af7d971f7..bd8d1ce68 100644 --- a/_TO_CHECK_ecolinter-plugin/src/test/java/fr/cnumr/ecolinter/MyEslintRulesDefinitionTest.java +++ b/_TO_CHECK_ecolinter-plugin/src/test/java/fr/cnumr/ecolinter/MyEslintRulesDefinitionTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.ecolinter; +package fr.greencodeinitiative.ecolinter; import org.junit.jupiter.api.Test; import org.sonar.api.server.rule.RulesDefinition; diff --git a/_TO_CHECK_ecolinter-plugin/src/test/java/fr/cnumr/ecolinter/MyStylelintRulesDefinitionTest.java b/_TO_CHECK_ecolinter-plugin/src/test/java/fr/cnumr/ecolinter/MyStylelintRulesDefinitionTest.java index 339ac65f2..e58a4a644 100644 --- a/_TO_CHECK_ecolinter-plugin/src/test/java/fr/cnumr/ecolinter/MyStylelintRulesDefinitionTest.java +++ b/_TO_CHECK_ecolinter-plugin/src/test/java/fr/cnumr/ecolinter/MyStylelintRulesDefinitionTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.ecolinter; +package fr.greencodeinitiative.ecolinter; import org.junit.jupiter.api.Test; import org.sonar.api.server.rule.RulesDefinition; diff --git a/_TO_CHECK_sonarqube-plugin-greenit/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidSQLRequestInLoopCheckTest.java b/_TO_CHECK_sonarqube-plugin-greenit/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidSQLRequestInLoopCheckTest.java index dc5a47206..9774591ea 100644 --- a/_TO_CHECK_sonarqube-plugin-greenit/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidSQLRequestInLoopCheckTest.java +++ b/_TO_CHECK_sonarqube-plugin-greenit/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidSQLRequestInLoopCheckTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; import org.junit.Test; import org.sonar.python.checks.utils.PythonCheckVerifier; diff --git a/_TO_CHECK_xml-plugin/pom.xml b/_TO_CHECK_xml-plugin/pom.xml index 12186a188..8c060c19a 100644 --- a/_TO_CHECK_xml-plugin/pom.xml +++ b/_TO_CHECK_xml-plugin/pom.xml @@ -1,7 +1,8 @@ - + 4.0.0 - + io.ecocode ecocode-parent @@ -10,9 +11,9 @@ ecocode-xml-plugin sonar-plugin - + ecoCode XML Sonar Plugin - + org.sonarsource.sonarqube @@ -40,7 +41,7 @@ org.sonarsource.analyzer-commons test-sonar-xml-parsing - 1.25.0.1003 + 1.25.0.1003 test @@ -55,7 +56,7 @@ ${project.artifactId} ${project.name} - fr.cnumr.xml.XMLCustomRulesPlugin + fr.greencodeinitiative.xml.XMLCustomRulesPlugin true 6.7 xml diff --git a/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/MyXmlRules.java b/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/MyXmlRules.java index f0265c021..85e33e82a 100644 --- a/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/MyXmlRules.java +++ b/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/MyXmlRules.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.xml; +package fr.greencodeinitiative.xml; import java.util.Collections; import java.util.Set; @@ -25,31 +25,31 @@ import org.sonar.api.server.rule.RulesDefinition; import org.sonarsource.analyzer.commons.RuleMetadataLoader; -import fr.cnumr.xml.checks.CheckList; +import fr.greencodeinitiative.xml.checks.CheckList; /** * Extension point to define a PHP rule repository. */ public class MyXmlRules implements RulesDefinition { - public static final String LANGUAGE = "xml"; - public static final String NAME = "Collectif Conception Numérique Responsable"; - public static final String RESOURCE_BASE_PATH = "fr/cnumr/l10n/xml/rules/custom"; - public static final String REPOSITORY_KEY = "cnumr-xml"; - private static final Set RULE_TEMPLATES_KEY = Collections.emptySet(); + public static final String LANGUAGE = "xml"; + public static final String NAME = "Collectif Conception Numérique Responsable"; + public static final String RESOURCE_BASE_PATH = "fr/greencodeinitiative/l10n/xml/rules/custom"; + public static final String REPOSITORY_KEY = "greencodeinitiative-xml"; + private static final Set RULE_TEMPLATES_KEY = Collections.emptySet(); - @Override - public void define(Context context) { - NewRepository repository = context.createRepository(REPOSITORY_KEY, LANGUAGE).setName(NAME); + @Override + public void define(Context context) { + NewRepository repository = context.createRepository(REPOSITORY_KEY, LANGUAGE).setName(NAME); - RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH); + RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH); - // add the new checks - ruleMetadataLoader.addRulesByAnnotatedClass(repository, CheckList.getCheckClasses()); + // add the new checks + ruleMetadataLoader.addRulesByAnnotatedClass(repository, CheckList.getCheckClasses()); // repository.rule("XPathCheck").setTemplate(true); // repository.rule("S3417").setTemplate(true); - repository.done(); - } + repository.done(); + } } diff --git a/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/XMLCustomRulesPlugin.java b/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/XMLCustomRulesPlugin.java index b3cd9d0d3..29a8ccecb 100644 --- a/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/XMLCustomRulesPlugin.java +++ b/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/XMLCustomRulesPlugin.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.xml; +package fr.greencodeinitiative.xml; import org.sonar.api.Plugin; @@ -26,8 +26,8 @@ */ public class XMLCustomRulesPlugin implements Plugin { - @Override - public void define(Context context) { - context.addExtension(MyXmlRules.class); + @Override + public void define(Context context) { + context.addExtension(MyXmlRules.class); } } diff --git a/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/checks/CheckList.java b/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/checks/CheckList.java index 9f21859a4..d07a80f2d 100644 --- a/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/checks/CheckList.java +++ b/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/checks/CheckList.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.xml.checks; +package fr.greencodeinitiative.xml.checks; import java.util.Arrays; import java.util.List; diff --git a/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/checks/UseUnoptimizedVectorImage.java b/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/checks/UseUnoptimizedVectorImage.java index 01aa9360f..cc63d9b44 100644 --- a/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/checks/UseUnoptimizedVectorImage.java +++ b/_TO_CHECK_xml-plugin/src/main/java/fr/cnumr/xml/checks/UseUnoptimizedVectorImage.java @@ -1,4 +1,4 @@ -package fr.cnumr.xml.checks; +package fr.greencodeinitiative.xml.checks; import java.util.Collections; @@ -7,7 +7,7 @@ import org.sonarsource.analyzer.commons.xml.XmlFile; import org.sonarsource.analyzer.commons.xml.checks.SonarXmlCheck; -@Rule(key = "S99", name = "Developpement", description = UseUnoptimizedVectorImage.MESSAGERULE, priority = Priority.MINOR, tags = { "bug" }) +@Rule(key = "S99", name = "Developpement", description = UseUnoptimizedVectorImage.MESSAGERULE, priority = Priority.MINOR, tags = {"bug"}) public class UseUnoptimizedVectorImage extends SonarXmlCheck { protected static final String MESSAGERULE = "Avoid the use of unoptimized vector images"; diff --git a/_TO_CHECK_xml-plugin/src/main/resources/fr/cnumr/l10n/xml/rules/custom/S99.html b/_TO_CHECK_xml-plugin/src/main/resources/fr/greencodeinitiative/l10n/xml/rules/custom/S99.html similarity index 100% rename from _TO_CHECK_xml-plugin/src/main/resources/fr/cnumr/l10n/xml/rules/custom/S99.html rename to _TO_CHECK_xml-plugin/src/main/resources/fr/greencodeinitiative/l10n/xml/rules/custom/S99.html diff --git a/_TO_CHECK_xml-plugin/src/main/resources/fr/cnumr/l10n/xml/rules/custom/S99.json b/_TO_CHECK_xml-plugin/src/main/resources/fr/greencodeinitiative/l10n/xml/rules/custom/S99.json similarity index 100% rename from _TO_CHECK_xml-plugin/src/main/resources/fr/cnumr/l10n/xml/rules/custom/S99.json rename to _TO_CHECK_xml-plugin/src/main/resources/fr/greencodeinitiative/l10n/xml/rules/custom/S99.json diff --git a/_TO_CHECK_xml-plugin/src/test/java/fr/cnumr/xml/MyXmlRulesTest.java b/_TO_CHECK_xml-plugin/src/test/java/fr/cnumr/xml/MyXmlRulesTest.java index 49405b855..ea1cc1f13 100644 --- a/_TO_CHECK_xml-plugin/src/test/java/fr/cnumr/xml/MyXmlRulesTest.java +++ b/_TO_CHECK_xml-plugin/src/test/java/fr/cnumr/xml/MyXmlRulesTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.xml; +package fr.greencodeinitiative.xml; import static org.junit.Assert.assertEquals; @@ -7,7 +7,7 @@ public class MyXmlRulesTest { - private int NumberOfRuleInRepository = 6; + private final int NumberOfRuleInRepository = 6; @Test public void rules() { diff --git a/_TO_CHECK_xml-plugin/src/test/java/fr/cnumr/xml/checks/UseUnoptimizedVectorImageTest.java b/_TO_CHECK_xml-plugin/src/test/java/fr/cnumr/xml/checks/UseUnoptimizedVectorImageTest.java index 36a5826d3..067b8a4f4 100644 --- a/_TO_CHECK_xml-plugin/src/test/java/fr/cnumr/xml/checks/UseUnoptimizedVectorImageTest.java +++ b/_TO_CHECK_xml-plugin/src/test/java/fr/cnumr/xml/checks/UseUnoptimizedVectorImageTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.xml.checks; +package fr.greencodeinitiative.xml.checks; import java.io.IOException; diff --git a/java-plugin/pom.xml b/java-plugin/pom.xml index 83d58f730..8bb86de73 100644 --- a/java-plugin/pom.xml +++ b/java-plugin/pom.xml @@ -85,7 +85,7 @@ ${project.artifactId} ${project.name} - fr.cnumr.java.JavaPlugin + fr.greencodeinitiative.java.JavaPlugin true ${sonarqube.version} diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/ArrayCopyCheck.java b/java-plugin/src/main/java/fr/cnumr/java/checks/ArrayCopyCheck.java deleted file mode 100644 index 9ce3cbb7c..000000000 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/ArrayCopyCheck.java +++ /dev/null @@ -1,284 +0,0 @@ -package fr.cnumr.java.checks; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.function.Predicate; -import java.util.stream.Collectors; - -import org.sonar.check.Priority; -import org.sonar.check.Rule; -import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; -import org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree; -import org.sonar.plugins.java.api.tree.AssignmentExpressionTree; -import org.sonar.plugins.java.api.tree.BlockTree; -import org.sonar.plugins.java.api.tree.CatchTree; -import org.sonar.plugins.java.api.tree.DoWhileStatementTree; -import org.sonar.plugins.java.api.tree.ExpressionStatementTree; -import org.sonar.plugins.java.api.tree.ExpressionTree; -import org.sonar.plugins.java.api.tree.ForEachStatement; -import org.sonar.plugins.java.api.tree.ForStatementTree; -import org.sonar.plugins.java.api.tree.IdentifierTree; -import org.sonar.plugins.java.api.tree.IfStatementTree; -import org.sonar.plugins.java.api.tree.StatementTree; -import org.sonar.plugins.java.api.tree.Tree; -import org.sonar.plugins.java.api.tree.Tree.Kind; -import org.sonar.plugins.java.api.tree.TryStatementTree; -import org.sonar.plugins.java.api.tree.VariableTree; -import org.sonar.plugins.java.api.tree.WhileStatementTree; - -/** - * Array Copy Check - * @formatter:off - * @author Aubay - */ -@Rule(key = "GRPS0027", - name = "Developpement", - description = ArrayCopyCheck.MESSAGERULE, - priority = Priority.MINOR, - tags = { "bug" }) -public class ArrayCopyCheck extends IssuableSubscriptionVisitor { - - //@formatter:on - protected static final String MESSAGERULE = "Use System.arraycopy to copy arrays"; - - @Override - public List nodesToVisit() { - return Arrays.asList(Kind.FOR_STATEMENT, Kind.WHILE_STATEMENT, Kind.DO_STATEMENT, Kind.FOR_EACH_STATEMENT); - } - - /** - * Check a node. Report issue when found. - */ - @Override - public void visitNode(final Tree tree) { - // Determine blocks to be analyzed - final List blocs = getBlocsOfCode(tree); - - // Analyze blocks - for (final Bloc bloc : blocs) { - if (bloc.isForeach()) { - handleForEachAssignments(tree, bloc); - } - handleAssignments(tree, bloc); - } - } - - /** - * Handle for-each assignments controls. - * - * @param tree - * @param bloc - */ - private void handleForEachAssignments(final Tree tree, final Bloc bloc) { - for (final AssignmentExpressionTree assignment : extractAssignments(tree, bloc)) { - final ExpressionTree destination = assignment.variable(); - final ExpressionTree source = assignment.expression(); - if (isArray(destination) && isVariable(source)) { - final String destinationIdentifier = getArrayIdentifier(destination); - final String sourceIdentifier = ((IdentifierTree) source).name(); - if (bloc.getValue().equals(sourceIdentifier) && !bloc.getIterable().equals(destinationIdentifier)) { - reportIssue(tree, MESSAGERULE); - } - } - } - } - - /** - * Handle assignments controls. - * - * @param tree - * @param bloc - */ - private void handleAssignments(final Tree tree, final Bloc bloc) { - for (final AssignmentExpressionTree assignment : extractAssignments(tree, bloc)) { - final ExpressionTree destVariable = assignment.variable(); - final ExpressionTree srcEspression = assignment.expression(); - if (isArray(destVariable) && isArray(srcEspression)) { - final String destArray = getArrayIdentifier(destVariable); - final String srcArray = getArrayIdentifier(srcEspression); - if (destArray != null && !destArray.equals(srcArray)) { - reportIssue(tree, MESSAGERULE); - } - } - } - } - - /** - * Extract variable's name of array - * - * @param expression of Array - * @return Array's name - */ - private String getArrayIdentifier(final ExpressionTree expression) { - if (expression instanceof ArrayAccessExpressionTree) { - final ExpressionTree identifier = ((ArrayAccessExpressionTree) expression).expression(); - if (identifier instanceof IdentifierTree) { - return ((IdentifierTree) identifier).identifierToken().text(); - } - } - return null; - } - - /** - * Verify if expression is an Array - * - * @param expression - * @return true if instance of ArrayAccessExpressionTree, false else - */ - private boolean isArray(final ExpressionTree expression) { - return expression instanceof ArrayAccessExpressionTree; - } - - /** - * Verify if expression is an variable - * - * @param source - * @return - */ - private boolean isVariable(final ExpressionTree source) { - return source instanceof IdentifierTree; - } - - /** - * Extract nested blocks of code - * - * @param tree - * @return - */ - private List getBlocsOfCode(final Tree tree) { - final List blocs = new ArrayList<>(); - if (tree instanceof ForStatementTree) { - ForStatementTree castedForTree = (ForStatementTree) tree; - addBloc(blocs, castedForTree.statement()); - } else if (tree instanceof ForEachStatement) { - ForEachStatement castedForEachTree = (ForEachStatement) tree; - addForEachBloc(blocs, castedForEachTree.statement(), castedForEachTree.variable(), - castedForEachTree.expression()); - } else if (tree instanceof WhileStatementTree) { - WhileStatementTree castedWhileTree = (WhileStatementTree) tree; - addBloc(blocs, castedWhileTree.statement()); - } else if (tree instanceof DoWhileStatementTree) { - DoWhileStatementTree castedDoWhileTree = (DoWhileStatementTree) tree; - addBloc(blocs, castedDoWhileTree.statement()); - } else if (tree instanceof IfStatementTree) { - IfStatementTree castedIfTree = (IfStatementTree) tree; - addBloc(blocs, castedIfTree.thenStatement()); - addBloc(blocs, castedIfTree.elseStatement()); - } else if (tree instanceof TryStatementTree) { - final TryStatementTree tryTree = (TryStatementTree) tree; - addBloc(blocs, tryTree.block()); - addBloc(blocs, extractCatchBlocks(tryTree)); - addBloc(blocs, tryTree.finallyBlock()); - } - return blocs; - } - - /** - * Assignments extraction from block of code. - * - * @param tree - * - * @param block - * @return - */ - private List extractAssignments(final Tree tree, final Bloc bloc) { - final BlockTree block = bloc.getBlockTree(); - - // Prepare useful predicates - final Predicate blocksPredicate = statement -> statement.is(Kind.IF_STATEMENT) - || statement.is(Kind.TRY_STATEMENT); - final Predicate assignmentPredicate = statement -> statement.is(Kind.EXPRESSION_STATEMENT) - && ((ExpressionStatementTree) statement).expression().is(Kind.ASSIGNMENT); - - // Filter expressions to find assignments - final List result = block.body().stream().filter(assignmentPredicate) - .map(assign -> (AssignmentExpressionTree) ((ExpressionStatementTree) assign).expression()) - .collect(Collectors.toList()); - - // Recursive loop for nested blocks, add nested assignments to results - final List ifStatements = block.body().stream().filter(blocksPredicate) - .collect(Collectors.toList()); - for (final StatementTree ifstatement : ifStatements) { - final List blocs = getBlocsOfCode(ifstatement); - for (final Bloc b : blocs) { - result.addAll(extractAssignments(tree, b)); - } - } - return result; - } - - /** - * Extract all blocks of code from try/catch statement - * - * @param tryTree - * @return Array of StatementTree - */ - private BlockTree[] extractCatchBlocks(final TryStatementTree tryTree) { - final List catches = tryTree.catches(); - return catches.stream().map(CatchTree::block).collect(Collectors.toList()).toArray(new BlockTree[0]); - } - - /** - * Add a Bloc in list after type checking - * - * @param blocs - * @param statements - */ - private void addBloc(final List blocs, final StatementTree... statements) { - for (final StatementTree statement : statements) { - if (statement instanceof BlockTree) { - blocs.add(new Bloc((BlockTree) statement)); - } - } - } - - /** - * Add a Bloc in list after type checking - * - * @param blocs - * @param statement - * @param variable - * @param expression - */ - private void addForEachBloc(final List blocs, final StatementTree statement, final VariableTree variable, - final ExpressionTree expression) { - if (statement instanceof BlockTree && expression instanceof IdentifierTree) { - blocs.add(new Bloc((BlockTree) statement, ((IdentifierTree) expression).identifierToken().text(), - variable.simpleName().identifierToken().text())); - } - } - - private static class Bloc { - private final BlockTree blockTree; - private String iterable; - private String value; - - public Bloc(final BlockTree blockTree, final String iterable, final String value) { - this.blockTree = blockTree; - this.iterable = iterable; - this.value = value; - } - - public boolean isForeach() { - return iterable != null && value != null; - } - - public Bloc(final BlockTree blockTree) { - this.blockTree = blockTree; - } - - public BlockTree getBlockTree() { - return blockTree; - } - - public String getIterable() { - return iterable; - } - - public String getValue() { - return value; - } - - } -} diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidSetConstantInBatchUpdate.java b/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidSetConstantInBatchUpdate.java deleted file mode 100644 index b9691e7a2..000000000 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidSetConstantInBatchUpdate.java +++ /dev/null @@ -1,75 +0,0 @@ -package fr.cnumr.java.checks; - -import static fr.cnumr.java.checks.ConstOrLiteralDeclare.isLiteral; -import static java.util.Arrays.asList; -import static org.sonar.plugins.java.api.semantic.Type.Primitives.INT; -import static org.sonar.plugins.java.api.tree.Tree.Kind.MEMBER_SELECT; -import static org.sonar.plugins.java.api.tree.Tree.Kind.METHOD_INVOCATION; - -import java.sql.PreparedStatement; -import java.util.List; -import java.util.stream.Stream; - -import org.sonar.check.Priority; -import org.sonar.check.Rule; -import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; -import org.sonar.plugins.java.api.semantic.MethodMatchers; -import org.sonar.plugins.java.api.tree.BaseTreeVisitor; -import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree; -import org.sonar.plugins.java.api.tree.MethodInvocationTree; -import org.sonar.plugins.java.api.tree.Tree; -import org.sonar.plugins.java.api.tree.Tree.Kind; - -@Rule(key = "S78", name = "Developpement", - description = AvoidSetConstantInBatchUpdate.MESSAGERULE, - priority = Priority.MINOR, - tags = { "bug" }) - -public class AvoidSetConstantInBatchUpdate extends IssuableSubscriptionVisitor { - - protected static final String MESSAGERULE = "Avoid setting constants in batch update"; - private final AvoidSetConstantInBatchUpdateVisitor visitorInFile = new AvoidSetConstantInBatchUpdateVisitor(); - - @Override - public List nodesToVisit() { - return asList( - Tree.Kind.FOR_EACH_STATEMENT, - Tree.Kind.FOR_STATEMENT, - Tree.Kind.WHILE_STATEMENT, - Tree.Kind.DO_STATEMENT); - } - - @Override - public void visitNode(Tree tree) { - tree.accept(visitorInFile); - } - - private class AvoidSetConstantInBatchUpdateVisitor extends BaseTreeVisitor { - - private final MethodMatchers setters = MethodMatchers.create().ofSubTypes(PreparedStatement.class.getName()) - .names("setBoolean", "setByte", "setShort", "setInt", "setLong", "setFloat", "setDouble", - "setBigDecimal", "setString") - .addParametersMatcher(args-> args.size() == 2 && args.get(0).isPrimitive(INT)).build(); - - @Override - public void visitMethodInvocation(MethodInvocationTree tree) { - if (setters.matches(tree) && isConstant(tree.arguments().get(1))) { - reportIssue(tree, MESSAGERULE); - } else { - super.visitMethodInvocation(tree); - } - } - } - - private static final boolean isConstant(Tree arg) { - - if (arg.is(METHOD_INVOCATION)) { - MethodInvocationTree m = (MethodInvocationTree) arg; - return Stream.of(ConstOrLiteralDeclare.values()).anyMatch(o-> o.isLiteralDeclare(m)); - } else if (arg.is(MEMBER_SELECT)) { - MemberSelectExpressionTree m = (MemberSelectExpressionTree) arg; - return Stream.of(ConstOrLiteralDeclare.values()).anyMatch(o-> o.isPublicMember(m)); - } - return isLiteral(arg); - } -} \ No newline at end of file diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/ConstOrLiteralDeclare.java b/java-plugin/src/main/java/fr/cnumr/java/checks/ConstOrLiteralDeclare.java deleted file mode 100644 index f3bc1f5df..000000000 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/ConstOrLiteralDeclare.java +++ /dev/null @@ -1,198 +0,0 @@ -package fr.cnumr.java.checks; - -import static org.sonar.plugins.java.api.semantic.MethodMatchers.create; -import static org.sonar.plugins.java.api.tree.Tree.Kind.BOOLEAN_LITERAL; -import static org.sonar.plugins.java.api.tree.Tree.Kind.CHAR_LITERAL; -import static org.sonar.plugins.java.api.tree.Tree.Kind.DOUBLE_LITERAL; -import static org.sonar.plugins.java.api.tree.Tree.Kind.FLOAT_LITERAL; -import static org.sonar.plugins.java.api.tree.Tree.Kind.INT_LITERAL; -import static org.sonar.plugins.java.api.tree.Tree.Kind.LONG_LITERAL; -import static org.sonar.plugins.java.api.tree.Tree.Kind.STRING_LITERAL; -import static org.sonar.plugins.java.api.tree.Tree.Kind.TYPE_CAST; - -import java.math.BigDecimal; -import java.util.Set; - -import org.sonar.plugins.java.api.semantic.MethodMatchers; -import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree; -import org.sonar.plugins.java.api.tree.MethodInvocationTree; -import org.sonar.plugins.java.api.tree.Tree; -import org.sonar.plugins.java.api.tree.TypeCastTree; - -enum ConstOrLiteralDeclare { - - BOOLEAN { - @Override - String className() { - return Boolean.class.getName(); - } - @Override - Set publicMembers() { - return Set.of("TRUE", "FALSE"); - } - @Override - MethodMatchers methodMatchers() { - return DEFAULT_METHOD_MATCHERS; - } - }, - - BYTE { - @Override - String className() { - return Byte.class.getName(); - } - @Override - Set publicMembers() { - return NUMBER_DEFAULT_MEMBERS; - } - @Override - MethodMatchers methodMatchers() { - return DEFAULT_METHOD_MATCHERS; - } - }, - - SHORT { - @Override - String className() { - return Short.class.getName(); - } - @Override - Set publicMembers() { - return NUMBER_DEFAULT_MEMBERS; - } - @Override - MethodMatchers methodMatchers() { - return DEFAULT_METHOD_MATCHERS; - } - }, - - INTEGER { - @Override - String className() { - return Integer.class.getName(); - } - - @Override - Set publicMembers() { - return NUMBER_DEFAULT_MEMBERS; - } - @Override - MethodMatchers methodMatchers() { - return DEFAULT_METHOD_MATCHERS; - } - }, - - LONG { - @Override - String className() { - return Long.class.getName(); - } - @Override - Set publicMembers() { - return NUMBER_DEFAULT_MEMBERS; - } - @Override - MethodMatchers methodMatchers() { - return DEFAULT_METHOD_MATCHERS; - } - }, - - FLOAT { - @Override - String className() { - return Float.class.getName(); - } - @Override - Set publicMembers() { - return NUMBER_DEFAULT_MEMBERS; - } - @Override - MethodMatchers methodMatchers() { - return DEFAULT_METHOD_MATCHERS; - } - }, - - DOUBLE { - @Override - String className() { - return Double.class.getName(); - } - @Override - Set publicMembers() { - return NUMBER_DEFAULT_MEMBERS; - } - @Override - MethodMatchers methodMatchers() { - return DEFAULT_METHOD_MATCHERS; - } - }, - - CHARACTER { - @Override - String className() { - return Character.class.getName(); - } - @Override - Set publicMembers() { - return NUMBER_DEFAULT_MEMBERS; - } - @Override - MethodMatchers methodMatchers() { - return DEFAULT_METHOD_MATCHERS; - } - }, - - BIGDECIMAL { - - @Override - String className() { - return BigDecimal.class.getName(); - } - - @Override - Set publicMembers() { - return Set.of("ZERO", "ONE", "TEN"); - } - @Override - MethodMatchers methodMatchers() { - return DEFAULT_METHOD_MATCHERS; - } - }; - - public boolean isPublicMember(MemberSelectExpressionTree tree) { - - return className().equals(tree.expression().symbolType().fullyQualifiedName()) //strong check - && publicMembers().contains(tree.identifier().toString()); - } - - public boolean isLiteralDeclare(MethodInvocationTree tree) { - - return methodMatchers().matches(tree) - && tree.arguments().stream().allMatch(ConstOrLiteralDeclare::isLiteral); - } - - abstract String className(); - - abstract Set publicMembers(); - - abstract MethodMatchers methodMatchers(); - - private static final Set NUMBER_DEFAULT_MEMBERS = Set.of("MIN_VALUE", "MAX_VALUE"); - - private static final MethodMatchers DEFAULT_METHOD_MATCHERS = create() - .ofSubTypes(Number.class.getName(), Boolean.class.getName(), Character.class.getName()).names("valueOf") - .addParametersMatcher(args-> !args.isEmpty()).build(); - - public static final boolean isLiteral(Tree arg) { - if (arg.is(TYPE_CAST)) { - arg = ((TypeCastTree) arg).expression(); - } - return arg.is(BOOLEAN_LITERAL) || - arg.is(INT_LITERAL) || - arg.is(LONG_LITERAL) || - arg.is(FLOAT_LITERAL) || - arg.is(DOUBLE_LITERAL) || - arg.is(STRING_LITERAL) || - arg.is(CHAR_LITERAL); - } -} \ No newline at end of file diff --git a/java-plugin/src/main/java/fr/cnumr/java/utils/PrinterVisitor.java b/java-plugin/src/main/java/fr/cnumr/java/utils/PrinterVisitor.java deleted file mode 100644 index b43154ad3..000000000 --- a/java-plugin/src/main/java/fr/cnumr/java/utils/PrinterVisitor.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) 2012-2021 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package fr.cnumr.java.utils; - -import java.util.List; -import java.util.function.Consumer; -import javax.annotation.Nullable; -import org.sonar.plugins.java.api.tree.BaseTreeVisitor; -import org.sonar.plugins.java.api.tree.Tree; - -public class PrinterVisitor extends BaseTreeVisitor { - private static final int INDENT_SPACES = 2; - - private final StringBuilder sb; - private int indentLevel; - - public PrinterVisitor() { - sb = new StringBuilder(); - indentLevel = 0; - } - - public static void print(Tree tree, Consumer output) { - PrinterVisitor pv = new PrinterVisitor(); - pv.scan(tree); - output.accept(pv.sb.toString()); - } - - private StringBuilder indent() { - return sb.append(StringUtils.spaces(INDENT_SPACES * indentLevel)); - } - - @Override - protected void scan(List trees) { - if (!trees.isEmpty()) { - sb.deleteCharAt(sb.length() - 1); - sb.append(" : [\n"); - super.scan(trees); - indent().append("]\n"); - } - } - - @Override - protected void scan(@Nullable Tree tree) { - if (tree != null) { - Class[] interfaces = tree.getClass().getInterfaces(); - if (interfaces.length > 0) { - indent().append(interfaces[0].getSimpleName()).append("\n"); - } - } - indentLevel++; - super.scan(tree); - indentLevel--; - } -} diff --git a/java-plugin/src/main/java/fr/cnumr/java/JavaCheckRegistrar.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java similarity index 98% rename from java-plugin/src/main/java/fr/cnumr/java/JavaCheckRegistrar.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java index 4fd2f8cca..1d92303b1 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/JavaCheckRegistrar.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.java; +package fr.greencodeinitiative.java; import java.util.List; diff --git a/java-plugin/src/main/java/fr/cnumr/java/JavaPlugin.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaPlugin.java similarity index 97% rename from java-plugin/src/main/java/fr/cnumr/java/JavaPlugin.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/JavaPlugin.java index 2e83d4a31..1e40808d8 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/JavaPlugin.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaPlugin.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.java; +package fr.greencodeinitiative.java; import org.sonar.api.Plugin; diff --git a/java-plugin/src/main/java/fr/cnumr/java/JavaRulesDefinition.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java similarity index 92% rename from java-plugin/src/main/java/fr/cnumr/java/JavaRulesDefinition.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java index 18a9fafbe..0e53324f5 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/JavaRulesDefinition.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.java; +package fr.greencodeinitiative.java; import java.util.ArrayList; import java.util.Collections; @@ -39,14 +39,14 @@ public class JavaRulesDefinition implements RulesDefinition { // don't change that because the path is hard coded in CheckVerifier - private static final String RESOURCE_BASE_PATH = "fr/cnumr/l10n/java/rules/java"; + 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 RULE_TEMPLATES_KEY = Collections.emptySet(); public static final String NAME = "Collectif Conception Numérique Responsable "; public static final String LANGUAGE = "java"; - public static final String REPOSITORY_KEY = "cnumr-java"; + public static final String REPOSITORY_KEY = "greencodeinitiative-java"; @Override public void define(Context context) { diff --git a/java-plugin/src/main/java/fr/cnumr/java/RulesList.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java similarity index 64% rename from java-plugin/src/main/java/fr/cnumr/java/RulesList.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java index f4ca41a00..d6a5cdbf4 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/RulesList.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java @@ -17,16 +17,34 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.java; - -import fr.cnumr.java.checks.*; -import org.sonar.plugins.java.api.JavaCheck; +package fr.greencodeinitiative.java; import java.util.ArrayList; import java.util.Arrays; 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.JavaCheck; + public final class RulesList { private RulesList() { @@ -41,7 +59,7 @@ public static List> getChecks() { public static List> getJavaChecks() { return Collections.unmodifiableList(Arrays.asList( - ArrayCopyCheck.class, + ArrayCopyCheck.class, IncrementCheck.class, AvoidConcatenateStringsInLoop.class, AvoidUsageOfStaticCollections.class, diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ArrayCopyCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ArrayCopyCheck.java new file mode 100644 index 000000000..984ed80d1 --- /dev/null +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ArrayCopyCheck.java @@ -0,0 +1,284 @@ +package fr.greencodeinitiative.java.checks; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +import org.sonar.check.Priority; +import org.sonar.check.Rule; +import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; +import org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree; +import org.sonar.plugins.java.api.tree.AssignmentExpressionTree; +import org.sonar.plugins.java.api.tree.BlockTree; +import org.sonar.plugins.java.api.tree.CatchTree; +import org.sonar.plugins.java.api.tree.DoWhileStatementTree; +import org.sonar.plugins.java.api.tree.ExpressionStatementTree; +import org.sonar.plugins.java.api.tree.ExpressionTree; +import org.sonar.plugins.java.api.tree.ForEachStatement; +import org.sonar.plugins.java.api.tree.ForStatementTree; +import org.sonar.plugins.java.api.tree.IdentifierTree; +import org.sonar.plugins.java.api.tree.IfStatementTree; +import org.sonar.plugins.java.api.tree.StatementTree; +import org.sonar.plugins.java.api.tree.Tree; +import org.sonar.plugins.java.api.tree.Tree.Kind; +import org.sonar.plugins.java.api.tree.TryStatementTree; +import org.sonar.plugins.java.api.tree.VariableTree; +import org.sonar.plugins.java.api.tree.WhileStatementTree; + +/** + * Array Copy Check + * + * @author Aubay + * @formatter:off + */ +@Rule(key = "GRPS0027", + name = "Developpement", + description = ArrayCopyCheck.MESSAGERULE, + priority = Priority.MINOR, + tags = {"bug"}) +public class ArrayCopyCheck extends IssuableSubscriptionVisitor { + + //@formatter:on + protected static final String MESSAGERULE = "Use System.arraycopy to copy arrays"; + + @Override + public List nodesToVisit() { + return Arrays.asList(Kind.FOR_STATEMENT, Kind.WHILE_STATEMENT, Kind.DO_STATEMENT, Kind.FOR_EACH_STATEMENT); + } + + /** + * Check a node. Report issue when found. + */ + @Override + public void visitNode(final Tree tree) { + // Determine blocks to be analyzed + final List blocs = getBlocsOfCode(tree); + + // Analyze blocks + for (final Bloc bloc : blocs) { + if (bloc.isForeach()) { + handleForEachAssignments(tree, bloc); + } + handleAssignments(tree, bloc); + } + } + + /** + * Handle for-each assignments controls. + * + * @param tree + * @param bloc + */ + private void handleForEachAssignments(final Tree tree, final Bloc bloc) { + for (final AssignmentExpressionTree assignment : extractAssignments(tree, bloc)) { + final ExpressionTree destination = assignment.variable(); + final ExpressionTree source = assignment.expression(); + if (isArray(destination) && isVariable(source)) { + final String destinationIdentifier = getArrayIdentifier(destination); + final String sourceIdentifier = ((IdentifierTree) source).name(); + if (bloc.getValue().equals(sourceIdentifier) && !bloc.getIterable().equals(destinationIdentifier)) { + reportIssue(tree, MESSAGERULE); + } + } + } + } + + /** + * Handle assignments controls. + * + * @param tree + * @param bloc + */ + private void handleAssignments(final Tree tree, final Bloc bloc) { + for (final AssignmentExpressionTree assignment : extractAssignments(tree, bloc)) { + final ExpressionTree destVariable = assignment.variable(); + final ExpressionTree srcEspression = assignment.expression(); + if (isArray(destVariable) && isArray(srcEspression)) { + final String destArray = getArrayIdentifier(destVariable); + final String srcArray = getArrayIdentifier(srcEspression); + if (destArray != null && !destArray.equals(srcArray)) { + reportIssue(tree, MESSAGERULE); + } + } + } + } + + /** + * Extract variable's name of array + * + * @param expression of Array + * @return Array's name + */ + private String getArrayIdentifier(final ExpressionTree expression) { + if (expression instanceof ArrayAccessExpressionTree) { + final ExpressionTree identifier = ((ArrayAccessExpressionTree) expression).expression(); + if (identifier instanceof IdentifierTree) { + return ((IdentifierTree) identifier).identifierToken().text(); + } + } + return null; + } + + /** + * Verify if expression is an Array + * + * @param expression + * @return true if instance of ArrayAccessExpressionTree, false else + */ + private boolean isArray(final ExpressionTree expression) { + return expression instanceof ArrayAccessExpressionTree; + } + + /** + * Verify if expression is an variable + * + * @param source + * @return + */ + private boolean isVariable(final ExpressionTree source) { + return source instanceof IdentifierTree; + } + + /** + * Extract nested blocks of code + * + * @param tree + * @return + */ + private List getBlocsOfCode(final Tree tree) { + final List blocs = new ArrayList<>(); + if (tree instanceof ForStatementTree) { + ForStatementTree castedForTree = (ForStatementTree) tree; + addBloc(blocs, castedForTree.statement()); + } else if (tree instanceof ForEachStatement) { + ForEachStatement castedForEachTree = (ForEachStatement) tree; + addForEachBloc(blocs, castedForEachTree.statement(), castedForEachTree.variable(), + castedForEachTree.expression()); + } else if (tree instanceof WhileStatementTree) { + WhileStatementTree castedWhileTree = (WhileStatementTree) tree; + addBloc(blocs, castedWhileTree.statement()); + } else if (tree instanceof DoWhileStatementTree) { + DoWhileStatementTree castedDoWhileTree = (DoWhileStatementTree) tree; + addBloc(blocs, castedDoWhileTree.statement()); + } else if (tree instanceof IfStatementTree) { + IfStatementTree castedIfTree = (IfStatementTree) tree; + addBloc(blocs, castedIfTree.thenStatement()); + addBloc(blocs, castedIfTree.elseStatement()); + } else if (tree instanceof TryStatementTree) { + final TryStatementTree tryTree = (TryStatementTree) tree; + addBloc(blocs, tryTree.block()); + addBloc(blocs, extractCatchBlocks(tryTree)); + addBloc(blocs, tryTree.finallyBlock()); + } + return blocs; + } + + /** + * Assignments extraction from block of code. + * + * @param tree + * @param block + * @return + */ + private List extractAssignments(final Tree tree, final Bloc bloc) { + final BlockTree block = bloc.getBlockTree(); + + // Prepare useful predicates + final Predicate blocksPredicate = statement -> statement.is(Kind.IF_STATEMENT) + || statement.is(Kind.TRY_STATEMENT); + final Predicate assignmentPredicate = statement -> statement.is(Kind.EXPRESSION_STATEMENT) + && ((ExpressionStatementTree) statement).expression().is(Kind.ASSIGNMENT); + + // Filter expressions to find assignments + final List result = block.body().stream().filter(assignmentPredicate) + .map(assign -> (AssignmentExpressionTree) ((ExpressionStatementTree) assign).expression()) + .collect(Collectors.toList()); + + // Recursive loop for nested blocks, add nested assignments to results + final List ifStatements = block.body().stream().filter(blocksPredicate) + .collect(Collectors.toList()); + for (final StatementTree ifstatement : ifStatements) { + final List blocs = getBlocsOfCode(ifstatement); + for (final Bloc b : blocs) { + result.addAll(extractAssignments(tree, b)); + } + } + return result; + } + + /** + * Extract all blocks of code from try/catch statement + * + * @param tryTree + * @return Array of StatementTree + */ + private BlockTree[] extractCatchBlocks(final TryStatementTree tryTree) { + final List catches = tryTree.catches(); + return catches.stream().map(CatchTree::block).collect(Collectors.toList()).toArray(new BlockTree[0]); + } + + /** + * Add a Bloc in list after type checking + * + * @param blocs + * @param statements + */ + private void addBloc(final List blocs, final StatementTree... statements) { + for (final StatementTree statement : statements) { + if (statement instanceof BlockTree) { + blocs.add(new Bloc((BlockTree) statement)); + } + } + } + + /** + * Add a Bloc in list after type checking + * + * @param blocs + * @param statement + * @param variable + * @param expression + */ + private void addForEachBloc(final List blocs, final StatementTree statement, final VariableTree variable, + final ExpressionTree expression) { + if (statement instanceof BlockTree && expression instanceof IdentifierTree) { + blocs.add(new Bloc((BlockTree) statement, ((IdentifierTree) expression).identifierToken().text(), + variable.simpleName().identifierToken().text())); + } + } + + private static class Bloc { + private final BlockTree blockTree; + private String iterable; + private String value; + + public Bloc(final BlockTree blockTree, final String iterable, final String value) { + this.blockTree = blockTree; + this.iterable = iterable; + this.value = value; + } + + public boolean isForeach() { + return iterable != null && value != null; + } + + public Bloc(final BlockTree blockTree) { + this.blockTree = blockTree; + } + + public BlockTree getBlockTree() { + return blockTree; + } + + public String getIterable() { + return iterable; + } + + public String getValue() { + return value; + } + + } +} diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidConcatenateStringsInLoop.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoop.java similarity index 85% rename from java-plugin/src/main/java/fr/cnumr/java/checks/AvoidConcatenateStringsInLoop.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoop.java index 378524252..6a0dfd670 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidConcatenateStringsInLoop.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoop.java @@ -1,13 +1,18 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; + +import java.util.Arrays; +import java.util.List; + +import javax.annotation.Nonnull; import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; -import org.sonar.plugins.java.api.tree.*; - -import javax.annotation.Nonnull; -import java.util.Arrays; -import java.util.List; +import org.sonar.plugins.java.api.tree.AssignmentExpressionTree; +import org.sonar.plugins.java.api.tree.BaseTreeVisitor; +import org.sonar.plugins.java.api.tree.BinaryExpressionTree; +import org.sonar.plugins.java.api.tree.ExpressionTree; +import org.sonar.plugins.java.api.tree.Tree; @Rule( key = "S75", diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidFullSQLRequest.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidFullSQLRequest.java similarity index 66% rename from java-plugin/src/main/java/fr/cnumr/java/checks/AvoidFullSQLRequest.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidFullSQLRequest.java index 29d2bf3f1..01d8d7029 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidFullSQLRequest.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidFullSQLRequest.java @@ -1,12 +1,11 @@ -package fr.cnumr.java.checks; - -import static java.util.Collections.singletonList; -import static java.util.regex.Pattern.CASE_INSENSITIVE; -import static java.util.regex.Pattern.compile; +package fr.greencodeinitiative.java.checks; import java.util.List; import java.util.function.Predicate; +import static java.util.Collections.singletonList; +import static java.util.regex.Pattern.CASE_INSENSITIVE; +import static java.util.regex.Pattern.compile; import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; @@ -22,10 +21,10 @@ tags = {"bug"}) public class AvoidFullSQLRequest extends IssuableSubscriptionVisitor { - protected static final String MESSAGERULE = "Don't use the query SELECT * FROM"; - private static final Predicate SELECT_FROM_REGEXP = - compile("select\\s*\\*\\s*from", CASE_INSENSITIVE).asPredicate(); //simple regexp, more precision - + protected static final String MESSAGERULE = "Don't use the query SELECT * FROM"; + private static final Predicate SELECT_FROM_REGEXP = + compile("select\\s*\\*\\s*from", CASE_INSENSITIVE).asPredicate(); //simple regexp, more precision + @Override public List nodesToVisit() { return singletonList(Tree.Kind.STRING_LITERAL); @@ -33,9 +32,9 @@ public List nodesToVisit() { @Override public void visitNode(Tree tree) { - String value = ((LiteralTree) tree).value(); - if (SELECT_FROM_REGEXP.test(value)) { - reportIssue(tree, MESSAGERULE); - } + String value = ((LiteralTree) tree).value(); + if (SELECT_FROM_REGEXP.test(value)) { + reportIssue(tree, MESSAGERULE); + } } } \ No newline at end of file diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidGettingSizeCollectionInLoop.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidGettingSizeCollectionInLoop.java similarity index 98% rename from java-plugin/src/main/java/fr/cnumr/java/checks/AvoidGettingSizeCollectionInLoop.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidGettingSizeCollectionInLoop.java index 7bc65e966..ba48279f2 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidGettingSizeCollectionInLoop.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidGettingSizeCollectionInLoop.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Arrays; import java.util.List; diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidMultipleIfElseStatement.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatement.java similarity index 98% rename from java-plugin/src/main/java/fr/cnumr/java/checks/AvoidMultipleIfElseStatement.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatement.java index 88c378d67..9922dcd50 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidMultipleIfElseStatement.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatement.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Arrays; import java.util.List; diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidRegexPatternNotStatic.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java similarity index 97% rename from java-plugin/src/main/java/fr/cnumr/java/checks/AvoidRegexPatternNotStatic.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java index 4201d6cda..c586b6978 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidRegexPatternNotStatic.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java @@ -1,4 +1,10 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; + +import java.util.Collections; +import java.util.List; +import java.util.regex.Pattern; + +import javax.annotation.Nonnull; import org.sonar.check.Priority; import org.sonar.check.Rule; @@ -9,11 +15,6 @@ import org.sonar.plugins.java.api.tree.MethodTree; import org.sonar.plugins.java.api.tree.Tree; -import javax.annotation.Nonnull; -import java.util.Collections; -import java.util.List; -import java.util.regex.Pattern; - @Rule( key = "S77", name = "Developpement", diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidSQLRequestInLoop.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoop.java similarity index 76% rename from java-plugin/src/main/java/fr/cnumr/java/checks/AvoidSQLRequestInLoop.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoop.java index d59b786a3..e3fbb0cfe 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidSQLRequestInLoop.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoop.java @@ -1,6 +1,4 @@ -package fr.cnumr.java.checks; - -import static org.sonar.plugins.java.api.semantic.MethodMatchers.CONSTRUCTOR; +package fr.greencodeinitiative.java.checks; import java.util.Arrays; import java.util.List; @@ -9,13 +7,14 @@ import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.semantic.MethodMatchers; +import static org.sonar.plugins.java.api.semantic.MethodMatchers.CONSTRUCTOR; import org.sonar.plugins.java.api.tree.BaseTreeVisitor; import org.sonar.plugins.java.api.tree.MethodInvocationTree; import org.sonar.plugins.java.api.tree.Tree; import org.sonar.plugins.java.api.tree.Tree.Kind; @Rule(key = "S72", name = "Developpement", description = AvoidSQLRequestInLoop.MESSAGERULE, priority = Priority.MINOR, - tags = {"bug" }) + tags = {"bug"}) public class AvoidSQLRequestInLoop extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Avoid SQL request in loop"; @@ -46,30 +45,30 @@ public class AvoidSQLRequestInLoop extends IssuableSubscriptionVisitor { .withAnyParameters().build(), MethodMatchers.create().ofSubTypes("javax.jdo.Query").names("setFilter", "setGrouping") .withAnyParameters().build()); - - private final AvoidSQLRequestInLoopVisitor visitorInFile = new AvoidSQLRequestInLoopVisitor(); - @Override - public List nodesToVisit() { - return Arrays.asList( - Tree.Kind.FOR_EACH_STATEMENT, Tree.Kind.FOR_STATEMENT, - Tree.Kind.WHILE_STATEMENT, Tree.Kind.DO_STATEMENT); - } + private final AvoidSQLRequestInLoopVisitor visitorInFile = new AvoidSQLRequestInLoopVisitor(); + + @Override + public List nodesToVisit() { + return Arrays.asList( + Tree.Kind.FOR_EACH_STATEMENT, Tree.Kind.FOR_STATEMENT, + Tree.Kind.WHILE_STATEMENT, Tree.Kind.DO_STATEMENT); + } - @Override - public void visitNode(Tree tree) { - tree.accept(visitorInFile); - } + @Override + public void visitNode(Tree tree) { + tree.accept(visitorInFile); + } - private class AvoidSQLRequestInLoopVisitor extends BaseTreeVisitor { + private class AvoidSQLRequestInLoopVisitor extends BaseTreeVisitor { - @Override - public void visitMethodInvocation(MethodInvocationTree tree) { - if (SQL_METHOD.matches(tree)) { - reportIssue(tree, MESSAGERULE); - } else { - super.visitMethodInvocation(tree); - } - } - } + @Override + public void visitMethodInvocation(MethodInvocationTree tree) { + if (SQL_METHOD.matches(tree)) { + reportIssue(tree, MESSAGERULE); + } else { + super.visitMethodInvocation(tree); + } + } + } } \ No newline at end of file diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchUpdate.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchUpdate.java new file mode 100644 index 000000000..eddd33552 --- /dev/null +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchUpdate.java @@ -0,0 +1,74 @@ +package fr.greencodeinitiative.java.checks; + +import java.sql.PreparedStatement; +import java.util.List; +import java.util.stream.Stream; + +import static fr.greencodeinitiative.java.checks.ConstOrLiteralDeclare.isLiteral; +import static java.util.Arrays.asList; +import org.sonar.check.Priority; +import org.sonar.check.Rule; +import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; +import org.sonar.plugins.java.api.semantic.MethodMatchers; +import static org.sonar.plugins.java.api.semantic.Type.Primitives.INT; +import org.sonar.plugins.java.api.tree.BaseTreeVisitor; +import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree; +import org.sonar.plugins.java.api.tree.MethodInvocationTree; +import org.sonar.plugins.java.api.tree.Tree; +import org.sonar.plugins.java.api.tree.Tree.Kind; +import static org.sonar.plugins.java.api.tree.Tree.Kind.MEMBER_SELECT; +import static org.sonar.plugins.java.api.tree.Tree.Kind.METHOD_INVOCATION; + +@Rule(key = "S78", name = "Developpement", + description = AvoidSetConstantInBatchUpdate.MESSAGERULE, + priority = Priority.MINOR, + tags = {"bug"}) + +public class AvoidSetConstantInBatchUpdate extends IssuableSubscriptionVisitor { + + protected static final String MESSAGERULE = "Avoid setting constants in batch update"; + private final AvoidSetConstantInBatchUpdateVisitor visitorInFile = new AvoidSetConstantInBatchUpdateVisitor(); + + @Override + public List nodesToVisit() { + return asList( + Tree.Kind.FOR_EACH_STATEMENT, + Tree.Kind.FOR_STATEMENT, + Tree.Kind.WHILE_STATEMENT, + Tree.Kind.DO_STATEMENT); + } + + @Override + public void visitNode(Tree tree) { + tree.accept(visitorInFile); + } + + private class AvoidSetConstantInBatchUpdateVisitor extends BaseTreeVisitor { + + private final MethodMatchers setters = MethodMatchers.create().ofSubTypes(PreparedStatement.class.getName()) + .names("setBoolean", "setByte", "setShort", "setInt", "setLong", "setFloat", "setDouble", + "setBigDecimal", "setString") + .addParametersMatcher(args -> args.size() == 2 && args.get(0).isPrimitive(INT)).build(); + + @Override + public void visitMethodInvocation(MethodInvocationTree tree) { + if (setters.matches(tree) && isConstant(tree.arguments().get(1))) { + reportIssue(tree, MESSAGERULE); + } else { + super.visitMethodInvocation(tree); + } + } + } + + private static final boolean isConstant(Tree arg) { + + if (arg.is(METHOD_INVOCATION)) { + MethodInvocationTree m = (MethodInvocationTree) arg; + return Stream.of(ConstOrLiteralDeclare.values()).anyMatch(o -> o.isLiteralDeclare(m)); + } else if (arg.is(MEMBER_SELECT)) { + MemberSelectExpressionTree m = (MemberSelectExpressionTree) arg; + return Stream.of(ConstOrLiteralDeclare.values()).anyMatch(o -> o.isPublicMember(m)); + } + return isLiteral(arg); + } +} \ No newline at end of file diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidSpringRepositoryCallInLoopCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopCheck.java similarity index 87% rename from java-plugin/src/main/java/fr/cnumr/java/checks/AvoidSpringRepositoryCallInLoopCheck.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopCheck.java index 5a8fcf61e..73df8da7c 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidSpringRepositoryCallInLoopCheck.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopCheck.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Arrays; import java.util.List; @@ -11,11 +11,11 @@ import org.sonar.plugins.java.api.tree.MethodInvocationTree; import org.sonar.plugins.java.api.tree.Tree; -@Rule(key = "GRC1", - name = "Developpement", - description = AvoidSpringRepositoryCallInLoopCheck.RULE_MESSAGE, - priority = Priority.MINOR, - tags = {"bug" }) +@Rule(key = "GRC1", + name = "Developpement", + description = AvoidSpringRepositoryCallInLoopCheck.RULE_MESSAGE, + priority = Priority.MINOR, + tags = {"bug"}) public class AvoidSpringRepositoryCallInLoopCheck extends IssuableSubscriptionVisitor { protected static final String RULE_MESSAGE = "Avoid Spring repository call in loop"; @@ -25,7 +25,7 @@ public class AvoidSpringRepositoryCallInLoopCheck extends IssuableSubscriptionVi private static final MethodMatchers REPOSITORY_METHOD = MethodMatchers.create().ofSubTypes(SPRING_REPOSITORY).anyName().withAnyParameters() .build(); - + private final AvoidSpringRepositoryCallInLoopCheckVisitor visitorInFile = new AvoidSpringRepositoryCallInLoopCheckVisitor(); @Override diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidStatementForDMLQueries.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueries.java similarity index 81% rename from java-plugin/src/main/java/fr/cnumr/java/checks/AvoidStatementForDMLQueries.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueries.java index 520372964..c9ccd778f 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidStatementForDMLQueries.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueries.java @@ -1,15 +1,18 @@ -package fr.cnumr.java.checks; - -import org.sonar.check.Rule; -import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; -import org.sonar.plugins.java.api.semantic.MethodMatchers; -import org.sonar.plugins.java.api.tree.*; +package fr.greencodeinitiative.java.checks; +import java.util.Collections; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import java.util.Collections; -import java.util.List; +import org.sonar.check.Rule; +import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; +import org.sonar.plugins.java.api.semantic.MethodMatchers; +import org.sonar.plugins.java.api.tree.Arguments; +import org.sonar.plugins.java.api.tree.ExpressionTree; +import org.sonar.plugins.java.api.tree.LiteralTree; +import org.sonar.plugins.java.api.tree.MethodInvocationTree; +import org.sonar.plugins.java.api.tree.Tree; @Rule(key = "SDMLQ1") public class AvoidStatementForDMLQueries extends IssuableSubscriptionVisitor { @@ -32,8 +35,7 @@ public void visitNode(Tree tree) { if (arguments.size() < 1) return; ExpressionTree first = arguments.get(0); - if (first.is(Tree.Kind.STRING_LITERAL)) - { + if (first.is(Tree.Kind.STRING_LITERAL)) { LiteralTree literalTree = (LiteralTree) first; String str = literalTree.value(); String regex = "(SELECT|INSERT INTO|UPDATE|DELETE FROM)\\s?.*"; diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidUsageOfStaticCollections.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollections.java similarity index 97% rename from java-plugin/src/main/java/fr/cnumr/java/checks/AvoidUsageOfStaticCollections.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollections.java index 638656b93..177cec7e9 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidUsageOfStaticCollections.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollections.java @@ -1,4 +1,10 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import javax.annotation.Nonnull; import org.sonar.check.Priority; import org.sonar.check.Rule; @@ -7,11 +13,6 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonar.plugins.java.api.tree.VariableTree; -import javax.annotation.Nonnull; -import java.util.Collections; -import java.util.List; -import java.util.Map; - @Rule( key = "S76", name = "Developpement", diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidUsingGlobalVariablesCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheck.java similarity index 97% rename from java-plugin/src/main/java/fr/cnumr/java/checks/AvoidUsingGlobalVariablesCheck.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheck.java index c5b2e2d58..010a09b25 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/AvoidUsingGlobalVariablesCheck.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheck.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Arrays; import java.util.List; diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ConstOrLiteralDeclare.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ConstOrLiteralDeclare.java new file mode 100644 index 000000000..8a5b459a0 --- /dev/null +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ConstOrLiteralDeclare.java @@ -0,0 +1,212 @@ +package fr.greencodeinitiative.java.checks; + +import java.math.BigDecimal; +import java.util.Set; + +import org.sonar.plugins.java.api.semantic.MethodMatchers; +import static org.sonar.plugins.java.api.semantic.MethodMatchers.create; +import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree; +import org.sonar.plugins.java.api.tree.MethodInvocationTree; +import org.sonar.plugins.java.api.tree.Tree; +import static org.sonar.plugins.java.api.tree.Tree.Kind.BOOLEAN_LITERAL; +import static org.sonar.plugins.java.api.tree.Tree.Kind.CHAR_LITERAL; +import static org.sonar.plugins.java.api.tree.Tree.Kind.DOUBLE_LITERAL; +import static org.sonar.plugins.java.api.tree.Tree.Kind.FLOAT_LITERAL; +import static org.sonar.plugins.java.api.tree.Tree.Kind.INT_LITERAL; +import static org.sonar.plugins.java.api.tree.Tree.Kind.LONG_LITERAL; +import static org.sonar.plugins.java.api.tree.Tree.Kind.STRING_LITERAL; +import static org.sonar.plugins.java.api.tree.Tree.Kind.TYPE_CAST; +import org.sonar.plugins.java.api.tree.TypeCastTree; + +enum ConstOrLiteralDeclare { + + BOOLEAN { + @Override + String className() { + return Boolean.class.getName(); + } + + @Override + Set publicMembers() { + return Set.of("TRUE", "FALSE"); + } + + @Override + MethodMatchers methodMatchers() { + return DEFAULT_METHOD_MATCHERS; + } + }, + + BYTE { + @Override + String className() { + return Byte.class.getName(); + } + + @Override + Set publicMembers() { + return NUMBER_DEFAULT_MEMBERS; + } + + @Override + MethodMatchers methodMatchers() { + return DEFAULT_METHOD_MATCHERS; + } + }, + + SHORT { + @Override + String className() { + return Short.class.getName(); + } + + @Override + Set publicMembers() { + return NUMBER_DEFAULT_MEMBERS; + } + + @Override + MethodMatchers methodMatchers() { + return DEFAULT_METHOD_MATCHERS; + } + }, + + INTEGER { + @Override + String className() { + return Integer.class.getName(); + } + + @Override + Set publicMembers() { + return NUMBER_DEFAULT_MEMBERS; + } + + @Override + MethodMatchers methodMatchers() { + return DEFAULT_METHOD_MATCHERS; + } + }, + + LONG { + @Override + String className() { + return Long.class.getName(); + } + + @Override + Set publicMembers() { + return NUMBER_DEFAULT_MEMBERS; + } + + @Override + MethodMatchers methodMatchers() { + return DEFAULT_METHOD_MATCHERS; + } + }, + + FLOAT { + @Override + String className() { + return Float.class.getName(); + } + + @Override + Set publicMembers() { + return NUMBER_DEFAULT_MEMBERS; + } + + @Override + MethodMatchers methodMatchers() { + return DEFAULT_METHOD_MATCHERS; + } + }, + + DOUBLE { + @Override + String className() { + return Double.class.getName(); + } + + @Override + Set publicMembers() { + return NUMBER_DEFAULT_MEMBERS; + } + + @Override + MethodMatchers methodMatchers() { + return DEFAULT_METHOD_MATCHERS; + } + }, + + CHARACTER { + @Override + String className() { + return Character.class.getName(); + } + + @Override + Set publicMembers() { + return NUMBER_DEFAULT_MEMBERS; + } + + @Override + MethodMatchers methodMatchers() { + return DEFAULT_METHOD_MATCHERS; + } + }, + + BIGDECIMAL { + @Override + String className() { + return BigDecimal.class.getName(); + } + + @Override + Set publicMembers() { + return Set.of("ZERO", "ONE", "TEN"); + } + + @Override + MethodMatchers methodMatchers() { + return DEFAULT_METHOD_MATCHERS; + } + }; + + public boolean isPublicMember(MemberSelectExpressionTree tree) { + + return className().equals(tree.expression().symbolType().fullyQualifiedName()) //strong check + && publicMembers().contains(tree.identifier().toString()); + } + + public boolean isLiteralDeclare(MethodInvocationTree tree) { + + return methodMatchers().matches(tree) + && tree.arguments().stream().allMatch(ConstOrLiteralDeclare::isLiteral); + } + + abstract String className(); + + abstract Set publicMembers(); + + abstract MethodMatchers methodMatchers(); + + private static final Set NUMBER_DEFAULT_MEMBERS = Set.of("MIN_VALUE", "MAX_VALUE"); + + private static final MethodMatchers DEFAULT_METHOD_MATCHERS = create() + .ofSubTypes(Number.class.getName(), Boolean.class.getName(), Character.class.getName()).names("valueOf") + .addParametersMatcher(args -> !args.isEmpty()).build(); + + public static final boolean isLiteral(Tree arg) { + if (arg.is(TYPE_CAST)) { + arg = ((TypeCastTree) arg).expression(); + } + return arg.is(BOOLEAN_LITERAL) || + arg.is(INT_LITERAL) || + arg.is(LONG_LITERAL) || + arg.is(FLOAT_LITERAL) || + arg.is(DOUBLE_LITERAL) || + arg.is(STRING_LITERAL) || + arg.is(CHAR_LITERAL); + } +} \ No newline at end of file diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/FreeResourcesOfAutoCloseableInterface.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterface.java similarity index 92% rename from java-plugin/src/main/java/fr/cnumr/java/checks/FreeResourcesOfAutoCloseableInterface.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterface.java index 1c8a685b6..9d57a0ff2 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/FreeResourcesOfAutoCloseableInterface.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterface.java @@ -1,4 +1,12 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +import javax.annotation.ParametersAreNonnullByDefault; import org.sonar.check.Priority; import org.sonar.check.Rule; @@ -9,9 +17,6 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonar.plugins.java.api.tree.TryStatementTree; -import javax.annotation.ParametersAreNonnullByDefault; -import java.util.*; - @Rule( key = "S79", @@ -46,7 +51,7 @@ public void visitNode(Tree tree) { toReport.push(new ArrayList<>()); } } - if (tree.is(Tree.Kind.NEW_CLASS) && ((NewClassTree) tree).symbolType().isSubtypeOf(JAVA_LANG_AUTOCLOSEABLE) && withinStandardTryWithFinally() ) { + if (tree.is(Tree.Kind.NEW_CLASS) && ((NewClassTree) tree).symbolType().isSubtypeOf(JAVA_LANG_AUTOCLOSEABLE) && withinStandardTryWithFinally()) { assert toReport.peek() != null; toReport.peek().add(tree); } diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/IncrementCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java similarity index 86% rename from java-plugin/src/main/java/fr/cnumr/java/checks/IncrementCheck.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java index 67b3b5938..3212abff3 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/IncrementCheck.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java @@ -1,4 +1,7 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; + +import java.util.Collections; +import java.util.List; import org.sonar.check.Priority; import org.sonar.check.Rule; @@ -6,9 +9,6 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonar.plugins.java.api.tree.Tree.Kind; -import java.util.Collections; -import java.util.List; - @Rule( key = "S67", name = "Developpement", @@ -17,7 +17,8 @@ tags = {"bug"}) public class IncrementCheck extends IssuableSubscriptionVisitor { - protected static final String MESSAGERULE = "Use ++i instead of i++"; + protected static final String MESSAGERULE = "Use ++i instead of i++"; + @Override public List nodesToVisit() { return Collections.singletonList(Kind.POSTFIX_INCREMENT); diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/InitializeBufferWithAppropriateSize.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSize.java similarity index 87% rename from java-plugin/src/main/java/fr/cnumr/java/checks/InitializeBufferWithAppropriateSize.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSize.java index 01d9e1df0..70fd5b897 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/InitializeBufferWithAppropriateSize.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSize.java @@ -1,4 +1,7 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; + +import java.util.Collections; +import java.util.List; import org.sonar.check.Priority; import org.sonar.check.Rule; @@ -7,9 +10,6 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonar.plugins.java.api.tree.Tree.Kind; -import java.util.Collections; -import java.util.List; - @Rule( key = "GRSP0032", name = "Developpement", @@ -18,7 +18,7 @@ tags = {"bug"}) public class InitializeBufferWithAppropriateSize extends IssuableSubscriptionVisitor { - protected static final String RULE_MESSAGE = "Initialize StringBuilder or StringBuffer with appropriate size"; + protected static final String RULE_MESSAGE = "Initialize StringBuilder or StringBuffer with appropriate size"; @Override public List nodesToVisit() { diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/NoFunctionCallWhenDeclaringForLoop.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoop.java similarity index 99% rename from java-plugin/src/main/java/fr/cnumr/java/checks/NoFunctionCallWhenDeclaringForLoop.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoop.java index 00d6bb5d6..ee7d3a9e6 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/NoFunctionCallWhenDeclaringForLoop.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoop.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.ArrayList; import java.util.Collection; diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/OptimizeReadFileExceptions.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptions.java similarity index 98% rename from java-plugin/src/main/java/fr/cnumr/java/checks/OptimizeReadFileExceptions.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptions.java index ea653708e..cadc726f4 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/OptimizeReadFileExceptions.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptions.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Arrays; diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/UnnecessarilyAssignValuesToVariables.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariables.java similarity index 99% rename from java-plugin/src/main/java/fr/cnumr/java/checks/UnnecessarilyAssignValuesToVariables.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariables.java index 1cdb9b18b..fb7e2774f 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/UnnecessarilyAssignValuesToVariables.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariables.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.ArrayList; import java.util.Collection; diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/UseCorrectForLoop.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UseCorrectForLoop.java similarity index 75% rename from java-plugin/src/main/java/fr/cnumr/java/checks/UseCorrectForLoop.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UseCorrectForLoop.java index 08dae4b22..88d6bfe96 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/UseCorrectForLoop.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UseCorrectForLoop.java @@ -1,4 +1,7 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; + +import java.util.Arrays; +import java.util.List; import org.sonar.check.Priority; import org.sonar.check.Rule; @@ -7,9 +10,6 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonar.plugins.java.api.tree.Tree.Kind; -import java.util.Arrays; -import java.util.List; - @Rule( key = "S53", name = "Developpement", @@ -18,7 +18,8 @@ tags = {"bug"}) public class UseCorrectForLoop extends IssuableSubscriptionVisitor { - protected static final String MESSAGERULE = "Avoid the use of Foreach with Arrays"; + protected static final String MESSAGERULE = "Avoid the use of Foreach with Arrays"; + @Override public List nodesToVisit() { return Arrays.asList(Tree.Kind.FOR_EACH_STATEMENT); @@ -26,9 +27,9 @@ public List nodesToVisit() { @Override public void visitNode(Tree tree) { - - ForEachStatement forEachTree = (ForEachStatement) tree; - if (forEachTree.expression().symbolType().isArray()) { + + ForEachStatement forEachTree = (ForEachStatement) tree; + if (forEachTree.expression().symbolType().isArray()) { reportIssue(tree, MESSAGERULE); } } diff --git a/java-plugin/src/main/java/fr/cnumr/java/package-info.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/package-info.java similarity index 95% rename from java-plugin/src/main/java/fr/cnumr/java/package-info.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/checks/package-info.java index e675a9380..03dc1c94a 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/package-info.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/package-info.java @@ -17,4 +17,4 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.java; +package fr.greencodeinitiative.java.checks; diff --git a/java-plugin/src/main/java/fr/cnumr/java/utils/package-info.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/package-info.java similarity index 95% rename from java-plugin/src/main/java/fr/cnumr/java/utils/package-info.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/package-info.java index 449adc5b6..5f3b0c8b2 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/utils/package-info.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/package-info.java @@ -17,4 +17,4 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.java.utils; +package fr.greencodeinitiative.java; diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/utils/PrinterVisitor.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/utils/PrinterVisitor.java new file mode 100644 index 000000000..2cb531718 --- /dev/null +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/utils/PrinterVisitor.java @@ -0,0 +1,73 @@ +/* + * SonarQube Java + * Copyright (C) 2012-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package fr.greencodeinitiative.java.utils; + +import java.util.List; +import java.util.function.Consumer; + +import javax.annotation.Nullable; + +import org.sonar.plugins.java.api.tree.BaseTreeVisitor; +import org.sonar.plugins.java.api.tree.Tree; + +public class PrinterVisitor extends BaseTreeVisitor { + private static final int INDENT_SPACES = 2; + + private final StringBuilder sb; + private int indentLevel; + + public PrinterVisitor() { + sb = new StringBuilder(); + indentLevel = 0; + } + + public static void print(Tree tree, Consumer output) { + PrinterVisitor pv = new PrinterVisitor(); + pv.scan(tree); + output.accept(pv.sb.toString()); + } + + private StringBuilder indent() { + return sb.append(StringUtils.spaces(INDENT_SPACES * indentLevel)); + } + + @Override + protected void scan(List trees) { + if (!trees.isEmpty()) { + sb.deleteCharAt(sb.length() - 1); + sb.append(" : [\n"); + super.scan(trees); + indent().append("]\n"); + } + } + + @Override + protected void scan(@Nullable Tree tree) { + if (tree != null) { + Class[] interfaces = tree.getClass().getInterfaces(); + if (interfaces.length > 0) { + indent().append(interfaces[0].getSimpleName()).append("\n"); + } + } + indentLevel++; + super.scan(tree); + indentLevel--; + } +} diff --git a/java-plugin/src/main/java/fr/cnumr/java/utils/StringUtils.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/utils/StringUtils.java similarity index 68% rename from java-plugin/src/main/java/fr/cnumr/java/utils/StringUtils.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/utils/StringUtils.java index 4630d3483..78f6e4df6 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/utils/StringUtils.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/utils/StringUtils.java @@ -17,24 +17,24 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.java.utils; +package fr.greencodeinitiative.java.utils; public final class StringUtils { - private StringUtils() { - // Utility class - } + private StringUtils() { + // Utility class + } - public static String spaces(int number) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < number; i++) { - sb.append(' '); + public static String spaces(int number) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < number; i++) { + sb.append(' '); + } + return sb.toString(); } - return sb.toString(); - } - public static boolean isNotEmpty(String string) { - return string != null && !string.isEmpty(); - } + public static boolean isNotEmpty(String string) { + return string != null && !string.isEmpty(); + } } diff --git a/java-plugin/src/main/java/fr/cnumr/java/checks/package-info.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/utils/package-info.java similarity index 95% rename from java-plugin/src/main/java/fr/cnumr/java/checks/package-info.java rename to java-plugin/src/main/java/fr/greencodeinitiative/java/utils/package-info.java index 80fa7023e..f88cc6249 100644 --- a/java-plugin/src/main/java/fr/cnumr/java/checks/package-info.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/utils/package-info.java @@ -17,4 +17,4 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.utils; diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/AMIES.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/AMIES.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/AMIES.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/AMIES.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/AMIES.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/AMIES.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/AMIES.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/AMIES.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/D4.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/D4.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/D4.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/D4.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/D4.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/D4.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/D4.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/D4.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRC1.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRC1.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRC1.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRC1.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRC1.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRC1.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRC1.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRC1.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRPS0027.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRPS0027.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRPS0027.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRPS0027.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRPS0027.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRPS0027.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRPS0027.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRPS0027.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRSP0028.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRSP0028.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRSP0028.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRSP0028.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRSP0028.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRSP0028.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRSP0028.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRSP0028.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRSP0032.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRSP0032.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRSP0032.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRSP0032.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRSP0032.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRSP0032.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GRSP0032.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GRSP0032.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GSCIL.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GSCIL.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GSCIL.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GSCIL.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GSCIL.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GSCIL.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/GSCIL.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/GSCIL.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S53.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S53.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S53.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S53.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S53.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S53.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S53.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S53.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S63.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S63.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S63.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S63.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S63.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S63.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S63.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S63.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S67.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S67.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S67.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S67.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S67.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S67.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S67.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S67.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S69.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S69.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S69.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S69.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S69.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S69.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S69.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S69.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S72.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S72.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S72.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S72.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S72.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S72.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S72.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S72.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S74.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S74.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S74.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S74.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S74.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S74.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S74.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S74.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S75.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S75.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S75.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S75.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S75.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S75.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S75.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S75.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S76.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S76.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S76.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S76.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S76.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S76.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S76.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S76.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S77.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S77.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S77.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S77.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S77.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S77.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S77.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S77.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S78.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S78.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S78.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S78.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S78.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S78.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S78.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S78.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S79.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S79.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S79.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S79.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S79.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S79.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/S79.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/S79.json diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/SDMLQ1.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/SDMLQ1.html similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/SDMLQ1.html rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/SDMLQ1.html diff --git a/java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/SDMLQ1.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/SDMLQ1.json similarity index 100% rename from java-plugin/src/main/resources/fr/cnumr/l10n/java/rules/java/SDMLQ1.json rename to java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/SDMLQ1.json diff --git a/java-plugin/src/test/files/AvoidConcatenateStringsInLoop.java b/java-plugin/src/test/files/AvoidConcatenateStringsInLoop.java index 7b50c1c13..55446ae49 100644 --- a/java-plugin/src/test/files/AvoidConcatenateStringsInLoop.java +++ b/java-plugin/src/test/files/AvoidConcatenateStringsInLoop.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.utils; +package fr.greencodeinitiative.java.utils; public class AvoidConcatenateStringsInLoop { diff --git a/java-plugin/src/test/files/AvoidFullSQLRequestCheck.java b/java-plugin/src/test/files/AvoidFullSQLRequestCheck.java index 52a9bc391..b4a75b986 100644 --- a/java-plugin/src/test/files/AvoidFullSQLRequestCheck.java +++ b/java-plugin/src/test/files/AvoidFullSQLRequestCheck.java @@ -1,32 +1,32 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.regex.Pattern; class AvoidFullSQLRequestCheck { - AvoidFullSQLRequestCheck(AvoidFullSQLRequestCheck mc) { + AvoidFullSQLRequestCheck(AvoidFullSQLRequestCheck mc) { } - public void literalSQLrequest() { - dummyCall(" sElEcT * fRoM myTable"); // Noncompliant - dummyCall(" sElEcT user fRoM myTable"); - - dummyCall("SELECTABLE 2*2 FROMAGE"); //not sql - dummyCall("SELECT *FROM table"); // Noncompliant - } - - - public void variableSQLrequest() { - String requestNonCompiliant = " SeLeCt * FrOm myTable"; // Noncompliant - String requestCompiliant = " SeLeCt user FrOm myTable"; - dummyCall(requestNonCompiliant); - dummyCall(requestCompiliant); - - String noSqlCompiliant = "SELECTABLE 2*2 FROMAGE"; //not sql - String requestNonCompiliant_nSpace = "SELECT *FROM table"; // Noncompliant - } - - private void dummyCall (String request) { - - } + public void literalSQLrequest() { + dummyCall(" sElEcT * fRoM myTable"); // Noncompliant + dummyCall(" sElEcT user fRoM myTable"); + + dummyCall("SELECTABLE 2*2 FROMAGE"); //not sql + dummyCall("SELECT *FROM table"); // Noncompliant + } + + + public void variableSQLrequest() { + String requestNonCompiliant = " SeLeCt * FrOm myTable"; // Noncompliant + String requestCompiliant = " SeLeCt user FrOm myTable"; + dummyCall(requestNonCompiliant); + dummyCall(requestCompiliant); + + String noSqlCompiliant = "SELECTABLE 2*2 FROMAGE"; //not sql + String requestNonCompiliant_nSpace = "SELECT *FROM table"; // Noncompliant + } + + private void dummyCall(String request) { + + } } \ No newline at end of file diff --git a/java-plugin/src/test/files/AvoidGettingSizeCollectionInForEachLoopIgnored.java b/java-plugin/src/test/files/AvoidGettingSizeCollectionInForEachLoopIgnored.java index 44e4febef..18d38b49b 100644 --- a/java-plugin/src/test/files/AvoidGettingSizeCollectionInForEachLoopIgnored.java +++ b/java-plugin/src/test/files/AvoidGettingSizeCollectionInForEachLoopIgnored.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Collection; import java.util.ArrayList; diff --git a/java-plugin/src/test/files/AvoidGettingSizeCollectionInForLoopBad.java b/java-plugin/src/test/files/AvoidGettingSizeCollectionInForLoopBad.java index 77673994c..6e661d664 100644 --- a/java-plugin/src/test/files/AvoidGettingSizeCollectionInForLoopBad.java +++ b/java-plugin/src/test/files/AvoidGettingSizeCollectionInForLoopBad.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Collection; import java.util.ArrayList; diff --git a/java-plugin/src/test/files/AvoidGettingSizeCollectionInForLoopGood.java b/java-plugin/src/test/files/AvoidGettingSizeCollectionInForLoopGood.java index ba017706f..840ace8cc 100644 --- a/java-plugin/src/test/files/AvoidGettingSizeCollectionInForLoopGood.java +++ b/java-plugin/src/test/files/AvoidGettingSizeCollectionInForLoopGood.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Collection; import java.util.ArrayList; diff --git a/java-plugin/src/test/files/AvoidGettingSizeCollectionInForLoopIgnored.java b/java-plugin/src/test/files/AvoidGettingSizeCollectionInForLoopIgnored.java index a4fa122d1..719f620c2 100644 --- a/java-plugin/src/test/files/AvoidGettingSizeCollectionInForLoopIgnored.java +++ b/java-plugin/src/test/files/AvoidGettingSizeCollectionInForLoopIgnored.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Collection; import java.util.ArrayList; diff --git a/java-plugin/src/test/files/AvoidGettingSizeCollectionInWhileLoopBad.java b/java-plugin/src/test/files/AvoidGettingSizeCollectionInWhileLoopBad.java index 759a8d6df..1dd3da003 100644 --- a/java-plugin/src/test/files/AvoidGettingSizeCollectionInWhileLoopBad.java +++ b/java-plugin/src/test/files/AvoidGettingSizeCollectionInWhileLoopBad.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Collection; import java.util.ArrayList; diff --git a/java-plugin/src/test/files/AvoidGettingSizeCollectionInWhileLoopGood.java b/java-plugin/src/test/files/AvoidGettingSizeCollectionInWhileLoopGood.java index 9181c73a8..7329fba97 100644 --- a/java-plugin/src/test/files/AvoidGettingSizeCollectionInWhileLoopGood.java +++ b/java-plugin/src/test/files/AvoidGettingSizeCollectionInWhileLoopGood.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Collection; import java.util.ArrayList; diff --git a/java-plugin/src/test/files/AvoidGettingSizeCollectionInWhileLoopIgnored.java b/java-plugin/src/test/files/AvoidGettingSizeCollectionInWhileLoopIgnored.java index 000228cdc..ca913f17d 100644 --- a/java-plugin/src/test/files/AvoidGettingSizeCollectionInWhileLoopIgnored.java +++ b/java-plugin/src/test/files/AvoidGettingSizeCollectionInWhileLoopIgnored.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Collection; import java.util.ArrayList; diff --git a/java-plugin/src/test/files/AvoidMultipleIfElseStatement.java b/java-plugin/src/test/files/AvoidMultipleIfElseStatement.java index 6b06cf825..d84630d9f 100644 --- a/java-plugin/src/test/files/AvoidMultipleIfElseStatement.java +++ b/java-plugin/src/test/files/AvoidMultipleIfElseStatement.java @@ -1,7 +1,7 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; class AvoidMultipleIfElseStatementCheck { - AvoidMultipleIfElseStatementCheck (AvoidMultipleIfElseStatementCheck mc) { + AvoidMultipleIfElseStatementCheck(AvoidMultipleIfElseStatementCheck mc) { } public void methodWithMultipleIfElseIf() { diff --git a/java-plugin/src/test/files/AvoidMultipleIfElseStatementNoIssue.java b/java-plugin/src/test/files/AvoidMultipleIfElseStatementNoIssue.java index 89c2de6d3..60d10293c 100644 --- a/java-plugin/src/test/files/AvoidMultipleIfElseStatementNoIssue.java +++ b/java-plugin/src/test/files/AvoidMultipleIfElseStatementNoIssue.java @@ -1,7 +1,7 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; class AvoidMultipleIfElseStatementNoIssueCheck { - AvoidMultipleIfElseStatementNoIssueCheck (AvoidMultipleIfElseStatementNoIssueCheck mc) { + AvoidMultipleIfElseStatementNoIssueCheck(AvoidMultipleIfElseStatementNoIssueCheck mc) { } public void methodWithOneIfElseIf() { diff --git a/java-plugin/src/test/files/AvoidRegexPatternNotStatic.java b/java-plugin/src/test/files/AvoidRegexPatternNotStatic.java index be39ca75a..8f9d69039 100644 --- a/java-plugin/src/test/files/AvoidRegexPatternNotStatic.java +++ b/java-plugin/src/test/files/AvoidRegexPatternNotStatic.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.regex.Pattern; diff --git a/java-plugin/src/test/files/AvoidSQLRequestInLoopCheck.java b/java-plugin/src/test/files/AvoidSQLRequestInLoopCheck.java index 8bfa8d6f8..6365bc956 100644 --- a/java-plugin/src/test/files/AvoidSQLRequestInLoopCheck.java +++ b/java-plugin/src/test/files/AvoidSQLRequestInLoopCheck.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.sql.Connection; import java.sql.DriverManager; @@ -6,128 +6,128 @@ import java.sql.Statement; class AvoidSQLRequestInLoopCheck { - AvoidSQLRequestInLoopCheck(AvoidSQLRequestInLoopCheck mc) { - } - - public void testWithNoLoop() { - try { - // create our mysql database connection - String myDriver = "driver"; - String myUrl = "driver"; - Class.forName(myDriver); - Connection conn = DriverManager.getConnection(myUrl, "toor", ""); - - // our SQL SELECT query. - // if you only need a few columns, specify them by name instead of using "*" - String query = "SELECT * FROM users"; - - // create the java statement - Statement st = conn.createStatement(); - ResultSet rs = st.executeQuery(query); - - // iterate through the java resultset - while (rs.next()) { - int id = rs.getInt("id"); - System.out.println(id); - } - st.close(); - } catch (Exception e) { - System.err.println("Got an exception! "); - System.err.println(e.getMessage()); - } - } - - public void testWithForLoop() { - try { - // create our mysql database connection - String myDriver = "driver"; - String myUrl = "driver"; - Class.forName(myDriver); - Connection conn = DriverManager.getConnection(myUrl, "toor", ""); - - // our SQL SELECT query. - // if you only need a few columns, specify them by name instead of using "*" - String baseQuery = "SELECT name FROM users where id = "; - - for (int i = 0; i < 20; i++) { - - // create the java statement - String query = baseQuery.concat("" + i); - Statement st = conn.createStatement(); - ResultSet rs = st.executeQuery(query); // Noncompliant - - // iterate through the java resultset - while (rs.next()) { - String name = rs.getString("name"); - System.out.println(name); - } - st.close(); - } - } catch (Exception e) { - System.err.println("Got an exception! "); - System.err.println(e.getMessage()); - } - } - - public void testWithForEachLoop() { - try { - // create our mysql database connection - String myDriver = "driver"; - String myUrl = "driver"; - Class.forName(myDriver); - Connection conn = DriverManager.getConnection(myUrl, "toor", ""); - - // our SQL SELECT query. - // if you only need a few columns, specify them by name instead of using "*" - String query = "SELECT * FROM users"; - int [] intArray = { 10, 20, 30, 40, 50 }; - for (int i : intArray) { - System.out.println(i); - // create the java statement - Statement st = conn.createStatement(); - ResultSet rs = st.executeQuery(query); // Noncompliant - - // iterate through the java resultset - while (rs.next()) { - int id = rs.getInt("id"); - System.out.println(id); - } - st.close(); - } - } catch (Exception e) { - System.err.println("Got an exception! "); - System.err.println(e.getMessage()); - } - } - - public void testWithWhileLoop() { - try { - // create our mysql database connection - String myDriver = "driver"; - String myUrl = "driver"; - Class.forName(myDriver); - Connection conn = DriverManager.getConnection(myUrl, "toor", ""); - - // our SQL SELECT query. - // if you only need a few columns, specify them by name instead of using "*" - String query = "SELECT * FROM users"; - int i = 0; - while (i < -1) { - - // create the java statement - Statement st = conn.createStatement(); - ResultSet rs = st.executeQuery(query); // Noncompliant - - // iterate through the java resultset - while (rs.next()) { - int id = rs.getInt("id"); - System.out.println(id); - } - st.close(); - } - } catch (Exception e) { - System.err.println("Got an exception! "); - System.err.println(e.getMessage()); + AvoidSQLRequestInLoopCheck(AvoidSQLRequestInLoopCheck mc) { + } + + public void testWithNoLoop() { + try { + // create our mysql database connection + String myDriver = "driver"; + String myUrl = "driver"; + Class.forName(myDriver); + Connection conn = DriverManager.getConnection(myUrl, "toor", ""); + + // our SQL SELECT query. + // if you only need a few columns, specify them by name instead of using "*" + String query = "SELECT * FROM users"; + + // create the java statement + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(query); + + // iterate through the java resultset + while (rs.next()) { + int id = rs.getInt("id"); + System.out.println(id); + } + st.close(); + } catch (Exception e) { + System.err.println("Got an exception! "); + System.err.println(e.getMessage()); + } + } + + public void testWithForLoop() { + try { + // create our mysql database connection + String myDriver = "driver"; + String myUrl = "driver"; + Class.forName(myDriver); + Connection conn = DriverManager.getConnection(myUrl, "toor", ""); + + // our SQL SELECT query. + // if you only need a few columns, specify them by name instead of using "*" + String baseQuery = "SELECT name FROM users where id = "; + + for (int i = 0; i < 20; i++) { + + // create the java statement + String query = baseQuery.concat("" + i); + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(query); // Noncompliant + + // iterate through the java resultset + while (rs.next()) { + String name = rs.getString("name"); + System.out.println(name); + } + st.close(); + } + } catch (Exception e) { + System.err.println("Got an exception! "); + System.err.println(e.getMessage()); + } + } + + public void testWithForEachLoop() { + try { + // create our mysql database connection + String myDriver = "driver"; + String myUrl = "driver"; + Class.forName(myDriver); + Connection conn = DriverManager.getConnection(myUrl, "toor", ""); + + // our SQL SELECT query. + // if you only need a few columns, specify them by name instead of using "*" + String query = "SELECT * FROM users"; + int[] intArray = {10, 20, 30, 40, 50}; + for (int i : intArray) { + System.out.println(i); + // create the java statement + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(query); // Noncompliant + + // iterate through the java resultset + while (rs.next()) { + int id = rs.getInt("id"); + System.out.println(id); + } + st.close(); + } + } catch (Exception e) { + System.err.println("Got an exception! "); + System.err.println(e.getMessage()); + } + } + + public void testWithWhileLoop() { + try { + // create our mysql database connection + String myDriver = "driver"; + String myUrl = "driver"; + Class.forName(myDriver); + Connection conn = DriverManager.getConnection(myUrl, "toor", ""); + + // our SQL SELECT query. + // if you only need a few columns, specify them by name instead of using "*" + String query = "SELECT * FROM users"; + int i = 0; + while (i < -1) { + + // create the java statement + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(query); // Noncompliant + + // iterate through the java resultset + while (rs.next()) { + int id = rs.getInt("id"); + System.out.println(id); + } + st.close(); + } + } catch (Exception e) { + System.err.println("Got an exception! "); + System.err.println(e.getMessage()); } } diff --git a/java-plugin/src/test/files/AvoidSetConstantInBatchUpdateCheck.java b/java-plugin/src/test/files/AvoidSetConstantInBatchUpdateCheck.java index 31992cb36..b597afd76 100644 --- a/java-plugin/src/test/files/AvoidSetConstantInBatchUpdateCheck.java +++ b/java-plugin/src/test/files/AvoidSetConstantInBatchUpdateCheck.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.math.BigDecimal; import java.sql.PreparedStatement; @@ -7,133 +7,143 @@ import java.util.stream.Stream; class AvoidSetConstantInBatchUpdateCheck { - - void literalSQLrequest() { //dirty call - - int x = 0; - Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); - PreparedStatement stmt=con.prepareStatement("insert into Emp values(?,?,?,?)"); - stmt.setInt(1,101); - stmt.setString(2,"Ratan"); - stmt.setBigDecimal(3, Bigdecimal.ONE); - stmt.setBigDecimal(4, BigDecimal.valueOf(x)); - stmt.setBoolean(5, Boolean.valueOf("true")); - int i=stmt.executeUpdate(); - System.out.println(i+" records inserted"); - con.close(); - } - - void batchInsertInForLoop(int[] data) { - - Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); - PreparedStatement stmt=con.prepareStatement("insert into Emp values(?,?,?,?,?,?,?,?,?,?,?)"); - for(int i=0; i smellGetAllEmployeesByIds(List ids){ + + public List smellGetAllEmployeesByIds(List ids) { List employees = new ArrayList<>(); - for (Integer id: ids) { + for (Integer id : ids) { Optional employee = employeeRepository.findById(id); // Noncompliant {{Avoid Spring repository call in loop}} if (employee.isPresent()) { employees.add(employee.get()); @@ -21,9 +21,10 @@ public List smellGetAllEmployeesByIds(List ids){ return employees; } - public class Employee {} + public class Employee { + } - public interface EmployeeRepository extends JpaRepository{ + public interface EmployeeRepository extends JpaRepository { } diff --git a/java-plugin/src/test/files/AvoidStatementForDMLQueries.java b/java-plugin/src/test/files/AvoidStatementForDMLQueries.java index 25da52e7f..1fa281fc9 100644 --- a/java-plugin/src/test/files/AvoidStatementForDMLQueries.java +++ b/java-plugin/src/test/files/AvoidStatementForDMLQueries.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.sql.Connection; import java.sql.DriverManager; @@ -6,7 +6,6 @@ import java.sql.PreparedStatement; - class AvoidStatementForDMLQueries { AvoidStatementForDMLQueries(AvoidStatementForDMLQueries mc) { } diff --git a/java-plugin/src/test/files/AvoidUsageOfStaticCollections.java b/java-plugin/src/test/files/AvoidUsageOfStaticCollections.java index ec3736106..50eb50718 100644 --- a/java-plugin/src/test/files/AvoidUsageOfStaticCollections.java +++ b/java-plugin/src/test/files/AvoidUsageOfStaticCollections.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.*; diff --git a/java-plugin/src/test/files/FreeResourcesOfAutoCloseableInterface.java b/java-plugin/src/test/files/FreeResourcesOfAutoCloseableInterface.java index b29dfec4d..e2ca15225 100644 --- a/java-plugin/src/test/files/FreeResourcesOfAutoCloseableInterface.java +++ b/java-plugin/src/test/files/FreeResourcesOfAutoCloseableInterface.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.io.*; @@ -11,8 +11,7 @@ public void foo1() { String fileName = "./FreeResourcesOfAutoCloseableInterface.java"; try (FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr)) { - } - catch (IOException e) { + } catch (IOException e) { System.err.println(e.getMessage()); } } diff --git a/java-plugin/src/test/files/GoodUsageOfStaticCollections.java b/java-plugin/src/test/files/GoodUsageOfStaticCollections.java index bab0372e2..8f8e55c71 100644 --- a/java-plugin/src/test/files/GoodUsageOfStaticCollections.java +++ b/java-plugin/src/test/files/GoodUsageOfStaticCollections.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.*; diff --git a/java-plugin/src/test/files/GoodWayConcatenateStringsLoop.java b/java-plugin/src/test/files/GoodWayConcatenateStringsLoop.java index d6b9cebb2..b4218d0d7 100644 --- a/java-plugin/src/test/files/GoodWayConcatenateStringsLoop.java +++ b/java-plugin/src/test/files/GoodWayConcatenateStringsLoop.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.utils; +package fr.greencodeinitiative.java.utils; public class GoodWayConcatenateStringsLoop { diff --git a/java-plugin/src/test/files/InitializeBufferWithAppropriateSize.java b/java-plugin/src/test/files/InitializeBufferWithAppropriateSize.java index 664575af0..4c39ca89b 100644 --- a/java-plugin/src/test/files/InitializeBufferWithAppropriateSize.java +++ b/java-plugin/src/test/files/InitializeBufferWithAppropriateSize.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.sql.Connection; import java.sql.DriverManager; diff --git a/java-plugin/src/test/files/OptimizeReadFileExceptionCheck.java b/java-plugin/src/test/files/OptimizeReadFileExceptionCheck.java index 4aeccf549..e22239a6e 100644 --- a/java-plugin/src/test/files/OptimizeReadFileExceptionCheck.java +++ b/java-plugin/src/test/files/OptimizeReadFileExceptionCheck.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Arrays; import java.util.List; @@ -6,6 +6,7 @@ class ReadFile { ReadFile(ReadFile readFile) { } + public void readPreferences(String filename) { //... InputStream in = null; diff --git a/java-plugin/src/test/files/OptimizeReadFileExceptionCheck2.java b/java-plugin/src/test/files/OptimizeReadFileExceptionCheck2.java index c0952d661..9410346c9 100644 --- a/java-plugin/src/test/files/OptimizeReadFileExceptionCheck2.java +++ b/java-plugin/src/test/files/OptimizeReadFileExceptionCheck2.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Arrays; import java.util.List; diff --git a/java-plugin/src/test/files/OptimizeReadFileExceptionCheck3.java b/java-plugin/src/test/files/OptimizeReadFileExceptionCheck3.java index b56a71fcc..8f76f8715 100644 --- a/java-plugin/src/test/files/OptimizeReadFileExceptionCheck3.java +++ b/java-plugin/src/test/files/OptimizeReadFileExceptionCheck3.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Arrays; import java.util.List; diff --git a/java-plugin/src/test/files/OptimizeReadFileExceptionCheck4.java b/java-plugin/src/test/files/OptimizeReadFileExceptionCheck4.java index 3d0255fd2..d65198270 100644 --- a/java-plugin/src/test/files/OptimizeReadFileExceptionCheck4.java +++ b/java-plugin/src/test/files/OptimizeReadFileExceptionCheck4.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Arrays; import java.util.List; diff --git a/java-plugin/src/test/files/OptimizeReadFileExceptionCheck5.java b/java-plugin/src/test/files/OptimizeReadFileExceptionCheck5.java index 09a908b15..4c9cd821a 100644 --- a/java-plugin/src/test/files/OptimizeReadFileExceptionCheck5.java +++ b/java-plugin/src/test/files/OptimizeReadFileExceptionCheck5.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Arrays; import java.util.List; diff --git a/java-plugin/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheck.java b/java-plugin/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheck.java index 7402e44b9..3ed0cf70a 100644 --- a/java-plugin/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheck.java +++ b/java-plugin/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheck.java @@ -1,78 +1,78 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; class UnnecessarilyAssignValuesToVariablesTestCheck { - UnnecessarilyAssignValuesToVariablesTestCheck(UnnecessarilyAssignValuesToVariablesTestCheck mc) { - } + UnnecessarilyAssignValuesToVariablesTestCheck(UnnecessarilyAssignValuesToVariablesTestCheck mc) { + } - public int testSwitchCase() throws Exception { - int variableFor = 5; - int variableIf = 5; - int variableWhile = 5; - int variableExp = 5; - int variableReturn = 5; - int variableCLass = 5; - int [] intArray = { 10, 20, 30, 40, 50 }; - - Exception variableException = new Exception("message"); - int variableNotUse = 5; // Noncompliant - - - variableNotUse = 10; - for (variableFor = 0;variableFor < 5;++variableFor) { - System.out.println(variableFor); - } - - for (int ia : intArray) { - System.out.println((char) ia); - } - - if (variableIf > 10) { - System.out.println(variableIf); - } - - while (variableWhile > 10) { - System.out.println(variableWhile); - } - - variableExp += 1; - variableNotUse = variableExp; - TestClass testClass = new TestClass(variableCLass); - if (testClass.isTrue()) { - throw variableException; - } - return variableReturn; - } + public int testSwitchCase() throws Exception { + int variableFor = 5; + int variableIf = 5; + int variableWhile = 5; + int variableExp = 5; + int variableReturn = 5; + int variableCLass = 5; + int[] intArray = {10, 20, 30, 40, 50}; - private class TestClass { - TestClass(int i) { - ++i; - } + Exception variableException = new Exception("message"); + int variableNotUse = 5; // Noncompliant - public boolean isTrue() { - return true; - } - } + variableNotUse = 10; + for (variableFor = 0; variableFor < 5; ++variableFor) { + System.out.println(variableFor); + } - private int getIntValue() { - return 3; - } + for (int ia : intArray) { + System.out.println((char) ia); + } - public int testNonCompliantReturn() { - int i = getIntValue(); // Noncompliant - return i; - } + if (variableIf > 10) { + System.out.println(variableIf); + } - public int testCompliantReturn() { - return getIntValue(); - } + while (variableWhile > 10) { + System.out.println(variableWhile); + } - public void testNonCompliantThrow() throws Exception { - Exception exception = new Exception("dummy"); // Noncompliant - throw exception; - } + variableExp += 1; + variableNotUse = variableExp; + TestClass testClass = new TestClass(variableCLass); + if (testClass.isTrue()) { + throw variableException; + } + return variableReturn; + } + + private class TestClass { + TestClass(int i) { + ++i; + } + + public boolean isTrue() { + return true; + } + } + + + private int getIntValue() { + return 3; + } + + public int testNonCompliantReturn() { + int i = getIntValue(); // Noncompliant + return i; + } + + public int testCompliantReturn() { + return getIntValue(); + } + + public void testNonCompliantThrow() throws Exception { + Exception exception = new Exception("dummy"); // Noncompliant + throw exception; + } - public void testCompliantThrow() throws Exception { - throw new Exception("dummy"); + public void testCompliantThrow() throws Exception { + throw new Exception("dummy"); } } \ No newline at end of file diff --git a/java-plugin/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheckWithEmptyReturn.java b/java-plugin/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheckWithEmptyReturn.java index d76d85631..f732e4945 100644 --- a/java-plugin/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheckWithEmptyReturn.java +++ b/java-plugin/src/test/files/UnnecessarilyAssignValuesToVariablesTestCheckWithEmptyReturn.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; class UnnecessarilyAssignValuesToVariablesTestCheck { UnnecessarilyAssignValuesToVariablesTestCheck(UnnecessarilyAssignValuesToVariablesTestCheck mc) { diff --git a/java-plugin/src/test/files/UseCorrectForLoopCheck.java b/java-plugin/src/test/files/UseCorrectForLoopCheck.java index 15544e446..669f058f7 100644 --- a/java-plugin/src/test/files/UseCorrectForLoopCheck.java +++ b/java-plugin/src/test/files/UseCorrectForLoopCheck.java @@ -1,24 +1,24 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.Arrays; import java.util.List; class UseCorrectForLoopCheck { - UseCorrectForLoopCheck(UseCorrectForLoopCheck mc) { - } + UseCorrectForLoopCheck(UseCorrectForLoopCheck mc) { + } - private final Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; - private final List intList = Arrays.asList(intArray); + private final Integer[] intArray = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + private final List intList = Arrays.asList(intArray); - public void testForEachLoop() { - int dummy = 0; - for (Integer i : intArray) { // Noncompliant - dummy += i; - } + public void testForEachLoop() { + int dummy = 0; + for (Integer i : intArray) { // Noncompliant + dummy += i; + } - for (Integer i : intList) { - dummy += i; - } - System.out.println(dummy); + for (Integer i : intList) { + dummy += i; + } + System.out.println(dummy); } } \ No newline at end of file diff --git a/java-plugin/src/test/files/ValidRegexPattern.java b/java-plugin/src/test/files/ValidRegexPattern.java index 958c18eea..5ed3652f4 100644 --- a/java-plugin/src/test/files/ValidRegexPattern.java +++ b/java-plugin/src/test/files/ValidRegexPattern.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.regex.Pattern; diff --git a/java-plugin/src/test/files/ValidRegexPattern2.java b/java-plugin/src/test/files/ValidRegexPattern2.java index 56b624fbd..d6d9efd71 100644 --- a/java-plugin/src/test/files/ValidRegexPattern2.java +++ b/java-plugin/src/test/files/ValidRegexPattern2.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.regex.Pattern; diff --git a/java-plugin/src/test/files/ValidRegexPattern3.java b/java-plugin/src/test/files/ValidRegexPattern3.java index 6c1388abb..e19073454 100644 --- a/java-plugin/src/test/files/ValidRegexPattern3.java +++ b/java-plugin/src/test/files/ValidRegexPattern3.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import java.util.regex.Pattern; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/ArrayCopyCheckTest.java b/java-plugin/src/test/java/fr/cnumr/java/checks/ArrayCopyCheckTest.java deleted file mode 100644 index 9d2577ef2..000000000 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/ArrayCopyCheckTest.java +++ /dev/null @@ -1,19 +0,0 @@ -package fr.cnumr.java.checks; - -import org.junit.jupiter.api.Test; -import org.sonar.java.checks.verifier.CheckVerifier; - -class ArrayCopyCheckTest { - - /** - * @formatter:off - */ - @Test - void test() { - CheckVerifier.newVerifier() - .onFile("src/test/files/ArrayCopyCheck.java") - .withCheck(new ArrayCopyCheck()) - .verifyIssues(); - } - -} \ No newline at end of file diff --git a/java-plugin/src/test/java/fr/cnumr/java/utils/FilesUtils.java b/java-plugin/src/test/java/fr/cnumr/java/utils/FilesUtils.java deleted file mode 100644 index 40ddfb038..000000000 --- a/java-plugin/src/test/java/fr/cnumr/java/utils/FilesUtils.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) 2012-2021 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package fr.cnumr.java.utils; - -import java.io.File; -import java.io.IOException; -import java.nio.file.FileVisitResult; -import java.nio.file.FileVisitor; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; - -/** - * Duplicates org.sonar.java.checks.verifier.FilesUtils to locate test jars within the custom-rules plugin - */ -public class FilesUtils { - - private FilesUtils() { - } - - /** - * Default location of the jars/zips to be taken into account when performing the analysis. - */ - private static final String DEFAULT_TEST_JARS_DIRECTORY = "target/test-jars"; - - public static List getClassPath(String jarsDirectory) { - List classpath = new LinkedList<>(); - Path testJars = Paths.get(jarsDirectory); - if (testJars.toFile().exists()) { - classpath = getFilesRecursively(testJars, "jar", "zip"); - } else if (!DEFAULT_TEST_JARS_DIRECTORY.equals(jarsDirectory)) { - throw new AssertionError("The directory to be used to extend class path does not exists (" - + testJars.toAbsolutePath() - + ")."); - } - classpath.add(new File("target/test-classes")); - return classpath; - } - - private static List getFilesRecursively(Path root, String... extensions) { - final List files = new ArrayList<>(); - - FileVisitor visitor = new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path filePath, BasicFileAttributes attrs) { - for (String extension : extensions) { - if (filePath.toString().endsWith("." - + extension)) { - files.add(filePath.toFile()); - break; - } - } - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFileFailed(Path file, IOException exc) { - return FileVisitResult.CONTINUE; - } - }; - - try { - Files.walkFileTree(root, visitor); - } catch (IOException e) { - // we already ignore errors in the visitor - } - - return files; - } - -} diff --git a/java-plugin/src/test/java/fr/cnumr/java/JavaCheckRegistrarTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java similarity index 97% rename from java-plugin/src/test/java/fr/cnumr/java/JavaCheckRegistrarTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java index b9a1f63b4..3225b1a56 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/JavaCheckRegistrarTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.java; +package fr.greencodeinitiative.java; import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; diff --git a/java-plugin/src/test/java/fr/cnumr/java/JavaPluginTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaPluginTest.java similarity index 98% rename from java-plugin/src/test/java/fr/cnumr/java/JavaPluginTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/JavaPluginTest.java index 4708adbbb..6178fc0d0 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/JavaPluginTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaPluginTest.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.java; +package fr.greencodeinitiative.java; import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; diff --git a/java-plugin/src/test/java/fr/cnumr/java/JavaRulesDefinitionTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java similarity index 98% rename from java-plugin/src/test/java/fr/cnumr/java/JavaRulesDefinitionTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java index 8fd4999f1..08244a45a 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/JavaRulesDefinitionTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.java; +package fr.greencodeinitiative.java; import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ArrayCopyCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ArrayCopyCheckTest.java new file mode 100644 index 000000000..e83a49e2f --- /dev/null +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/ArrayCopyCheckTest.java @@ -0,0 +1,19 @@ +package fr.greencodeinitiative.java.checks; + +import org.junit.jupiter.api.Test; +import org.sonar.java.checks.verifier.CheckVerifier; + +class ArrayCopyCheckTest { + + /** + * @formatter:off + */ + @Test + void test() { + CheckVerifier.newVerifier() + .onFile("src/test/files/ArrayCopyCheck.java") + .withCheck(new ArrayCopyCheck()) + .verifyIssues(); + } + +} \ No newline at end of file diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidConcatenateStringsInLoopTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoopTest.java similarity index 93% rename from java-plugin/src/test/java/fr/cnumr/java/checks/AvoidConcatenateStringsInLoopTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoopTest.java index 112503101..d5e3e6fcb 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidConcatenateStringsInLoopTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoopTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidFullSQLRequestCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidFullSQLRequestCheckTest.java similarity index 89% rename from java-plugin/src/test/java/fr/cnumr/java/checks/AvoidFullSQLRequestCheckTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidFullSQLRequestCheckTest.java index 950b827c7..516f31fec 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidFullSQLRequestCheckTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidFullSQLRequestCheckTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidGettingSizeCollectionInLoopTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidGettingSizeCollectionInLoopTest.java similarity index 97% rename from java-plugin/src/test/java/fr/cnumr/java/checks/AvoidGettingSizeCollectionInLoopTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidGettingSizeCollectionInLoopTest.java index 88eacdc16..4a0bad7f8 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidGettingSizeCollectionInLoopTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidGettingSizeCollectionInLoopTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidMultipleIfElseStatementTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatementTest.java similarity index 93% rename from java-plugin/src/test/java/fr/cnumr/java/checks/AvoidMultipleIfElseStatementTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatementTest.java index 99227ffc4..84a7e5f32 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidMultipleIfElseStatementTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatementTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidRegexPatternNotStaticTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStaticTest.java similarity index 94% rename from java-plugin/src/test/java/fr/cnumr/java/checks/AvoidRegexPatternNotStaticTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStaticTest.java index ba63c0646..a3c8ba912 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidRegexPatternNotStaticTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStaticTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidSQLRequestInLoopCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoopCheckTest.java similarity index 89% rename from java-plugin/src/test/java/fr/cnumr/java/checks/AvoidSQLRequestInLoopCheckTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoopCheckTest.java index 89538c934..54f5f4a53 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidSQLRequestInLoopCheckTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoopCheckTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidSetConstantInBatchInsertTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchInsertTest.java similarity index 89% rename from java-plugin/src/test/java/fr/cnumr/java/checks/AvoidSetConstantInBatchInsertTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchInsertTest.java index 9644ad136..73a7e58d3 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidSetConstantInBatchInsertTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchInsertTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java similarity index 83% rename from java-plugin/src/test/java/fr/cnumr/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java index ceeffae91..ce5dfc36e 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java @@ -1,10 +1,9 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; +import fr.greencodeinitiative.java.utils.FilesUtils; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; -import fr.cnumr.java.utils.FilesUtils; - class AvoidSpringRepositoryCallInLoopCheckTest { @Test diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidStatementForDMLQueriesTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueriesTest.java similarity index 89% rename from java-plugin/src/test/java/fr/cnumr/java/checks/AvoidStatementForDMLQueriesTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueriesTest.java index 8b56d3aa6..1a8803440 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidStatementForDMLQueriesTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueriesTest.java @@ -1,4 +1,5 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; + import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidUsageOfStaticCollectionsTests.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollectionsTests.java similarity index 93% rename from java-plugin/src/test/java/fr/cnumr/java/checks/AvoidUsageOfStaticCollectionsTests.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollectionsTests.java index 2fac72c43..e72d271de 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidUsageOfStaticCollectionsTests.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollectionsTests.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java similarity index 89% rename from java-plugin/src/test/java/fr/cnumr/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java index fa33b2e22..c8dbeb26a 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java similarity index 94% rename from java-plugin/src/test/java/fr/cnumr/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java index 3cc25db25..2736a12e3 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/IncrementCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java similarity index 88% rename from java-plugin/src/test/java/fr/cnumr/java/checks/IncrementCheckTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java index e14538cd9..4044341cd 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/IncrementCheckTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/IncrementCheckTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/InitializeBufferWithAppropriateSizeTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSizeTest.java similarity index 90% rename from java-plugin/src/test/java/fr/cnumr/java/checks/InitializeBufferWithAppropriateSizeTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSizeTest.java index b9f09b68e..5096eee2f 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/InitializeBufferWithAppropriateSizeTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSizeTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java similarity index 89% rename from java-plugin/src/test/java/fr/cnumr/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java index 62b956e76..f2876bb1d 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/OptimizeReadFileExceptionCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptionCheckTest.java similarity index 96% rename from java-plugin/src/test/java/fr/cnumr/java/checks/OptimizeReadFileExceptionCheckTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptionCheckTest.java index a04b92384..5dffac408 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/OptimizeReadFileExceptionCheckTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptionCheckTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/UnnecessarilyAssignValuesToVariablesTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariablesTest.java similarity index 94% rename from java-plugin/src/test/java/fr/cnumr/java/checks/UnnecessarilyAssignValuesToVariablesTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariablesTest.java index a26bbca9b..efcb2aa1b 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/UnnecessarilyAssignValuesToVariablesTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariablesTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/cnumr/java/checks/UseCorrectLoopCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/UseCorrectLoopCheckTest.java similarity index 88% rename from java-plugin/src/test/java/fr/cnumr/java/checks/UseCorrectLoopCheckTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/checks/UseCorrectLoopCheckTest.java index 09af6c477..f6add5776 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/checks/UseCorrectLoopCheckTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/UseCorrectLoopCheckTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.java.checks; +package fr.greencodeinitiative.java.checks; import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/utils/FilesUtils.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/utils/FilesUtils.java new file mode 100644 index 000000000..37be25da4 --- /dev/null +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/utils/FilesUtils.java @@ -0,0 +1,93 @@ +/* + * SonarQube Java + * Copyright (C) 2012-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package fr.greencodeinitiative.java.utils; + +import java.io.File; +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.FileVisitor; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +/** + * Duplicates org.sonar.java.checks.verifier.FilesUtils to locate test jars within the custom-rules plugin + */ +public class FilesUtils { + + private FilesUtils() { + } + + /** + * Default location of the jars/zips to be taken into account when performing the analysis. + */ + private static final String DEFAULT_TEST_JARS_DIRECTORY = "target/test-jars"; + + public static List getClassPath(String jarsDirectory) { + List classpath = new LinkedList<>(); + Path testJars = Paths.get(jarsDirectory); + if (testJars.toFile().exists()) { + classpath = getFilesRecursively(testJars, "jar", "zip"); + } else if (!DEFAULT_TEST_JARS_DIRECTORY.equals(jarsDirectory)) { + throw new AssertionError("The directory to be used to extend class path does not exists (" + + testJars.toAbsolutePath() + + ")."); + } + classpath.add(new File("target/test-classes")); + return classpath; + } + + private static List getFilesRecursively(Path root, String... extensions) { + final List files = new ArrayList<>(); + + FileVisitor visitor = new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path filePath, BasicFileAttributes attrs) { + for (String extension : extensions) { + if (filePath.toString().endsWith("." + + extension)) { + files.add(filePath.toFile()); + break; + } + } + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFileFailed(Path file, IOException exc) { + return FileVisitResult.CONTINUE; + } + }; + + try { + Files.walkFileTree(root, visitor); + } catch (IOException e) { + // we already ignore errors in the visitor + } + + return files; + } + +} diff --git a/java-plugin/src/test/java/fr/cnumr/java/utils/StringUtilsTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/utils/StringUtilsTest.java similarity index 64% rename from java-plugin/src/test/java/fr/cnumr/java/utils/StringUtilsTest.java rename to java-plugin/src/test/java/fr/greencodeinitiative/java/utils/StringUtilsTest.java index ced6cce2b..c0eee7c3c 100644 --- a/java-plugin/src/test/java/fr/cnumr/java/utils/StringUtilsTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/utils/StringUtilsTest.java @@ -17,28 +17,27 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.java.utils; - -import org.junit.jupiter.api.Test; +package fr.greencodeinitiative.java.utils; import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.Test; class StringUtilsTest { - @Test - void spaces() { - assertThat(StringUtils.spaces(5)) - .hasSize(5) - .containsOnlyWhitespaces(); - } + @Test + void spaces() { + assertThat(StringUtils.spaces(5)) + .hasSize(5) + .containsOnlyWhitespaces(); + } - @Test - void isNotEmpty() { - assertThat(StringUtils.isNotEmpty(null)).isFalse(); - assertThat(StringUtils.isNotEmpty("")).isFalse(); - assertThat(StringUtils.isNotEmpty(" ")).isTrue(); - assertThat(StringUtils.isNotEmpty("bob")).isTrue(); - assertThat(StringUtils.isNotEmpty(" bob ")).isTrue(); - } + @Test + void isNotEmpty() { + assertThat(StringUtils.isNotEmpty(null)).isFalse(); + assertThat(StringUtils.isNotEmpty("")).isFalse(); + assertThat(StringUtils.isNotEmpty(" ")).isTrue(); + assertThat(StringUtils.isNotEmpty("bob")).isTrue(); + assertThat(StringUtils.isNotEmpty(" bob ")).isTrue(); + } } diff --git a/php-plugin/pom.xml b/php-plugin/pom.xml index 40e38d008..7c7e4611e 100644 --- a/php-plugin/pom.xml +++ b/php-plugin/pom.xml @@ -53,7 +53,7 @@ ${project.artifactId} ${project.name} - fr.cnumr.php.PHPPlugin + fr.greencodeinitiative.php.PHPPlugin true ${sonarqube.version} php diff --git a/php-plugin/src/main/java/fr/cnumr/php/checks/AvoidFullSQLRequestCheck.java b/php-plugin/src/main/java/fr/cnumr/php/checks/AvoidFullSQLRequestCheck.java deleted file mode 100644 index b88a08dfc..000000000 --- a/php-plugin/src/main/java/fr/cnumr/php/checks/AvoidFullSQLRequestCheck.java +++ /dev/null @@ -1,39 +0,0 @@ -package fr.cnumr.php.checks; - -import org.sonar.check.Priority; -import org.sonar.check.Rule; -import org.sonar.plugins.php.api.tree.Tree; -import org.sonar.plugins.php.api.tree.Tree.Kind; -import org.sonar.plugins.php.api.tree.expression.LiteralTree; -import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; - -import java.util.Arrays; -import java.util.List; - -@Rule( - key = "S74", - name = "Developpement", - description = AvoidFullSQLRequestCheck.ERROR_MESSAGE, - priority = Priority.MINOR, - tags = {"bug"} -) - -public class AvoidFullSQLRequestCheck extends PHPSubscriptionCheck { - - public static final String ERROR_MESSAGE = "Don't use the query SELECT * FROM"; - private static final String REGEXPSELECTFROM = "(?i).*select.*\\*.*from.*"; - - @Override - public List nodesToVisit() { - return Arrays.asList(Kind.REGULAR_STRING_LITERAL); - } - - @Override - public void visitNode(Tree tree) { - - LiteralTree literal = (LiteralTree) tree; - if(literal.value().matches(REGEXPSELECTFROM)) - context().newIssue(this, tree, ERROR_MESSAGE); - - } -} diff --git a/php-plugin/src/main/java/fr/cnumr/php/PHPPlugin.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/PHPPlugin.java similarity index 96% rename from php-plugin/src/main/java/fr/cnumr/php/PHPPlugin.java rename to php-plugin/src/main/java/fr/greencodeinitiative/php/PHPPlugin.java index f3cbb5254..47326df34 100644 --- a/php-plugin/src/main/java/fr/cnumr/php/PHPPlugin.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/PHPPlugin.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.php; +package fr.greencodeinitiative.php; import org.sonar.api.Plugin; diff --git a/php-plugin/src/main/java/fr/cnumr/php/PhpRulesDefinition.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/PhpRulesDefinition.java similarity index 92% rename from php-plugin/src/main/java/fr/cnumr/php/PhpRulesDefinition.java rename to php-plugin/src/main/java/fr/greencodeinitiative/php/PhpRulesDefinition.java index 54a8e90a6..f8f44c7ce 100644 --- a/php-plugin/src/main/java/fr/cnumr/php/PhpRulesDefinition.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/PhpRulesDefinition.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.php; +package fr.greencodeinitiative.php; import java.util.ArrayList; @@ -40,8 +40,8 @@ public class PhpRulesDefinition implements RulesDefinition { public static final String LANGUAGE = "php"; public static final String NAME = "Collectif Conception Numérique Responsable"; - public static final String RESOURCE_BASE_PATH = "fr/cnumr/l10n/php/rules/custom"; - public static final String REPOSITORY_KEY = "cnumr-php"; + public static final String RESOURCE_BASE_PATH = "fr/greencodeinitiative/l10n/php/rules/custom"; + public static final String REPOSITORY_KEY = "greencodeinitiative-php"; private static final Set RULE_TEMPLATES_KEY = Collections.emptySet(); @Override diff --git a/php-plugin/src/main/java/fr/cnumr/php/RulesList.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/RulesList.java similarity index 65% rename from php-plugin/src/main/java/fr/cnumr/php/RulesList.java rename to php-plugin/src/main/java/fr/greencodeinitiative/php/RulesList.java index dee7ecc40..ca98b6013 100644 --- a/php-plugin/src/main/java/fr/cnumr/php/RulesList.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/RulesList.java @@ -17,15 +17,20 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.php; - -import fr.cnumr.php.checks.*; +package fr.greencodeinitiative.php; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import fr.greencodeinitiative.php.checks.AvoidDoubleQuoteCheck; +import fr.greencodeinitiative.php.checks.AvoidFullSQLRequestCheck; +import fr.greencodeinitiative.php.checks.AvoidSQLRequestInLoopCheck; +import fr.greencodeinitiative.php.checks.AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements; +import fr.greencodeinitiative.php.checks.IncrementCheck; +import fr.greencodeinitiative.php.checks.NoFunctionCallWhenDeclaringForLoop; + public final class RulesList { private RulesList() { @@ -40,12 +45,12 @@ public static List> getChecks() { public static List> getPhpChecks() { return Collections.unmodifiableList(Arrays.asList( - IncrementCheck.class, - AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.class, - AvoidDoubleQuoteCheck.class, - AvoidFullSQLRequestCheck.class, - AvoidSQLRequestInLoopCheck.class, - NoFunctionCallWhenDeclaringForLoop.class + IncrementCheck.class, + AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.class, + AvoidDoubleQuoteCheck.class, + AvoidFullSQLRequestCheck.class, + AvoidSQLRequestInLoopCheck.class, + NoFunctionCallWhenDeclaringForLoop.class )); } diff --git a/php-plugin/src/main/java/fr/cnumr/php/checks/AvoidDoubleQuoteCheck.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidDoubleQuoteCheck.java similarity index 91% rename from php-plugin/src/main/java/fr/cnumr/php/checks/AvoidDoubleQuoteCheck.java rename to php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidDoubleQuoteCheck.java index 0c3fc4212..ba6b85161 100644 --- a/php-plugin/src/main/java/fr/cnumr/php/checks/AvoidDoubleQuoteCheck.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidDoubleQuoteCheck.java @@ -1,4 +1,11 @@ -package fr.cnumr.php.checks; +package fr.greencodeinitiative.php.checks; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.sonar.check.Priority; import org.sonar.check.Rule; @@ -6,8 +13,6 @@ import org.sonar.plugins.php.api.tree.expression.LiteralTree; import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; -import java.util.*; - @Rule( key = "S66", name = "Developpement", diff --git a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidFullSQLRequestCheck.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidFullSQLRequestCheck.java new file mode 100644 index 000000000..cb3cf2252 --- /dev/null +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidFullSQLRequestCheck.java @@ -0,0 +1,39 @@ +package fr.greencodeinitiative.php.checks; + +import java.util.Arrays; +import java.util.List; + +import org.sonar.check.Priority; +import org.sonar.check.Rule; +import org.sonar.plugins.php.api.tree.Tree; +import org.sonar.plugins.php.api.tree.Tree.Kind; +import org.sonar.plugins.php.api.tree.expression.LiteralTree; +import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; + +@Rule( + key = "S74", + name = "Developpement", + description = AvoidFullSQLRequestCheck.ERROR_MESSAGE, + priority = Priority.MINOR, + tags = {"bug"} +) + +public class AvoidFullSQLRequestCheck extends PHPSubscriptionCheck { + + public static final String ERROR_MESSAGE = "Don't use the query SELECT * FROM"; + private static final String REGEXPSELECTFROM = "(?i).*select.*\\*.*from.*"; + + @Override + public List nodesToVisit() { + return Arrays.asList(Kind.REGULAR_STRING_LITERAL); + } + + @Override + public void visitNode(Tree tree) { + + LiteralTree literal = (LiteralTree) tree; + if (literal.value().matches(REGEXPSELECTFROM)) + context().newIssue(this, tree, ERROR_MESSAGE); + + } +} diff --git a/php-plugin/src/main/java/fr/cnumr/php/checks/AvoidSQLRequestInLoopCheck.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidSQLRequestInLoopCheck.java similarity index 92% rename from php-plugin/src/main/java/fr/cnumr/php/checks/AvoidSQLRequestInLoopCheck.java rename to php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidSQLRequestInLoopCheck.java index 163d14bf9..b06e5f0db 100644 --- a/php-plugin/src/main/java/fr/cnumr/php/checks/AvoidSQLRequestInLoopCheck.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidSQLRequestInLoopCheck.java @@ -1,4 +1,4 @@ -package fr.cnumr.php.checks; +package fr.greencodeinitiative.php.checks; import java.util.Arrays; import java.util.List; @@ -18,10 +18,10 @@ import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; @Rule( - key = "S72", - name = "Developpement", description = AvoidSQLRequestInLoopCheck.ERROR_MESSAGE, - priority = Priority.MINOR, - tags = { "bug" } + key = "S72", + name = "Developpement", description = AvoidSQLRequestInLoopCheck.ERROR_MESSAGE, + priority = Priority.MINOR, + tags = {"bug"} ) public class AvoidSQLRequestInLoopCheck extends PHPSubscriptionCheck { diff --git a/php-plugin/src/main/java/fr/cnumr/php/checks/AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.java similarity index 95% rename from php-plugin/src/main/java/fr/cnumr/php/checks/AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.java rename to php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.java index 739206d6c..147e86ff9 100644 --- a/php-plugin/src/main/java/fr/cnumr/php/checks/AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.java @@ -1,13 +1,13 @@ -package fr.cnumr.php.checks; +package fr.greencodeinitiative.php.checks; + +import java.util.Collections; +import java.util.List; import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.php.api.tree.Tree; import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; -import java.util.Collections; -import java.util.List; - @Rule( key = "S34", name = "Developpement", diff --git a/php-plugin/src/main/java/fr/cnumr/php/checks/AvoidUsingGlobalVariablesCheck.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidUsingGlobalVariablesCheck.java similarity index 84% rename from php-plugin/src/main/java/fr/cnumr/php/checks/AvoidUsingGlobalVariablesCheck.java rename to php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidUsingGlobalVariablesCheck.java index a622c1235..5a1d6bae6 100644 --- a/php-plugin/src/main/java/fr/cnumr/php/checks/AvoidUsingGlobalVariablesCheck.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidUsingGlobalVariablesCheck.java @@ -1,12 +1,12 @@ -package fr.cnumr.php.checks; +package fr.greencodeinitiative.php.checks; + +import java.util.regex.Pattern; import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.php.api.tree.declaration.FunctionDeclarationTree; import org.sonar.plugins.php.api.visitors.PHPVisitorCheck; -import java.util.regex.Pattern; - @Rule( key = "D4", name = "Developpement", @@ -20,13 +20,15 @@ public class AvoidUsingGlobalVariablesCheck extends PHPVisitorCheck { public static final String GLOBALS_PATTERN = "^.*(global \\$|\\$GLOBALS).*$"; private Pattern pattern; String format = GLOBALS_PATTERN; + @Override public void init() { - pattern = Pattern.compile(format,Pattern.CASE_INSENSITIVE); + pattern = Pattern.compile(format, Pattern.CASE_INSENSITIVE); } + @Override public void visitFunctionDeclaration(FunctionDeclarationTree tree) { - if(pattern.matcher(tree.body().toString()).matches()){ + if (pattern.matcher(tree.body().toString()).matches()) { context().newIssue(this, tree, String.format(ERROR_MESSAGE, tree.body().toString())); } } diff --git a/php-plugin/src/main/java/fr/cnumr/php/checks/IncrementCheck.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/IncrementCheck.java similarity index 97% rename from php-plugin/src/main/java/fr/cnumr/php/checks/IncrementCheck.java rename to php-plugin/src/main/java/fr/greencodeinitiative/php/checks/IncrementCheck.java index 323ed9385..e1bb5c137 100644 --- a/php-plugin/src/main/java/fr/cnumr/php/checks/IncrementCheck.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/IncrementCheck.java @@ -17,7 +17,10 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.php.checks; +package fr.greencodeinitiative.php.checks; + +import java.util.Collections; +import java.util.List; import org.sonar.check.Priority; import org.sonar.check.Rule; @@ -25,9 +28,6 @@ import org.sonar.plugins.php.api.tree.Tree.Kind; import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; -import java.util.Collections; -import java.util.List; - @Rule( key = "S67", name = "Developpement", diff --git a/php-plugin/src/main/java/fr/cnumr/php/checks/NoFunctionCallWhenDeclaringForLoop.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/NoFunctionCallWhenDeclaringForLoop.java similarity index 97% rename from php-plugin/src/main/java/fr/cnumr/php/checks/NoFunctionCallWhenDeclaringForLoop.java rename to php-plugin/src/main/java/fr/greencodeinitiative/php/checks/NoFunctionCallWhenDeclaringForLoop.java index ed06ac9c6..4044456e0 100644 --- a/php-plugin/src/main/java/fr/cnumr/php/checks/NoFunctionCallWhenDeclaringForLoop.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/NoFunctionCallWhenDeclaringForLoop.java @@ -1,4 +1,7 @@ -package fr.cnumr.php.checks; +package fr.greencodeinitiative.php.checks; + +import java.util.Collections; +import java.util.List; import org.sonar.check.Priority; import org.sonar.check.Rule; @@ -10,9 +13,6 @@ import org.sonar.plugins.php.api.tree.statement.ForStatementTree; import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; -import java.util.Collections; -import java.util.List; - @Rule( key = "S69", name = "Developpement", diff --git a/php-plugin/src/main/java/fr/cnumr/php/checks/UseOfMethodsForBasicOperations.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/UseOfMethodsForBasicOperations.java similarity index 82% rename from php-plugin/src/main/java/fr/cnumr/php/checks/UseOfMethodsForBasicOperations.java rename to php-plugin/src/main/java/fr/greencodeinitiative/php/checks/UseOfMethodsForBasicOperations.java index e843ee486..d1168289a 100644 --- a/php-plugin/src/main/java/fr/cnumr/php/checks/UseOfMethodsForBasicOperations.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/UseOfMethodsForBasicOperations.java @@ -1,4 +1,10 @@ -package fr.cnumr.php.checks; +package fr.greencodeinitiative.php.checks; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; import org.sonar.check.Priority; import org.sonar.check.Rule; @@ -8,20 +14,16 @@ import org.sonar.plugins.php.api.tree.declaration.ClassMemberTree; import org.sonar.plugins.php.api.tree.declaration.MethodDeclarationTree; import org.sonar.plugins.php.api.tree.expression.FunctionCallTree; -import org.sonar.plugins.php.api.tree.statement.*; +import org.sonar.plugins.php.api.tree.statement.StatementTree; import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; -import java.util.*; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.stream.Collectors; - @Rule( key = "D2", name = "Developpement", description = UseOfMethodsForBasicOperations.ERROR_MESSAGE, priority = Priority.MINOR, tags = {"bug"}) -public class UseOfMethodsForBasicOperations extends PHPSubscriptionCheck { +public class UseOfMethodsForBasicOperations extends PHPSubscriptionCheck { protected static final String ERROR_MESSAGE = "Use of methods for basic operations"; @@ -40,23 +42,23 @@ public void visitNode(Tree tree) { final List parents = this.getAllParent(tree, new ArrayList<>()); parents.forEach(parent -> { - if(parent.is(Tree.Kind.SCRIPT)) { + if (parent.is(Tree.Kind.SCRIPT)) { final ScriptTree specific = (ScriptTree) parent; final List trees = specific.statements(); trees.forEach(statement -> { - if(statement.is(Tree.Kind.CLASS_DECLARATION)) { + if (statement.is(Tree.Kind.CLASS_DECLARATION)) { final List methodDeclarations = ((ClassDeclarationTree) statement).members() .stream() .filter(member -> member.is(Tree.Kind.METHOD_DECLARATION)) .map(MethodDeclarationTree.class::cast) .filter(declarationTree -> this.isFunctionDeclared(declarationTree, functionName)) - .collect(Collectors.toList()); + .collect(Collectors.toList()); - if(methodDeclarations != null && !methodDeclarations.isEmpty()) { + if (methodDeclarations != null && !methodDeclarations.isEmpty()) { contains.set(true); } } @@ -64,13 +66,13 @@ public void visitNode(Tree tree) { } }); - if(!contains.get()) { + if (!contains.get()) { context().newIssue(this, tree, ERROR_MESSAGE); } } public boolean isFunctionDeclared(final MethodDeclarationTree method, final String name) { - if(method == null) { + if (method == null) { return false; } @@ -80,12 +82,12 @@ public boolean isFunctionDeclared(final MethodDeclarationTree method, final Stri } public List getAllParent(final Tree tree, final List list) { - if(tree == null) + if (tree == null) return list; final Tree parent = tree.getParent(); - if(parent == null) + if (parent == null) return list; list.add(parent); diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/D2.html b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/D2.html similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/D2.html rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/D2.html diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/D2.json b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/D2.json similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/D2.json rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/D2.json diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/D4.html b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/D4.html similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/D4.html rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/D4.html diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/D4.json b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/D4.json similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/D4.json rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/D4.json diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S34.html b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S34.html similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S34.html rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S34.html diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S34.json b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S34.json similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S34.json rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S34.json diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S66.html b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S66.html similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S66.html rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S66.html diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S66.json b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S66.json similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S66.json rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S66.json diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S67.html b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S67.html similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S67.html rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S67.html diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S67.json b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S67.json similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S67.json rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S67.json diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S69.html b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S69.html similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S69.html rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S69.html diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S69.json b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S69.json similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S69.json rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S69.json diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S72.html b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S72.html similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S72.html rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S72.html diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S72.json b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S72.json similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S72.json rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S72.json diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S74.html b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S74.html similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S74.html rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S74.html diff --git a/php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S74.json b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S74.json similarity index 100% rename from php-plugin/src/main/resources/fr/cnumr/l10n/php/rules/custom/S74.json rename to php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/S74.json diff --git a/php-plugin/src/test/java/fr/cnumr/php/checks/AvoidFullSQLRequestCheckTest.java b/php-plugin/src/test/java/fr/cnumr/php/checks/AvoidFullSQLRequestCheckTest.java deleted file mode 100644 index 713afa617..000000000 --- a/php-plugin/src/test/java/fr/cnumr/php/checks/AvoidFullSQLRequestCheckTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package fr.cnumr.php.checks; - -import org.junit.Test; -import org.sonar.plugins.php.api.tests.PHPCheckTest; -import org.sonar.plugins.php.api.tests.PhpTestFile; - -import java.io.File; - -public class AvoidFullSQLRequestCheckTest { - - @Test - public void test() { - PHPCheckTest.check(new AvoidFullSQLRequestCheck(), new PhpTestFile(new File("src/test/resources/checks/AvoidFullSQLRequest.php"))); - } -} diff --git a/php-plugin/src/test/java/fr/cnumr/php/checks/AvoidSQLRequestInLoopCheckTest.java b/php-plugin/src/test/java/fr/cnumr/php/checks/AvoidSQLRequestInLoopCheckTest.java deleted file mode 100644 index e42a9d911..000000000 --- a/php-plugin/src/test/java/fr/cnumr/php/checks/AvoidSQLRequestInLoopCheckTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package fr.cnumr.php.checks; - -import org.junit.Test; -import org.sonar.plugins.php.api.tests.PHPCheckTest; -import org.sonar.plugins.php.api.tests.PhpTestFile; - -import java.io.File; - -public class AvoidSQLRequestInLoopCheckTest { - - @Test - public void test() throws Exception { - PHPCheckTest.check(new AvoidSQLRequestInLoopCheck(), new PhpTestFile(new File("src/test/resources/checks/AvoidSQLRequestInLoop.php"))); - } -} diff --git a/php-plugin/src/test/java/fr/cnumr/php/checks/AvoidTryCatchFinallyCheckNOKfailsAllTryStatementsTest.java b/php-plugin/src/test/java/fr/cnumr/php/checks/AvoidTryCatchFinallyCheckNOKfailsAllTryStatementsTest.java deleted file mode 100644 index e270951d2..000000000 --- a/php-plugin/src/test/java/fr/cnumr/php/checks/AvoidTryCatchFinallyCheckNOKfailsAllTryStatementsTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package fr.cnumr.php.checks; - -import org.junit.Test; -import org.sonar.plugins.php.api.tests.PHPCheckTest; -import org.sonar.plugins.php.api.tests.PhpTestFile; - -import java.io.File; - -public class AvoidTryCatchFinallyCheckNOKfailsAllTryStatementsTest { - - @Test - public void test() throws Exception { - PHPCheckTest.check(new AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements(), new PhpTestFile(new File("src/test/resources/checks/avoidTryCatchFinallyCheck_NOK_FailsAllTryStatements.php"))); - } - -} diff --git a/php-plugin/src/test/java/fr/cnumr/php/PhpRulesDefinitionTest.java b/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpRulesDefinitionTest.java similarity index 94% rename from php-plugin/src/test/java/fr/cnumr/php/PhpRulesDefinitionTest.java rename to php-plugin/src/test/java/fr/greencodeinitiative/php/PhpRulesDefinitionTest.java index b456fab4c..dcf5cbf46 100644 --- a/php-plugin/src/test/java/fr/cnumr/php/PhpRulesDefinitionTest.java +++ b/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpRulesDefinitionTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.php; +package fr.greencodeinitiative.php; import static org.junit.Assert.assertEquals; import org.junit.Test; diff --git a/php-plugin/src/test/java/fr/cnumr/php/checks/AvoidDoubleQuoteCheckTest.java b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidDoubleQuoteCheckTest.java similarity index 90% rename from php-plugin/src/test/java/fr/cnumr/php/checks/AvoidDoubleQuoteCheckTest.java rename to php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidDoubleQuoteCheckTest.java index 5f6f416ab..1bd7f2ddb 100644 --- a/php-plugin/src/test/java/fr/cnumr/php/checks/AvoidDoubleQuoteCheckTest.java +++ b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidDoubleQuoteCheckTest.java @@ -1,11 +1,11 @@ -package fr.cnumr.php.checks; +package fr.greencodeinitiative.php.checks; + +import java.io.File; import org.junit.Test; import org.sonar.plugins.php.api.tests.PHPCheckTest; import org.sonar.plugins.php.api.tests.PhpTestFile; -import java.io.File; - public class AvoidDoubleQuoteCheckTest { @Test diff --git a/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidFullSQLRequestCheckTest.java b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidFullSQLRequestCheckTest.java new file mode 100644 index 000000000..de90bec78 --- /dev/null +++ b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidFullSQLRequestCheckTest.java @@ -0,0 +1,15 @@ +package fr.greencodeinitiative.php.checks; + +import java.io.File; + +import org.junit.Test; +import org.sonar.plugins.php.api.tests.PHPCheckTest; +import org.sonar.plugins.php.api.tests.PhpTestFile; + +public class AvoidFullSQLRequestCheckTest { + + @Test + public void test() { + PHPCheckTest.check(new AvoidFullSQLRequestCheck(), new PhpTestFile(new File("src/test/resources/checks/AvoidFullSQLRequest.php"))); + } +} diff --git a/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidSQLRequestInLoopCheckTest.java b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidSQLRequestInLoopCheckTest.java new file mode 100644 index 000000000..e124b8547 --- /dev/null +++ b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidSQLRequestInLoopCheckTest.java @@ -0,0 +1,15 @@ +package fr.greencodeinitiative.php.checks; + +import java.io.File; + +import org.junit.Test; +import org.sonar.plugins.php.api.tests.PHPCheckTest; +import org.sonar.plugins.php.api.tests.PhpTestFile; + +public class AvoidSQLRequestInLoopCheckTest { + + @Test + public void test() throws Exception { + PHPCheckTest.check(new AvoidSQLRequestInLoopCheck(), new PhpTestFile(new File("src/test/resources/checks/AvoidSQLRequestInLoop.php"))); + } +} diff --git a/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidTryCatchFinallyCheckNOKfailsAllTryStatementsTest.java b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidTryCatchFinallyCheckNOKfailsAllTryStatementsTest.java new file mode 100644 index 000000000..7a14288ce --- /dev/null +++ b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidTryCatchFinallyCheckNOKfailsAllTryStatementsTest.java @@ -0,0 +1,16 @@ +package fr.greencodeinitiative.php.checks; + +import java.io.File; + +import org.junit.Test; +import org.sonar.plugins.php.api.tests.PHPCheckTest; +import org.sonar.plugins.php.api.tests.PhpTestFile; + +public class AvoidTryCatchFinallyCheckNOKfailsAllTryStatementsTest { + + @Test + public void test() throws Exception { + PHPCheckTest.check(new AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements(), new PhpTestFile(new File("src/test/resources/checks/avoidTryCatchFinallyCheck_NOK_FailsAllTryStatements.php"))); + } + +} diff --git a/php-plugin/src/test/java/fr/cnumr/php/checks/AvoidUsingGlobalVariablesCheckTest.java b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidUsingGlobalVariablesCheckTest.java similarity index 81% rename from php-plugin/src/test/java/fr/cnumr/php/checks/AvoidUsingGlobalVariablesCheckTest.java rename to php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidUsingGlobalVariablesCheckTest.java index e2156bb20..4b8bda378 100644 --- a/php-plugin/src/test/java/fr/cnumr/php/checks/AvoidUsingGlobalVariablesCheckTest.java +++ b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/AvoidUsingGlobalVariablesCheckTest.java @@ -17,22 +17,22 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.php.checks; +package fr.greencodeinitiative.php.checks; + +import java.io.File; import org.junit.Test; import org.sonar.plugins.php.api.tests.PHPCheckTest; import org.sonar.plugins.php.api.tests.PhpTestFile; -import java.io.File; - /** * Test class to test the check implementation. */ public class AvoidUsingGlobalVariablesCheckTest { - @Test - public void test() throws Exception { - PHPCheckTest.check(new AvoidUsingGlobalVariablesCheck(), new PhpTestFile(new File("src/test/resources/checks/AvoidUsingGlobalVariablesCheck.php"))); - } + @Test + public void test() throws Exception { + PHPCheckTest.check(new AvoidUsingGlobalVariablesCheck(), new PhpTestFile(new File("src/test/resources/checks/AvoidUsingGlobalVariablesCheck.php"))); + } } diff --git a/php-plugin/src/test/java/fr/cnumr/php/checks/IncrementCheckTest.java b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/IncrementCheckTest.java similarity index 83% rename from php-plugin/src/test/java/fr/cnumr/php/checks/IncrementCheckTest.java rename to php-plugin/src/test/java/fr/greencodeinitiative/php/checks/IncrementCheckTest.java index fcbad267c..777531563 100644 --- a/php-plugin/src/test/java/fr/cnumr/php/checks/IncrementCheckTest.java +++ b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/IncrementCheckTest.java @@ -17,22 +17,22 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.php.checks; +package fr.greencodeinitiative.php.checks; + +import java.io.File; import org.junit.Test; import org.sonar.plugins.php.api.tests.PHPCheckTest; import org.sonar.plugins.php.api.tests.PhpTestFile; -import java.io.File; - /** * Test class to test the check implementation. */ public class IncrementCheckTest { - @Test - public void test() throws Exception { - PHPCheckTest.check(new IncrementCheck(), new PhpTestFile(new File("src/test/resources/checks/incrementCheck.php"))); - } + @Test + public void test() throws Exception { + PHPCheckTest.check(new IncrementCheck(), new PhpTestFile(new File("src/test/resources/checks/incrementCheck.php"))); + } } diff --git a/php-plugin/src/test/java/fr/cnumr/php/checks/NoFunctionCallWhenDeclaringForLoopTest.java b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/NoFunctionCallWhenDeclaringForLoopTest.java similarity index 90% rename from php-plugin/src/test/java/fr/cnumr/php/checks/NoFunctionCallWhenDeclaringForLoopTest.java rename to php-plugin/src/test/java/fr/greencodeinitiative/php/checks/NoFunctionCallWhenDeclaringForLoopTest.java index b078bc755..2f47ad768 100644 --- a/php-plugin/src/test/java/fr/cnumr/php/checks/NoFunctionCallWhenDeclaringForLoopTest.java +++ b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/NoFunctionCallWhenDeclaringForLoopTest.java @@ -1,11 +1,11 @@ -package fr.cnumr.php.checks; +package fr.greencodeinitiative.php.checks; + +import java.io.File; import org.junit.Test; import org.sonar.plugins.php.api.tests.PHPCheckTest; import org.sonar.plugins.php.api.tests.PhpTestFile; -import java.io.File; - public class NoFunctionCallWhenDeclaringForLoopTest { @Test diff --git a/php-plugin/src/test/java/fr/cnumr/php/checks/UseOfMethodsForBasicOperationsTest.java b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/UseOfMethodsForBasicOperationsTest.java similarity index 90% rename from php-plugin/src/test/java/fr/cnumr/php/checks/UseOfMethodsForBasicOperationsTest.java rename to php-plugin/src/test/java/fr/greencodeinitiative/php/checks/UseOfMethodsForBasicOperationsTest.java index 34d431f49..ba878b1b6 100644 --- a/php-plugin/src/test/java/fr/cnumr/php/checks/UseOfMethodsForBasicOperationsTest.java +++ b/php-plugin/src/test/java/fr/greencodeinitiative/php/checks/UseOfMethodsForBasicOperationsTest.java @@ -1,11 +1,11 @@ -package fr.cnumr.php.checks; +package fr.greencodeinitiative.php.checks; + +import java.io.File; import org.junit.Test; import org.sonar.plugins.php.api.tests.PHPCheckTest; import org.sonar.plugins.php.api.tests.PhpTestFile; -import java.io.File; - public class UseOfMethodsForBasicOperationsTest { @Test diff --git a/python-plugin/pom.xml b/python-plugin/pom.xml index d83ac8bf9..b6263b73c 100644 --- a/python-plugin/pom.xml +++ b/python-plugin/pom.xml @@ -58,7 +58,7 @@ ${project.artifactId} ${project.name} - fr.cnumr.python.PythonPlugin + fr.greencodeinitiative.python.PythonPlugin true ${sonarqube.version} diff --git a/python-plugin/src/main/java/fr/cnumr/python/PythonPlugin.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonPlugin.java similarity index 96% rename from python-plugin/src/main/java/fr/cnumr/python/PythonPlugin.java rename to python-plugin/src/main/java/fr/greencodeinitiative/python/PythonPlugin.java index 7cfd8a575..2a44514ae 100644 --- a/python-plugin/src/main/java/fr/cnumr/python/PythonPlugin.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonPlugin.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.python; +package fr.greencodeinitiative.python; import org.sonar.api.Plugin; diff --git a/python-plugin/src/main/java/fr/cnumr/python/PythonRulesDefinition.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonRulesDefinition.java similarity index 91% rename from python-plugin/src/main/java/fr/cnumr/python/PythonRulesDefinition.java rename to python-plugin/src/main/java/fr/greencodeinitiative/python/PythonRulesDefinition.java index dc1ab66fb..31843ea13 100644 --- a/python-plugin/src/main/java/fr/cnumr/python/PythonRulesDefinition.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonRulesDefinition.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.python; +package fr.greencodeinitiative.python; import java.util.ArrayList; @@ -36,8 +36,8 @@ public class PythonRulesDefinition implements RulesDefinition { public static final String LANGUAGE = "py"; public static final String NAME = "Collectif Conception Numérique Responsable"; - public static final String RESOURCE_BASE_PATH = "fr/cnumr/l10n/python/rules/python"; - public static final String REPOSITORY_KEY = "cnumr-python"; + public static final String RESOURCE_BASE_PATH = "fr/greencodeinitiative/l10n/python/rules/python"; + public static final String REPOSITORY_KEY = "greencodeinitiative-python"; private static final Set RULE_TEMPLATES_KEY = Collections.emptySet(); private static void setTemplates(NewRepository repository) { diff --git a/python-plugin/src/main/java/fr/cnumr/python/RulesList.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/RulesList.java similarity index 80% rename from python-plugin/src/main/java/fr/cnumr/python/RulesList.java rename to python-plugin/src/main/java/fr/greencodeinitiative/python/RulesList.java index ae30ba853..4e9cf0590 100644 --- a/python-plugin/src/main/java/fr/cnumr/python/RulesList.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/RulesList.java @@ -17,16 +17,21 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.python; - -import fr.cnumr.python.checks.*; -import org.sonar.plugins.python.api.PythonCheck; +package fr.greencodeinitiative.python; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import fr.greencodeinitiative.python.checks.AvoidFullSQLRequest; +import fr.greencodeinitiative.python.checks.AvoidGettersAndSetters; +import fr.greencodeinitiative.python.checks.AvoidGlobalVariableInFunctionCheck; +import fr.greencodeinitiative.python.checks.AvoidSQLRequestInLoop; +import fr.greencodeinitiative.python.checks.AvoidTryCatchFinallyCheck; +import fr.greencodeinitiative.python.checks.NoFunctionCallWhenDeclaringForLoop; +import org.sonar.plugins.python.api.PythonCheck; + public final class RulesList { private RulesList() { diff --git a/python-plugin/src/main/java/fr/cnumr/python/checks/AvoidFullSQLRequest.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidFullSQLRequest.java similarity index 89% rename from python-plugin/src/main/java/fr/cnumr/python/checks/AvoidFullSQLRequest.java rename to python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidFullSQLRequest.java index 91b9c872a..0fb917722 100644 --- a/python-plugin/src/main/java/fr/cnumr/python/checks/AvoidFullSQLRequest.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidFullSQLRequest.java @@ -1,4 +1,4 @@ -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; import java.util.ArrayList; @@ -22,14 +22,13 @@ tags = {"bug"}) public class AvoidFullSQLRequest extends PythonSubscriptionCheck { - protected static final String MESSAGERULE = "Don't use the query SELECT * FROM"; - private static final String REGEXPSELECTFROM = "(?i).*select.*\\*.*from.*"; + protected static final String MESSAGERULE = "Don't use the query SELECT * FROM"; + private static final String REGEXPSELECTFROM = "(?i).*select.*\\*.*from.*"; private static final Map> linesWithIssuesByFile = new HashMap<>(); - - @Override - public void initialize(Context context) { + @Override + public void initialize(Context context) { context.registerSyntaxNodeConsumer(Tree.Kind.STRING_LITERAL, this::visitNodeString); } @@ -54,6 +53,7 @@ private void repport(StringElement stringElement, SubscriptionContext ctx) { } ctx.addIssue(stringElement, MESSAGERULE); } + private boolean lineAlreadyHasThisIssue(StringElement stringElement, SubscriptionContext ctx) { if (stringElement.firstToken() != null) { final String filename = ctx.pythonFile().fileName(); diff --git a/python-plugin/src/main/java/fr/cnumr/python/checks/AvoidGettersAndSetters.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSetters.java similarity index 67% rename from python-plugin/src/main/java/fr/cnumr/python/checks/AvoidGettersAndSetters.java rename to python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSetters.java index cb44282b2..7d7aa00f7 100644 --- a/python-plugin/src/main/java/fr/cnumr/python/checks/AvoidGettersAndSetters.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSetters.java @@ -1,4 +1,4 @@ -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; import java.util.List; import java.util.stream.Collectors; @@ -6,6 +6,7 @@ import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.python.api.PythonSubscriptionCheck; +import org.sonar.plugins.python.api.SubscriptionContext; import org.sonar.plugins.python.api.tree.AnyParameter; import org.sonar.plugins.python.api.tree.AssignmentStatement; import org.sonar.plugins.python.api.tree.FunctionDef; @@ -15,14 +16,13 @@ import org.sonar.plugins.python.api.tree.Statement; import org.sonar.plugins.python.api.tree.StatementList; import org.sonar.plugins.python.api.tree.Tree; -import org.sonar.plugins.python.api.SubscriptionContext; @Rule( key = AvoidGettersAndSetters.RULE_KEY, name = "Developpement", description = AvoidGettersAndSetters.DESCRIPTION, priority = Priority.MINOR, - tags = { "bug" }) + tags = {"bug"}) public class AvoidGettersAndSetters extends PythonSubscriptionCheck { public static final String RULE_KEY = "D7"; @@ -35,42 +35,45 @@ public void initialize(Context context) { StatementList statementList = functionDef.body(); List statements = statementList.statements(); if (functionDef.parent().parent().is(Tree.Kind.CLASSDEF)) { - checkAllGetters(statements,functionDef,ctx); - checkAllSetters(statements,functionDef,ctx); + checkAllGetters(statements, functionDef, ctx); + checkAllSetters(statements, functionDef, ctx); } }); } - public void checkAllSetters(List statements,FunctionDef functionDef,SubscriptionContext ctx){ - if(statements.size() == 1 && statements.get(0).is(Tree.Kind.ASSIGNMENT_STMT)){ + + public void checkAllSetters(List statements, FunctionDef functionDef, SubscriptionContext ctx) { + if (statements.size() == 1 && statements.get(0).is(Tree.Kind.ASSIGNMENT_STMT)) { AssignmentStatement assignmentStatement = (AssignmentStatement) statements.get(0); - if(checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot((QualifiedExpression) assignmentStatement.children().get(0).children().get(0))){ + if (checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot((QualifiedExpression) assignmentStatement.children().get(0).children().get(0))) { // Check if assignedValue is a parameter of the function - ParameterList parameters = functionDef.parameters(); - if(parameters != null && !parameters.all().stream().filter(p -> checkAssignementFromParameter(assignmentStatement, p)).collect(Collectors.toList()).isEmpty()){ - ctx.addIssue(functionDef.defKeyword(), AvoidGettersAndSetters.DESCRIPTION); + ParameterList parameters = functionDef.parameters(); + if (parameters != null && !parameters.all().stream().filter(p -> checkAssignementFromParameter(assignmentStatement, p)).collect(Collectors.toList()).isEmpty()) { + ctx.addIssue(functionDef.defKeyword(), AvoidGettersAndSetters.DESCRIPTION); } - } + } } } - public void checkAllGetters(List statements,FunctionDef functionDef,SubscriptionContext ctx){ + + public void checkAllGetters(List statements, FunctionDef functionDef, SubscriptionContext ctx) { Statement lastStatement = statements.get(statements.size() - 1); if (lastStatement.is(Tree.Kind.RETURN_STMT)) { List returnStatementChildren = ((ReturnStatement) lastStatement).children(); if (returnStatementChildren.get(1).is(Tree.Kind.QUALIFIED_EXPR) && - checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot((QualifiedExpression) returnStatementChildren.get(1))){ + checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot((QualifiedExpression) returnStatementChildren.get(1))) { ctx.addIssue(functionDef.defKeyword(), AvoidGettersAndSetters.DESCRIPTION); } } } - public boolean checkAssignementFromParameter(AssignmentStatement assignmentStatement, AnyParameter parameter){ + + public boolean checkAssignementFromParameter(AssignmentStatement assignmentStatement, AnyParameter parameter) { String parameterToString = parameter.firstToken().value(); return assignmentStatement.assignedValue().firstToken().value().equalsIgnoreCase(parameterToString); } - public boolean checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot(QualifiedExpression qualifiedExpression){ + public boolean checkIfStatementIsQualifiedExpressionAndStartsWithSelfDot(QualifiedExpression qualifiedExpression) { List qualifedExpressionChildren = qualifiedExpression.children(); return qualifedExpressionChildren.size() == 3 && - qualifedExpressionChildren.get(0).firstToken().value().equalsIgnoreCase("self") && - qualifedExpressionChildren.get(1).firstToken().value().equalsIgnoreCase("."); + qualifedExpressionChildren.get(0).firstToken().value().equalsIgnoreCase("self") && + qualifedExpressionChildren.get(1).firstToken().value().equalsIgnoreCase("."); } } diff --git a/python-plugin/src/main/java/fr/cnumr/python/checks/AvoidGlobalVariableInFunctionCheck.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGlobalVariableInFunctionCheck.java similarity index 99% rename from python-plugin/src/main/java/fr/cnumr/python/checks/AvoidGlobalVariableInFunctionCheck.java rename to python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGlobalVariableInFunctionCheck.java index 8ac24365a..44fe0f87d 100644 --- a/python-plugin/src/main/java/fr/cnumr/python/checks/AvoidGlobalVariableInFunctionCheck.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGlobalVariableInFunctionCheck.java @@ -1,4 +1,4 @@ -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; import java.util.ArrayList; import java.util.HashMap; diff --git a/python-plugin/src/main/java/fr/cnumr/python/checks/AvoidSQLRequestInLoop.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidSQLRequestInLoop.java similarity index 81% rename from python-plugin/src/main/java/fr/cnumr/python/checks/AvoidSQLRequestInLoop.java rename to python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidSQLRequestInLoop.java index 344164358..81386776c 100644 --- a/python-plugin/src/main/java/fr/cnumr/python/checks/AvoidSQLRequestInLoop.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidSQLRequestInLoop.java @@ -1,13 +1,23 @@ -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.python.api.PythonSubscriptionCheck; import org.sonar.plugins.python.api.SubscriptionContext; import org.sonar.plugins.python.api.symbols.Symbol; -import org.sonar.plugins.python.api.tree.*; - -import java.util.*; +import org.sonar.plugins.python.api.tree.AliasedName; +import org.sonar.plugins.python.api.tree.BaseTreeVisitor; +import org.sonar.plugins.python.api.tree.CallExpression; +import org.sonar.plugins.python.api.tree.FileInput; +import org.sonar.plugins.python.api.tree.Name; +import org.sonar.plugins.python.api.tree.QualifiedExpression; +import org.sonar.plugins.python.api.tree.Tree; @Rule( key = "S72", @@ -45,7 +55,7 @@ private void visitFile(SubscriptionContext ctx) { } private static class SymbolsFromImport extends BaseTreeVisitor { - private Set symbols = new HashSet<>(); + private final Set symbols = new HashSet<>(); @Override public void visitAliasedName(AliasedName aliasedName) { @@ -53,6 +63,7 @@ public void visitAliasedName(AliasedName aliasedName) { symbols.add(names.get(names.size() - 1).symbol()); } } + private void checkCallExpression(SubscriptionContext context) { CallExpression expression = (CallExpression) context.syntaxNode(); diff --git a/python-plugin/src/main/java/fr/cnumr/python/checks/AvoidTryCatchFinallyCheck.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheck.java similarity index 95% rename from python-plugin/src/main/java/fr/cnumr/python/checks/AvoidTryCatchFinallyCheck.java rename to python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheck.java index 60f4c0ab0..45128bab3 100644 --- a/python-plugin/src/main/java/fr/cnumr/python/checks/AvoidTryCatchFinallyCheck.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheck.java @@ -1,4 +1,4 @@ -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; import org.sonar.check.Priority; import org.sonar.check.Rule; diff --git a/python-plugin/src/main/java/fr/cnumr/python/checks/NoFunctionCallWhenDeclaringForLoop.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/NoFunctionCallWhenDeclaringForLoop.java similarity index 95% rename from python-plugin/src/main/java/fr/cnumr/python/checks/NoFunctionCallWhenDeclaringForLoop.java rename to python-plugin/src/main/java/fr/greencodeinitiative/python/checks/NoFunctionCallWhenDeclaringForLoop.java index a099b3825..2a613bcc3 100644 --- a/python-plugin/src/main/java/fr/cnumr/python/checks/NoFunctionCallWhenDeclaringForLoop.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/NoFunctionCallWhenDeclaringForLoop.java @@ -1,4 +1,4 @@ -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; import org.sonar.check.Priority; import org.sonar.check.Rule; diff --git a/python-plugin/src/main/java/fr/cnumr/python/checks/package-info.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/package-info.java similarity index 95% rename from python-plugin/src/main/java/fr/cnumr/python/checks/package-info.java rename to python-plugin/src/main/java/fr/greencodeinitiative/python/checks/package-info.java index 61366432f..63be179f7 100644 --- a/python-plugin/src/main/java/fr/cnumr/python/checks/package-info.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/package-info.java @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ @ParametersAreNonnullByDefault -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/python-plugin/src/main/java/fr/cnumr/python/package-info.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/package-info.java similarity index 96% rename from python-plugin/src/main/java/fr/cnumr/python/package-info.java rename to python-plugin/src/main/java/fr/greencodeinitiative/python/package-info.java index 1d6a8636f..58299e2f7 100644 --- a/python-plugin/src/main/java/fr/cnumr/python/package-info.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/package-info.java @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ @ParametersAreNonnullByDefault -package fr.cnumr.python; +package fr.greencodeinitiative.python; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/D4.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/D4.html similarity index 100% rename from python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/D4.html rename to python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/D4.html diff --git a/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/D4.json b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/D4.json similarity index 100% rename from python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/D4.json rename to python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/D4.json diff --git a/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/D7.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/D7.html similarity index 100% rename from python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/D7.html rename to python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/D7.html diff --git a/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/D7.json b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/D7.json similarity index 100% rename from python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/D7.json rename to python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/D7.json diff --git a/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S34.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S34.html similarity index 100% rename from python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S34.html rename to python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S34.html diff --git a/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S34.json b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S34.json similarity index 100% rename from python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S34.json rename to python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S34.json diff --git a/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S69.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S69.html similarity index 100% rename from python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S69.html rename to python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S69.html diff --git a/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S69.json b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S69.json similarity index 100% rename from python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S69.json rename to python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S69.json diff --git a/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S72.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S72.html similarity index 100% rename from python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S72.html rename to python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S72.html diff --git a/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S72.json b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S72.json similarity index 100% rename from python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S72.json rename to python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S72.json diff --git a/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S74.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S74.html similarity index 100% rename from python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S74.html rename to python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S74.html diff --git a/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S74.json b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S74.json similarity index 100% rename from python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/S74.json rename to python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/S74.json diff --git a/python-plugin/src/test/java/fr/cnumr/python/PythonPluginTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonPluginTest.java similarity index 97% rename from python-plugin/src/test/java/fr/cnumr/python/PythonPluginTest.java rename to python-plugin/src/test/java/fr/greencodeinitiative/python/PythonPluginTest.java index 1ba6ba168..bcf7ce18c 100644 --- a/python-plugin/src/test/java/fr/cnumr/python/PythonPluginTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonPluginTest.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.python; +package fr.greencodeinitiative.python; import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; diff --git a/python-plugin/src/test/java/fr/cnumr/python/PythonRulesDefinitionTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonRulesDefinitionTest.java similarity index 92% rename from python-plugin/src/test/java/fr/cnumr/python/PythonRulesDefinitionTest.java rename to python-plugin/src/test/java/fr/greencodeinitiative/python/PythonRulesDefinitionTest.java index c61bc6720..83c415654 100644 --- a/python-plugin/src/test/java/fr/cnumr/python/PythonRulesDefinitionTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonRulesDefinitionTest.java @@ -17,9 +17,9 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package fr.cnumr.python; +package fr.greencodeinitiative.python; -import static fr.cnumr.python.PythonRulesDefinition.REPOSITORY_KEY; +import static fr.greencodeinitiative.python.PythonRulesDefinition.REPOSITORY_KEY; import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; import org.sonar.api.server.rule.RulesDefinition; diff --git a/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidFullSQLRequestTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidFullSQLRequestTest.java similarity index 85% rename from python-plugin/src/test/java/fr/cnumr/python/checks/AvoidFullSQLRequestTest.java rename to python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidFullSQLRequestTest.java index 14ba67a62..e03a71fa2 100644 --- a/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidFullSQLRequestTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidFullSQLRequestTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; import org.junit.Test; import org.sonar.python.checks.utils.PythonCheckVerifier; diff --git a/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidGettersAndSettersTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSettersTest.java similarity index 86% rename from python-plugin/src/test/java/fr/cnumr/python/checks/AvoidGettersAndSettersTest.java rename to python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSettersTest.java index a4a265069..26d7b4db3 100644 --- a/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidGettersAndSettersTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSettersTest.java @@ -1,10 +1,10 @@ -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; import org.junit.Test; import org.sonar.python.checks.utils.PythonCheckVerifier; public class AvoidGettersAndSettersTest { - + @Test public void test() { PythonCheckVerifier.verify("src/test/resources/checks/avoidGettersAndSetters.py", new AvoidGettersAndSetters()); diff --git a/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidGlobalVariableInFunctionCheckTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidGlobalVariableInFunctionCheckTest.java similarity index 87% rename from python-plugin/src/test/java/fr/cnumr/python/checks/AvoidGlobalVariableInFunctionCheckTest.java rename to python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidGlobalVariableInFunctionCheckTest.java index 3b75bf356..c58ed4bc8 100644 --- a/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidGlobalVariableInFunctionCheckTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidGlobalVariableInFunctionCheckTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; import org.junit.Test; import org.sonar.python.checks.utils.PythonCheckVerifier; diff --git a/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidSQLRequestInLoopTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidSQLRequestInLoopTest.java similarity index 90% rename from python-plugin/src/test/java/fr/cnumr/python/checks/AvoidSQLRequestInLoopTest.java rename to python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidSQLRequestInLoopTest.java index 811cae909..a982bf0ef 100644 --- a/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidSQLRequestInLoopTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidSQLRequestInLoopTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; import org.junit.Test; import org.sonar.python.checks.utils.PythonCheckVerifier; diff --git a/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidTryCatchFinallyCheckTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheckTest.java similarity index 86% rename from python-plugin/src/test/java/fr/cnumr/python/checks/AvoidTryCatchFinallyCheckTest.java rename to python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheckTest.java index bfa4a6c50..03450d068 100644 --- a/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidTryCatchFinallyCheckTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheckTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; import org.junit.Test; diff --git a/python-plugin/src/test/java/fr/cnumr/python/checks/NoFunctionCallWhenDeclaringForLoopTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/NoFunctionCallWhenDeclaringForLoopTest.java similarity index 87% rename from python-plugin/src/test/java/fr/cnumr/python/checks/NoFunctionCallWhenDeclaringForLoopTest.java rename to python-plugin/src/test/java/fr/greencodeinitiative/python/checks/NoFunctionCallWhenDeclaringForLoopTest.java index 3f7c655cb..77b1feb31 100644 --- a/python-plugin/src/test/java/fr/cnumr/python/checks/NoFunctionCallWhenDeclaringForLoopTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/NoFunctionCallWhenDeclaringForLoopTest.java @@ -1,4 +1,4 @@ -package fr.cnumr.python.checks; +package fr.greencodeinitiative.python.checks; import org.junit.Test; import org.sonar.python.checks.utils.PythonCheckVerifier;