diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a688cf3..3e00672b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - correction of technical problem with Integration tests (because of Maven format in technical answer to "sonar-orchestrator-junit5" library) - upgrade JDK from 11 to 17 - [#4](https://github.com/green-code-initiative/creedengo-java/issues/4) Improvement: "++i" statement is not so bad +- huge refacto to have all the test files in the same place, to avoid maintaining 2... ### Deleted diff --git a/pom.xml b/pom.xml index ae43a190..f3d069ee 100644 --- a/pom.xml +++ b/pom.xml @@ -236,6 +236,11 @@ org.apache.maven.plugins maven-surefire-plugin 3.5.2 + + + src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks + + org.jacoco diff --git a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java index 3a51b723..4308202b 100644 --- a/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java +++ b/src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.java @@ -33,20 +33,20 @@ void testGCI27() { String ruleId = "creedengo-java:GCI27"; String ruleMsg = "Use System.arraycopy to copy arrays"; int[] startLines = new int[]{ - 51, 56, 63, 72, 85, 94, - 105, 116, 139, 145, 153, 163, - 177, 187, 199, 211, 229, 236, - 245, 256, 271, 282, 295, 308, - 334, 341, 350, 361, 376, 389, - 415, 422, 431, 442, 457, 470 + 68, 73, 80, 89, 102, 111, + 122, 133, 156, 162, 170, 180, + 194, 204, 216, 228, 246, 253, + 262, 273, 288, 299, 312, 325, + 351, 358, 367, 378, 393, 406, + 432, 439, 448, 459, 474, 487 }; int[] endLines = new int[]{ - 53, 60, 69, 82, 91, 102, - 113, 124, 141, 149, 159, 173, - 183, 195, 207, 219, 232, 241, - 252, 267, 278, 291, 304, 317, - 337, 346, 357, 372, 385, 398, - 418, 427, 438, 453, 466, 479 + 70, 77, 86, 99, 108, 119, + 130, 141, 158, 166, 176, 190, + 200, 212, 224, 236, 249, 258, + 269, 284, 295, 308, 321, 334, + 354, 363, 374, 389, 402, 415, + 435, 444, 455, 470, 483, 496 }; checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_20MIN); @@ -57,8 +57,8 @@ void testGCI27() { void testGCI74() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidFullSQLRequestCheck.java"; - int[] startLines = new int[]{8, 12, 17, 23}; - int[] endLines = new int[]{8, 12, 17, 23}; + int[] startLines = new int[]{25, 29, 34, 40}; + int[] endLines = new int[]{25, 29, 34, 40}; String ruleId = "creedengo-java:GCI74"; String ruleMsg = "Don't use the query SELECT * FROM"; @@ -83,8 +83,8 @@ void testGCI3_forEachLoopIgnored() { void testGCI3_forLoopBad() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopBad.java"; - int[] startLines = new int[]{13}; - int[] endLines = new int[]{13}; + int[] startLines = new int[]{30}; + int[] endLines = new int[]{30}; String ruleId = "creedengo-java:GCI3"; String ruleMsg = "Avoid getting the size of the collection in the loop"; @@ -122,8 +122,8 @@ void testGCI3_forLoopIgnored() { void testGCI3_whileLoopBad() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInWhileLoopBad.java"; - int[] startLines = new int[]{17}; - int[] endLines = new int[]{17}; + int[] startLines = new int[]{34}; + int[] endLines = new int[]{34}; String ruleId = "creedengo-java:GCI3"; String ruleMsg = "Avoid getting the size of the collection in the loop"; @@ -163,17 +163,17 @@ void testGCI2() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatement.java"; int[] startLines = new int[]{ - 24, 43, 45, 71, 88, 110, - 112, 131, 135, 137, 158, 164, - 190, 209, 212, 214, 211, 236, - 257, 259 + 41, 60, 62, 88, 105, 127, + 129, 148, 152, 154, 175, + 181, 207, 226, 228, 229, + 231, 253, 274, 276 }; int[] endLines = new int[]{ - 24, 43, 47, 71, 90, 110, - 114, 133, 135, 139, 160, 166, - 192, 209, 212, 216, 217, 238, - 257, 261 + 41, 60, 64, 88, 107, 127, + 131, 150, 152, 156, 177, + 183, 209, 226, 234, 229, + 233, 255, 274, 278 }; String ruleId = "creedengo-java:GCI2"; @@ -251,8 +251,8 @@ void testGCI2_noIssue() { void testGCI77_invalid() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java"; - int[] startLines = new int[]{8}; - int[] endLines = new int[]{8}; + int[] startLines = new int[]{25}; + int[] endLines = new int[]{25}; String ruleId = "creedengo-java:GCI77"; String ruleMsg = "Avoid using Pattern.compile() in a non-static context."; @@ -304,20 +304,20 @@ void testGCI78() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSetConstantInBatchUpdateCheck.java"; int[] startLines = new int[]{ - 34, 35, 36, 37, 38, 39, - 40, 41, 42, 43, 44, 45, - 46, 61, 63, 64, 65, 66, - 67, 70, 86, 88, 90, 91, - 92, 93, 94, 96, 114, 116, - 117, 118, 119, 120, 121, 123 + 51, 52, 53, 54, 55, 56, + 57, 58, 59, 60, 61, 62, + 63, 78, 80, 81, 82, 83, + 84, 87, 103, 105, 107, 108, + 109, 110, 111, 113, 131, 133, + 134, 135, 136, 137, 138, 140 }; int[] endLines = new int[]{ - 34, 35, 36, 37, 38, 39, - 40, 41, 42, 43, 44, 45, - 46, 61, 63, 64, 65, 66, - 67, 70, 86, 88, 90, 91, - 92, 93, 94, 96, 114, 116, - 117, 118, 119, 120, 121, 123 + 51, 52, 53, 54, 55, 56, + 57, 58, 59, 60, 61, 62, + 63, 78, 80, 81, 82, 83, + 84, 87, 103, 105, 107, 108, + 109, 110, 111, 113, 131, 133, + 134, 135, 136, 137, 138, 140 }; String ruleId = "creedengo-java:GCI78"; String ruleMsg = "Avoid setting constants in batch update"; @@ -363,8 +363,8 @@ void testGCI72() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSQLRequestInLoopCheck.java"; String ruleId = "creedengo-java:GCI72"; String ruleMsg = "Avoid SQL request in loop"; - int[] startLines = new int[]{57, 88, 119}; - int[] endLines = new int[]{57, 88, 119}; + int[] startLines = new int[]{74, 105, 136}; + int[] endLines = new int[]{74, 105, 136}; checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_10MIN); } @@ -374,8 +374,8 @@ void testGCI5() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidStatementForDMLQueries.java"; String ruleId = "creedengo-java:GCI5"; String ruleMsg = "You must not use Statement for a DML query"; - int[] startLines = new int[]{18}; - int[] endLines = new int[]{18}; + int[] startLines = new int[]{32}; + int[] endLines = new int[]{32}; checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_10MIN); } @@ -385,8 +385,8 @@ void testGCI76() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollections.java"; String ruleId = "creedengo-java:GCI76"; String ruleMsg = "Avoid usage of static collections."; - int[] startLines = new int[]{10, 12, 14}; - int[] endLines = new int[]{10, 12, 14}; + int[] startLines = new int[]{27, 29, 31}; + int[] endLines = new int[]{27, 29, 31}; checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_20MIN); } @@ -407,8 +407,8 @@ void testGCI79() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/FreeResourcesOfAutoCloseableInterface.java"; String ruleId = "creedengo-java:GCI79"; String ruleMsg = "try-with-resources Statement needs to be implemented for any object that implements the AutoClosable interface."; - int[] startLines = new int[]{23}; - int[] endLines = new int[]{36}; + int[] startLines = new int[]{40}; + int[] endLines = new int[]{53}; checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_15MIN); } @@ -418,19 +418,19 @@ void testGCI32() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/InitializeBufferWithAppropriateSize.java"; String ruleId = "creedengo-java:GCI32"; String ruleMsg = "Initialize StringBuilder or StringBuffer with appropriate size"; - int[] startLines = new int[]{16, 24}; - int[] endLines = new int[]{16, 24}; + int[] startLines = new int[]{33, 41}; + int[] endLines = new int[]{33, 41}; checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines); } @Test void testGCI67() { - String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheck.java"; + String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/gci67/IncrementCheck.java"; String ruleId = "creedengo-java:GCI67"; String ruleMsg = "Use ++i instead of i++"; - int[] startLines = new int[]{9, 24, 47}; - int[] endLines = new int[]{9, 24, 47}; + int[] startLines = new int[]{31, 51, 74}; + int[] endLines = new int[]{31, 51, 74}; checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines); } @@ -440,8 +440,8 @@ void testGCI82() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.java"; String ruleId = "creedengo-java:GCI82"; String ruleMsg = "The variable is never reassigned and can be 'final'"; - int[] startLines = new int[]{7, 12, 13, 45}; - int[] endLines = new int[]{7, 12, 13, 45}; + int[] startLines = new int[]{26, 31, 32, 64}; + int[] endLines = new int[]{26, 31, 32, 64}; checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines); } @@ -451,8 +451,8 @@ void testGCI69() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/NoFunctionCallWhenDeclaringForLoop.java"; String ruleId = "creedengo-java:GCI69"; String ruleMsg = "Do not call a function when declaring a for-type loop"; - int[] startLines = new int[]{58, 66, 74, 101}; - int[] endLines = new int[]{58, 66, 74, 101}; + int[] startLines = new int[]{60, 68, 76, 103}; + int[] endLines = new int[]{60, 68, 76, 103}; checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines); } @@ -462,9 +462,9 @@ void testGCI28() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck.java"; - int[] startLines = new int[]{23}; + int[] startLines = new int[]{38}; - int[] endLines = new int[]{23}; + int[] endLines = new int[]{38}; String ruleId = "creedengo-java:GCI28"; String ruleMsg = "Optimize Read File Exceptions"; @@ -478,9 +478,9 @@ void testGCI28_2() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck2.java"; - int[] startLines = new int[]{20}; + int[] startLines = new int[]{37}; - int[] endLines = new int[]{20}; + int[] endLines = new int[]{37}; String ruleId = "creedengo-java:GCI28"; String ruleMsg = "Optimize Read File Exceptions"; @@ -494,9 +494,9 @@ void testGCI28_3() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck3.java"; - int[] startLines = new int[]{19}; + int[] startLines = new int[]{36}; - int[] endLines = new int[]{19}; + int[] endLines = new int[]{36}; String ruleId = "creedengo-java:GCI28"; String ruleMsg = "Optimize Read File Exceptions"; @@ -510,9 +510,9 @@ void testGCI28_4() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck4.java"; - int[] startLines = new int[]{18}; + int[] startLines = new int[]{35}; - int[] endLines = new int[]{18}; + int[] endLines = new int[]{35}; String ruleId = "creedengo-java:GCI28"; String ruleMsg = "Optimize Read File Exceptions"; @@ -526,9 +526,9 @@ void testGCI28_5() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck5.java"; - int[] startLines = new int[]{18}; + int[] startLines = new int[]{35}; - int[] endLines = new int[]{18}; + int[] endLines = new int[]{35}; String ruleId = "creedengo-java:GCI28"; String ruleMsg = "Optimize Read File Exceptions"; @@ -542,8 +542,8 @@ void testGCI94() { String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElse.java"; String ruleId = "creedengo-java:GCI94"; String ruleMsg = "Use optional orElseGet instead of orElse."; - int[] startLines = new int[]{25}; - int[] endLines = new int[]{25}; + int[] startLines = new int[]{27}; + int[] endLines = new int[]{27}; checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN); } diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/ArrayCopyCheck.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/ArrayCopyCheck.java index 4a7d153c..71ec1553 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/ArrayCopyCheck.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/ArrayCopyCheck.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.Arrays; @@ -48,28 +65,28 @@ public void copyWithForLoop() { boolean[] dest = new boolean[len]; // Simple copy - for (int i = 0; i < len; i++) { + for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} dest[i] = src[i]; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy with nested conditions - for (int i = 0; i < len; i++) { - if (i + 2 < len) { + for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} + if(i + 2 < len) { dest[i] = src[i + 2]; } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy with nested ELSE conditions - for (int i = 0; i < len; i++) { - if (i + 2 >= len) { + for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} + if(i + 2 >= len) { i++; } else { dest[i] = src[i + 2]; } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy with more nested conditions - for (int i = 0; i < len; i++) { + for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} if (i + 2 < len) { if (dest != null) { if (src != null) { @@ -79,19 +96,19 @@ public void copyWithForLoop() { } } } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch - for (int i = 0; i < len; i++) { + for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { dest[i] = src[i]; } catch (RuntimeException e) { e.printStackTrace(); } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch and if - for (int i = 0; i < len; i++) { + for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { if (dest != null) { dest[i] = src[i]; @@ -99,10 +116,10 @@ public void copyWithForLoop() { } catch (RuntimeException e) { e.printStackTrace(); } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch in catch - for (int i = 0; i < len; i++) { + for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { dest.toString(); } catch (RuntimeException e) { @@ -110,10 +127,10 @@ public void copyWithForLoop() { dest[i] = src[i]; } } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch in finally - for (int i = 0; i < len; i++) { + for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { dest.toString(); } catch (RuntimeException e) { @@ -121,7 +138,7 @@ public void copyWithForLoop() { } finally { dest[i] = src[i]; } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Array transformation for (int i = 0; i < len; i++) { @@ -136,31 +153,31 @@ public void copyWithForEachLoop() { // Simple copy by foreach int i = -1; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} dest[++i] = b; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy with nested conditions by foreach i = -1; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} if (b) { dest[++i] = b; } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy with nested ELSE conditions by foreach i = -1; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} if (i + 2 >= len) { i++; } else { dest[++i] = b; } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy with more nested conditions i = -1; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} if (i + 2 < len) { if (dest != null) { if (src != null) { @@ -170,21 +187,21 @@ public void copyWithForEachLoop() { } } } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch i = -1; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { dest[++i] = b; } catch (RuntimeException e) { e.printStackTrace(); } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch and if i = -1; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { if (dest != null) { dest[++i] = b; @@ -192,11 +209,11 @@ public void copyWithForEachLoop() { } catch (RuntimeException e) { e.printStackTrace(); } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch in catch i = -1; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { dest.toString(); } catch (RuntimeException e) { @@ -204,11 +221,11 @@ public void copyWithForEachLoop() { dest[++i] = b; } } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch in finally i = -1; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { dest.toString(); } catch (RuntimeException e) { @@ -216,7 +233,7 @@ public void copyWithForEachLoop() { } finally { dest[++i] = b; } - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Array transformation i = -1; @@ -226,34 +243,34 @@ public void copyWithForEachLoop() { // Simple copy i = 0; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} dest[i] = src[i]; i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy with nested conditions i = 0; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} if (b) { dest[i] = src[i]; } i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy with nested ELSE conditions i = 0; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} if (i + 2 >= len) { i++; } else { dest[i] = src[i + 2]; } i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy with more nested conditions i = 0; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} if (i + 2 < len) { if (dest != null) { if (src != null) { @@ -264,22 +281,22 @@ public void copyWithForEachLoop() { } } i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch i = 0; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { dest[i] = src[i]; } catch (RuntimeException e) { e.printStackTrace(); } i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch and if i = 0; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { if (dest != null) { dest[i] = src[i]; @@ -288,11 +305,11 @@ public void copyWithForEachLoop() { e.printStackTrace(); } i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch in catch i = 0; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { dest.toString(); } catch (RuntimeException e) { @@ -301,11 +318,11 @@ public void copyWithForEachLoop() { } } i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch in finally i = 0; - for (boolean b : src) { + for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { dest.toString(); } catch (RuntimeException e) { @@ -314,7 +331,7 @@ public void copyWithForEachLoop() { dest[i] = src[i]; } i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Array transformation i = 0; @@ -331,34 +348,34 @@ public void copyWithWhileLoop() { // Simple copy int i = 0; - while (i < len) { + while (i < len) { // Noncompliant {{Use System.arraycopy to copy arrays}} dest[i] = src[i]; i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy with nested conditions i = 0; - while (i < len) { + while (i < len) { // Noncompliant {{Use System.arraycopy to copy arrays}} if (i + 2 < len) { dest[i] = src[i + 2]; } i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy with nested ELSE conditions i = 0; - while (i < len) { + while (i < len) { // Noncompliant {{Use System.arraycopy to copy arrays}} if (i + 2 >= len) { i++; } else { dest[i] = src[i + 2]; } i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy with more nested conditions i = 0; - while (i < len) { + while (i < len) { // Noncompliant {{Use System.arraycopy to copy arrays}} if (i + 2 < len) { if (dest != null) { if (src != null) { @@ -369,11 +386,11 @@ public void copyWithWhileLoop() { } } i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch and if i = 0; - while (i < len) { + while (i < len) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { if (dest != null) { dest[i] = src[i]; @@ -382,11 +399,11 @@ public void copyWithWhileLoop() { e.printStackTrace(); } i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Copy nested by try/catch in catch i = 0; - while (i < len) { + while (i < len) { // Noncompliant {{Use System.arraycopy to copy arrays}} try { dest.toString(); } catch (RuntimeException e) { @@ -395,7 +412,7 @@ public void copyWithWhileLoop() { } } i++; - } // Noncompliant {{Use System.arraycopy to copy arrays}} + } // Array transformation i = 0; @@ -412,34 +429,34 @@ public void copyWithDoWhileLoop() { // Simple copy int i = 0; - do { + do { // Noncompliant {{Use System.arraycopy to copy arrays}} dest[i] = src[i]; i++; - } while (i < len); // Noncompliant {{Use System.arraycopy to copy arrays}} + } while (i < len); // Copy with nested conditions i = 0; - do { + do { // Noncompliant {{Use System.arraycopy to copy arrays}} if (i + 2 < len) { dest[i] = src[i + 2]; } i++; - } while (i < len); // Noncompliant {{Use System.arraycopy to copy arrays}} + } while (i < len); // Copy with nested ELSE conditions i = 0; - do { + do { // Noncompliant {{Use System.arraycopy to copy arrays}} if (i + 2 >= len) { i++; } else { dest[i] = src[i + 2]; } i++; - } while (i < len); // Noncompliant {{Use System.arraycopy to copy arrays}} + } while (i < len); // Copy with more nested conditions i = 0; - do { + do { // Noncompliant {{Use System.arraycopy to copy arrays}} if (i + 2 < len) { if (dest != null) { if (src != null) { @@ -450,11 +467,11 @@ public void copyWithDoWhileLoop() { } } i++; - } while (i < len); // Noncompliant {{Use System.arraycopy to copy arrays}} + } while (i < len); // Copy nested by try/catch and if i = 0; - do { + do { // Noncompliant {{Use System.arraycopy to copy arrays}} try { if (dest != null) { dest[i] = src[i]; @@ -463,11 +480,11 @@ public void copyWithDoWhileLoop() { e.printStackTrace(); } i++; - } while (i < len); // Noncompliant {{Use System.arraycopy to copy arrays}} + } while (i < len); // Copy nested by try/catch in catch i = 0; - do { + do { // Noncompliant {{Use System.arraycopy to copy arrays}} try { dest.toString(); } catch (RuntimeException e) { @@ -476,7 +493,7 @@ public void copyWithDoWhileLoop() { } } i++; - } while (i < len); // Noncompliant {{Use System.arraycopy to copy arrays}} + } while (i < len); // Array transformation i = 0; @@ -484,7 +501,6 @@ public void copyWithDoWhileLoop() { dest[i] = transform(src[i]); i++; } while (i < len); - } private boolean transform(boolean a) { diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidFullSQLRequestCheck.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidFullSQLRequestCheck.java index a525a429..a2731a7a 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidFullSQLRequestCheck.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidFullSQLRequestCheck.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; class AvoidFullSQLRequestCheck { diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForEachLoopIgnored.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForEachLoopIgnored.java index c24f9c92..6eb54828 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForEachLoopIgnored.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForEachLoopIgnored.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.ArrayList; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopBad.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopBad.java index 03efe7c7..de3d8f8e 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopBad.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopBad.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.ArrayList; @@ -10,7 +27,7 @@ public void badForLoop() { numberList.add(10); numberList.add(20); - for (int i = 0; i < numberList.size(); ++i) { // Noncompliant + for (int i = 0; i < numberList.size(); ++i) { // Noncompliant {{Avoid getting the size of the collection in the loop}} System.out.println("numberList.size()"); } } diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopGood.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopGood.java index 88d98861..4c0a4491 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopGood.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopGood.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.Collection; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopIgnored.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopIgnored.java index 3ef6359e..df9e62c1 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopIgnored.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopIgnored.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.ArrayList; @@ -19,6 +36,4 @@ public void badForLoop() { System.out.println(it.next()); } } - - } diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInWhileLoopBad.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInWhileLoopBad.java index 7410af92..b791cd4f 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInWhileLoopBad.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInWhileLoopBad.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.ArrayList; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInWhileLoopGood.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInWhileLoopGood.java index a09e89e5..10f02233 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInWhileLoopGood.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInWhileLoopGood.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.ArrayList; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInWhileLoopIgnored.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInWhileLoopIgnored.java index c22b216b..23d5ae54 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInWhileLoopIgnored.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInWhileLoopIgnored.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.ArrayList; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatement.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatement.java index 3e4ee746..5c066f66 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatement.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatement.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; class AvoidMultipleIfElseStatement { @@ -42,9 +59,9 @@ public int shouldBeNotCompliantBecauseVariablesUsedMaximumTwiceAndDifferentsVari && nb3 == 2 && nb3 == 3) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb1 = 1; - } else { + } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb2 = 2; - } // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} + } if (nb2 == 2) { nb1 = 3; @@ -85,9 +102,9 @@ public int shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInIfStatementsAtD if (nb1 == 1) { if (nb1 == 2) { nb1 = 1; - } else { + } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb1 = 3; - } // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} + } } else { nb1 = 2; } @@ -109,9 +126,9 @@ public int shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseSta } else { if (nb1 == 2) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb1 = 1; - } else { + } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb1 = 3; - } // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} + } } return nb1; @@ -128,15 +145,15 @@ public int shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseSta if (nb1 == 1) { if (nb1 == 3) { nb1 = 4; - } else { + } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb1 = 5; - } // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} + } } else { if (nb1 == 2) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb1 = 1; - } else { + } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb1 = 3; - } // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} + } } return nb1; @@ -155,15 +172,15 @@ public int shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseSta if (nb1 == 1) { if (nb1 == 3) { nb1 = 4; - } else { + } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb1 = 5; - } // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} + } } else if (nb2 == 2) { if (nb1 == 4) { nb1 = 5; - } else { + } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb1 = 6; - } // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} + } } return nb1; @@ -187,9 +204,9 @@ public int shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseSta } else if (nb2 == 2) { if (nb1 == 3) { nb1 = 4; - } else { + } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb1 = 5; - } // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} + } } return nb1; @@ -208,13 +225,13 @@ public int shouldBeNotCompliantBecauseVariableUsedMaximumTwiceInComposedElseStat } else { if (nb1 == 2) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb1 = 1; - } else { + } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} if (nb1 == 3) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb1 = 4; - } else { + } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb1 = 5; - } // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - } // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} + } + } } return nb1; @@ -233,9 +250,9 @@ public int shouldBeNotCompliantBecauseTheSameVariableIsUsedMoreThanTwice() // NO nb2 = 1; } else if (nb1 == nb2) { nb2 = 2; - } else { + } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb2 = 4; - } // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} + } return nb2; } @@ -256,9 +273,9 @@ public int shouldBeNotCompliantBecauseTheSameVariableIsUsedManyTimes() // NOT Co nb2 = 2; } else if (nb3 == nb1) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb2 = 3; - } else { + } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} nb2 = 4; - } // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} + } return nb2; } diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatementCompareMethodNoIssue.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatementCompareMethodNoIssue.java index 1095d923..9a013908 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatementCompareMethodNoIssue.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatementCompareMethodNoIssue.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; class AvoidMultipleIfElseStatementCompareMethodNoIssue { diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatementNoIssue.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatementNoIssue.java index f787785c..3c7df17d 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatementNoIssue.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatementNoIssue.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; class AvoidMultipleIfElseStatementNoIssue { diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java index 20d9c24d..76387635 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStatic.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.regex.Pattern; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticValid1.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticValid1.java index 29ff6bbe..638ffd96 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticValid1.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticValid1.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.regex.Pattern; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticValid2.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticValid2.java index b238d4c2..0ef9517c 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticValid2.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticValid2.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.regex.Pattern; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticValid3.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticValid3.java index 0e748b13..f1378974 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticValid3.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticValid3.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.regex.Pattern; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSQLRequestInLoopCheck.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSQLRequestInLoopCheck.java index cb185891..f75e0c92 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSQLRequestInLoopCheck.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSQLRequestInLoopCheck.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.sql.Connection; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSetConstantInBatchUpdateCheck.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSetConstantInBatchUpdateCheck.java index 342f6588..294cf9ed 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSetConstantInBatchUpdateCheck.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSetConstantInBatchUpdateCheck.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.math.BigDecimal; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInLoopCheck.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInLoopCheck.java index 6e1a2d89..3d386901 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInLoopCheck.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInLoopCheck.java @@ -53,5 +53,5 @@ public Employee(Integer id, String name) { public interface EmployeeRepository extends JpaRepository { } - + } diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidStatementForDMLQueries.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidStatementForDMLQueries.java index def8b5b8..f62ae877 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidStatementForDMLQueries.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidStatementForDMLQueries.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.sql.Connection; @@ -5,9 +22,6 @@ import java.sql.*; import java.sql.PreparedStatement; -import javax.sql.DataSource; - - class AvoidStatementForDMLQueries { AvoidStatementForDMLQueries(AvoidStatementForDMLQueries mc) { } diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollections.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollections.java index 476fbda8..f14de6aa 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollections.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollections.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.*; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollectionsGoodWay.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollectionsGoodWay.java index bf6d3e06..e7a60938 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollectionsGoodWay.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollectionsGoodWay.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.util.*; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/FreeResourcesOfAutoCloseableInterface.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/FreeResourcesOfAutoCloseableInterface.java index 95ed3506..a6401735 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/FreeResourcesOfAutoCloseableInterface.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/FreeResourcesOfAutoCloseableInterface.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.io.*; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheck.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheck.java deleted file mode 100644 index b42caa96..00000000 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheck.java +++ /dev/null @@ -1,79 +0,0 @@ -package org.greencodeinitiative.creedengo.java.checks; - -class IncrementCheck { - IncrementCheck(IncrementCheck mc) { - } - - int foo1() { - int counter = 0; - return counter++; // Noncompliant {{Use ++i instead of i++}} - } - - private int j = 0; - int foo10() { - return this.j++; // Compliant because maybe the use case needs to return j AND increment it - } - - int foo11() { - int counter = 0; - return ++counter; - } - - int foo2() { - int counter = 0; - counter++; // Noncompliant {{Use ++i instead of i++}} - return counter; - } - - int foo22() { - int counter = 0; - ++counter; - return counter; - } - - int foo3() { - int counter = 0; - counter = counter + 197845 ; - return counter; - } - - int foo4() { - int counter = 0; - counter = counter + 35 + 78 ; - return counter; - } - - void foo50() { - for (int i=0; i < 10; i++) { // Noncompliant {{Use ++i instead of i++}} - System.out.println(i); //NOSONAR - } - } - - void foo51() { - for (int i=0; i < 10; ++i) { - System.out.println(i); //NOSONAR - } - } - - void bar61(int value) { - // For test purpose - } - - int foo61() { - int i = 0; - bar61(i++); // Compliant because maybe bar61 needs the unincremented value - return i; - } - - int foo62() { - int i = 0; - bar61(2 + i++); // Compliant because maybe bar61 needs the unincremented value - return i; - } - - void foo71() { - int counter = 0; - int a = 2 + counter++; // Compliant because we probably want to increment counter - // then to add it to 2 to initialize a - } -} diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/InitializeBufferWithAppropriateSize.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/InitializeBufferWithAppropriateSize.java index 8cc73e29..42fd687f 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/InitializeBufferWithAppropriateSize.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/InitializeBufferWithAppropriateSize.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; class InitializeBufferWithAppropriateSize { diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.java index bef640d4..c358c4fe 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.java @@ -1,3 +1,22 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.greencodeinitiative.creedengo.java.checks; + import java.util.logging.Logger; public class MakeNonReassignedVariablesConstants { diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/NoFunctionCallWhenDeclaringForLoop.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/NoFunctionCallWhenDeclaringForLoop.java index 42326070..85cdb1bf 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/NoFunctionCallWhenDeclaringForLoop.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/NoFunctionCallWhenDeclaringForLoop.java @@ -1,5 +1,5 @@ -package org.greencodeinitiative.creedengo.java.integration.tests;/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs +/* +* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) * * This program is free software: you can redistribute it and/or modify @@ -15,6 +15,8 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +package org.greencodeinitiative.creedengo.java.checks; + import java.util.Iterator; import java.util.List; import java.util.ListIterator; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck.java index 91520bd5..1163a226 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.io.FileInputStream; @@ -7,8 +24,6 @@ import java.util.List; import java.util.logging.Logger; -import static java.lang.System.Logger.Level.ERROR; - class OptimizeReadFileExceptionCheck { Logger logger = Logger.getLogger(""); diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck2.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck2.java index 205b814a..9f1dda5e 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck2.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck2.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.io.FileInputStream; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck3.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck3.java index a26b6ea7..7a3a3409 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck3.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck3.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.io.FileInputStream; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck4.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck4.java index 80a75d9d..eab3367b 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck4.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck4.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.io.FileInputStream; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck5.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck5.java index 2115bef1..04999b71 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck5.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheck5.java @@ -1,3 +1,20 @@ +/* + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.greencodeinitiative.creedengo.java.checks; import java.io.FileInputStream; diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElse.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElse.java index 20a0decc..6a93a0be 100644 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElse.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElse.java @@ -16,6 +16,8 @@ * along with this program. If not, see . */ +package org.greencodeinitiative.creedengo.java.checks; + import java.util.Optional; class UseOptionalOrElseGetVsOrElse { diff --git a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/ZzzDDCToCheckOptimizeSQLQueriesWithLimit.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/ZzzDDCToCheckOptimizeSQLQueriesWithLimit.java deleted file mode 100644 index f90c2cb4..00000000 --- a/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/ZzzDDCToCheckOptimizeSQLQueriesWithLimit.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.greencodeinitiative.creedengo.java.checks; - -import org.springframework.data.jpa.repository.Query; - -import java.util.ArrayList; -import java.util.List; - -class ZzzDDCToCheckOptimizeSQLQueriesWithLimit { - - public void literalSQLrequest() { - dummyCall("SELECT user FROM myTable"); // Noncompliant {{Optimize Database SQL Queries (Clause LIMIT)}} - dummyCall("SELECT user FROM myTable LIMIT 50"); // Compliant - } - - @Query("select t from Todo t where t.status != 'COMPLETED'") // Noncompliant {{Optimize Database SQL Queries (Clause LIMIT)}} - public List findAllUsers() { - return new ArrayList<>(); - } - - @Query("select t from Todo t where t.status != 'COMPLETED' LIMIT 25") // Compliant - public List findFirstUsers() { - return new ArrayList<>(); - } - - private void callQuery() { - String sql1 = "SELECT user FROM myTable"; // Noncompliant {{Optimize Database SQL Queries (Clause LIMIT)}} - String sql2 = "SELECT user FROM myTable LIMIT 50"; // Compliant - } - - private void dummyCall(String request) { - } -} diff --git a/src/test/files/IncrementCheck.java b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/gci67/IncrementCheck.java similarity index 94% rename from src/test/files/IncrementCheck.java rename to src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/gci67/IncrementCheck.java index 86da5166..b04028d4 100644 --- a/src/test/files/IncrementCheck.java +++ b/src/it/test-projects/creedengo-java-plugin-test-project/src/main/java/org/greencodeinitiative/creedengo/java/checks/gci67/IncrementCheck.java @@ -15,13 +15,13 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.greencodeinitiative.creedengo.java.checks; +package org.greencodeinitiative.creedengo.java.checks.gci67; -private class Foo { - public int i; //NOSONAR -} class IncrementCheck { + private class Foo { + public int i; //NOSONAR + } IncrementCheck(IncrementCheck mc) { } @@ -42,7 +42,7 @@ int foo11() { } int foo12() { - Foo f; + Foo f = new Foo(); return f.i++; // Compliant because maybe the use case needs to return j AND increment it } diff --git a/src/test/files/ArrayCopyCheck.java b/src/test/files/ArrayCopyCheck.java deleted file mode 100644 index 79d8353d..00000000 --- a/src/test/files/ArrayCopyCheck.java +++ /dev/null @@ -1,510 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; - -class TestClass { - - public void copyArrayOK() { - final int len = 5; - final boolean[] src = new boolean[len]; - boolean[] dest = new boolean[len]; - - // Copy with clone - dest = src.clone(); - - // Copy with System.arraycopy() - System.arraycopy(src, 0, dest, 0, src.length); - - // Copy with Arrays.copyOf() - dest = Arrays.copyOf(src, src.length); - } - - public void nonRegression() { - final int len = 5; - final boolean[] src = new boolean[len]; - boolean[] dest = new boolean[len]; - - // Simple assignation - for (int i = 0; i < len; i++) { - dest[i] = true; - } - - // Edit same array - for (int i = 0; i < len-1; i++) { - dest[i] = dest[i+1]; - } - - // Objects assignations - String a = null; - String b = "Sample Value"; - for (int i = 0; i < len; i++) { - a = b; - } - } - - public void copyWithForLoop() { - final int len = 5; - final boolean[] src = new boolean[len]; - boolean[] dest = new boolean[len]; - - // Simple copy - for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} - dest[i] = src[i]; - } - - // Copy with nested conditions - for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(i + 2 < len) { - dest[i] = src[i + 2]; - } - } - - // Copy with nested ELSE conditions - for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(i + 2 >= len) { - i++; - } else { - dest[i] = src[i + 2]; - } - } - - // Copy with more nested conditions - for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(i + 2 < len) { - if(dest != null) { - if(src != null) { - if(i > 1 && i + 2 < src.length) { - dest[i] = src[i + 2]; - } - } - } - } - } - - // Copy nested by try/catch - for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - dest[i] = src[i]; - } catch (RuntimeException e) { - e.printStackTrace(); - } - } - - // Copy nested by try/catch and if - for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - if(dest != null) { - dest[i] = src[i]; - } - } catch (RuntimeException e) { - e.printStackTrace(); - } - } - - // Copy nested by try/catch in catch - for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - dest.toString(); - } catch (RuntimeException e) { - if(dest != null) { - dest[i] = src[i]; - } - } - } - - // Copy nested by try/catch in finally - for (int i = 0; i < len; i++) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - dest.toString(); - } catch (RuntimeException e) { - e.printStackTrace(); - } finally { - dest[i] = src[i]; - } - } - - // Array transformation - for (int i = 0; i < len; i++) { - dest[i] = transform(src[i]); - } - } - - public void copyWithForEachLoop() { - final int len = 5; - final boolean[] src = new boolean[len]; - boolean[] dest = new boolean[len]; - - // Simple copy by foreach - int i = -1; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - dest[++i] = b; - } - - // Copy with nested conditions by foreach - i = -1; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(b) { - dest[++i] = b; - } - } - - // Copy with nested ELSE conditions by foreach - i = -1; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(i + 2 >= len) { - i++; - } else { - dest[++i] = b; - } - } - - // Copy with more nested conditions - i = -1; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(i + 2 < len) { - if(dest != null) { - if(src != null) { - if(i > 1 && i + 2 < src.length) { - dest[++i] = b; - } - } - } - } - } - - // Copy nested by try/catch - i = -1; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - dest[++i] = b; - } catch (RuntimeException e) { - e.printStackTrace(); - } - } - - // Copy nested by try/catch and if - i = -1; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - if(dest != null) { - dest[++i] = b; - } - } catch (RuntimeException e) { - e.printStackTrace(); - } - } - - // Copy nested by try/catch in catch - i = -1; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - dest.toString(); - } catch (RuntimeException e) { - if(dest != null) { - dest[++i] = b; - } - } - } - - // Copy nested by try/catch in finally - i = -1; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - dest.toString(); - } catch (RuntimeException e) { - e.printStackTrace(); - } finally { - dest[++i] = b; - } - } - - // Array transformation - i = -1; - for (boolean b : src) { - dest[++i] = transform(b); - } - - // Simple copy - int i = 0; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - dest[i] = src[i]; - i++; - } - - // Copy with nested conditions - i = 0; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(b) { - dest[i] = src[i]; - } - i++; - } - - // Copy with nested ELSE conditions - i = 0; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(i + 2 >= len) { - i++; - } else { - dest[i] = src[i + 2]; - } - i++; - } - - // Copy with more nested conditions - i = 0; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(i + 2 < len) { - if(dest != null) { - if(src != null) { - if(i > 1 && i + 2 < src.length) { - dest[i] = src[i + 2]; - } - } - } - } - i++; - } - - // Copy nested by try/catch - i = 0; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - dest[i] = src[i]; - } catch (RuntimeException e) { - e.printStackTrace(); - } - i++; - } - - // Copy nested by try/catch and if - i = 0; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - if(dest != null) { - dest[i] = src[i]; - } - } catch (RuntimeException e) { - e.printStackTrace(); - } - i++; - } - - // Copy nested by try/catch in catch - i = 0; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - dest.toString(); - } catch (RuntimeException e) { - if(dest != null) { - dest[i] = src[i]; - } - } - i++; - } - - // Copy nested by try/catch in finally - i = 0; - for (boolean b : src) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - dest.toString(); - } catch (RuntimeException e) { - e.printStackTrace(); - } finally { - dest[i] = src[i]; - } - i++; - } - - // Array transformation - i = 0; - for (boolean b : src) { - dest[i] = transform(src[i]); - i++; - } - } - - public void copyWithWhileLoop() { - final int len = 5; - final boolean[] src = new boolean[len]; - boolean[] dest = new boolean[len]; - - // Simple copy - int i = 0; - while (i < len) { // Noncompliant {{Use System.arraycopy to copy arrays}} - dest[i] = src[i]; - i++; - } - - // Copy with nested conditions - i = 0; - while (i < len) { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(i + 2 < len) { - dest[i] = src[i + 2]; - } - i++; - } - - // Copy with nested ELSE conditions - i = 0; - while (i < len) { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(i + 2 >= len) { - i++; - } else { - dest[i] = src[i + 2]; - } - i++; - } - - // Copy with more nested conditions - i = 0; - while (i < len) { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(i + 2 < len) { - if(dest != null) { - if(src != null) { - if(i > 1 && i + 2 < src.length) { - dest[i] = src[i + 2]; - } - } - } - } - i++; - } - - // Copy nested by try/catch and if - i = 0; - while (i < len) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - if(dest != null) { - dest[i] = src[i]; - } - } catch (RuntimeException e) { - e.printStackTrace(); - } - i++; - } - - // Copy nested by try/catch in catch - i = 0; - while (i < len) { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - dest.toString(); - } catch (RuntimeException e) { - if(dest != null) { - dest[i] = src[i]; - } - } - i++; - } - - // Array transformation - i = 0; - while (i < len) { - dest[i] = transform(src[i]); - i++; - } - } - - public void copyWithDoWhileLoop() { - final int len = 5; - final boolean[] src = new boolean[len]; - boolean[] dest = new boolean[len]; - - // Simple copy - int i = 0; - do { // Noncompliant {{Use System.arraycopy to copy arrays}} - dest[i] = src[i]; - i++; - } while (i < len); - - // Copy with nested conditions - i = 0; - do { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(i + 2 < len) { - dest[i] = src[i + 2]; - } - i++; - } while (i < len); - - // Copy with nested ELSE conditions - i = 0; - do { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(i + 2 >= len) { - i++; - } else { - dest[i] = src[i + 2]; - } - i++; - } while (i < len); - - // Copy with more nested conditions - i = 0; - do { // Noncompliant {{Use System.arraycopy to copy arrays}} - if(i + 2 < len) { - if(dest != null) { - if(src != null) { - if(i > 1 && i + 2 < src.length) { - dest[i] = src[i + 2]; - } - } - } - } - i++; - } while (i < len); - - // Copy nested by try/catch and if - i = 0; - do { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - if(dest != null) { - dest[i] = src[i]; - } - } catch (RuntimeException e) { - e.printStackTrace(); - } - i++; - } while (i < len); - - // Copy nested by try/catch in catch - i = 0; - do { // Noncompliant {{Use System.arraycopy to copy arrays}} - try { - dest.toString(); - } catch (RuntimeException e) { - if(dest != null) { - dest[i] = src[i]; - } - } - i++; - } while (i < len); - - // Array transformation - i = 0; - do { - dest[i] = transform(src[i]); - i++; - } while (i < len); - } - - private boolean transform(boolean a) { - return !a; - } - -} diff --git a/src/test/files/AvoidFullSQLRequestCheck.java b/src/test/files/AvoidFullSQLRequestCheck.java deleted file mode 100644 index 2d6b2e6a..00000000 --- a/src/test/files/AvoidFullSQLRequestCheck.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.regex.Pattern; - -class AvoidFullSQLRequestCheck { - AvoidFullSQLRequestCheck(AvoidFullSQLRequestCheck mc) { - } - - public void literalSQLrequest() { - dummyCall(" sElEcT * fRoM myTable"); // Noncompliant {{Don't use the query SELECT * FROM}} - dummyCall(" sElEcT user fRoM myTable"); - - dummyCall("SELECTABLE 2*2 FROMAGE"); //not sql - dummyCall("SELECT *FROM table"); // Noncompliant {{Don't use the query SELECT * FROM}} - } - - - public void variableSQLrequest() { - String requestNonCompiliant = " SeLeCt * FrOm myTable"; // Noncompliant {{Don't use the query SELECT * FROM}} - 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 {{Don't use the query SELECT * FROM}} - } - - private void dummyCall(String request) { - - } - -} diff --git a/src/test/files/AvoidGettingSizeCollectionInForEachLoopIgnored.java b/src/test/files/AvoidGettingSizeCollectionInForEachLoopIgnored.java deleted file mode 100644 index ce4eeac9..00000000 --- a/src/test/files/AvoidGettingSizeCollectionInForEachLoopIgnored.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.Collection; -import java.util.ArrayList; -import java.util.List; - -class AvoidGettingSizeCollectionInForEachLoopIgnored { - AvoidGettingSizeCollectionInForEachLoopIgnored(AvoidGettingSizeCollectionInForEachLoopIgnored obj) { - - } - - public void ignoredLoop() { - List numberList = new ArrayList(); - numberList.add(10); - numberList.add(20); - - for (Integer i : numberList) { // Ignored - int size = numberList.size(); // Compliant with this rule - System.out.println("numberList.size()"); - } - } -} diff --git a/src/test/files/AvoidGettingSizeCollectionInForLoopBad.java b/src/test/files/AvoidGettingSizeCollectionInForLoopBad.java deleted file mode 100644 index b437655d..00000000 --- a/src/test/files/AvoidGettingSizeCollectionInForLoopBad.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.ArrayList; -import java.util.List; - -class AvoidGettingSizeCollectionInForLoopBad { - - public void badForLoop() { - final List numberList = new ArrayList(); - numberList.add(10); - numberList.add(20); - - for (int i = 0; i < numberList.size(); ++i) { // Noncompliant {{Avoid getting the size of the collection in the loop}} - System.out.println("numberList.size()"); - } - } -} diff --git a/src/test/files/AvoidGettingSizeCollectionInForLoopGood.java b/src/test/files/AvoidGettingSizeCollectionInForLoopGood.java deleted file mode 100644 index 691bc334..00000000 --- a/src/test/files/AvoidGettingSizeCollectionInForLoopGood.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.Collection; -import java.util.ArrayList; -import java.util.List; - -class AvoidGettingSizeCollectionInForLoopGood { - AvoidGettingSizeCollectionInForLoopGood(AvoidGettingSizeCollectionInForLoopGood obj) { - - } - - public void goodForLoop() { - List numberList = new ArrayList(); - numberList.add(10); - numberList.add(20); - - int size = numberList.size(); - for (int i = 0; i < size; i++) { // Compliant - System.out.println("numberList.size()"); - int size = numberList.size(); // Compliant with this rule - } - } -} diff --git a/src/test/files/AvoidGettingSizeCollectionInForLoopIgnored.java b/src/test/files/AvoidGettingSizeCollectionInForLoopIgnored.java deleted file mode 100644 index c1fa56cb..00000000 --- a/src/test/files/AvoidGettingSizeCollectionInForLoopIgnored.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.Collection; -import java.util.ArrayList; -import java.util.List; - -class GCI69AvoidGettingSizeCollectionInForLoopBad { - AvoidGettingSizeCollectionInForLoopBad() { - - } - - public void badForLoop() { - final List numberList = new ArrayList(); - numberList.add(10); - numberList.add(20); - - final Iterator it = numberList.iterator(); - for (; it.hasNext(); ) { // Ignored => compliant - System.out.println(it.next()); - } - } -} diff --git a/src/test/files/AvoidGettingSizeCollectionInWhileLoopBad.java b/src/test/files/AvoidGettingSizeCollectionInWhileLoopBad.java deleted file mode 100644 index def86d2b..00000000 --- a/src/test/files/AvoidGettingSizeCollectionInWhileLoopBad.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.Collection; -import java.util.ArrayList; -import java.util.List; - -class AvoidGettingSizeCollectionInWhileLoopBad { - AvoidGettingSizeCollectionInWhileLoopBad() { - - } - - public void badWhileLoop() { - List numberList = new ArrayList(); - numberList.add(10); - numberList.add(20); - - int i = 0; - while (i < numberList.size()) { // Noncompliant {{Avoid getting the size of the collection in the loop}} - System.out.println("numberList.size()"); - i++; - } - } -} diff --git a/src/test/files/AvoidGettingSizeCollectionInWhileLoopGood.java b/src/test/files/AvoidGettingSizeCollectionInWhileLoopGood.java deleted file mode 100644 index 713998d5..00000000 --- a/src/test/files/AvoidGettingSizeCollectionInWhileLoopGood.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.Collection; -import java.util.ArrayList; -import java.util.List; - -class AvoidGettingSizeCollectionInWhileLoopGood { - AvoidGettingSizeCollectionInWhileLoopGood(AvoidGettingSizeCollectionInWhileLoopGood obj) { - - } - - public void goodWhileLoop() { - List numberList = new ArrayList(); - numberList.add(10); - numberList.add(20); - - int size = numberList.size(); - int i = 0; - while (i < size) { // Compliant - System.out.println("numberList.size()"); - int size2 = numberList.size(); // Compliant with this rule - i++; - } - } -} diff --git a/src/test/files/AvoidGettingSizeCollectionInWhileLoopIgnored.java b/src/test/files/AvoidGettingSizeCollectionInWhileLoopIgnored.java deleted file mode 100644 index 99009585..00000000 --- a/src/test/files/AvoidGettingSizeCollectionInWhileLoopIgnored.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.Collection; -import java.util.ArrayList; -import java.util.List; - -class AvoidGettingSizeCollectionInWhileLoopBad { - AvoidGettingSizeCollectionInWhileLoopBad() { - - } - - public void badWhileLoop() { - List numberList = new ArrayList(); - numberList.add(10); - numberList.add(20); - - Iterator it = numberList.iterator(); - int i = 0; - while (it.hasNext()) { // Ignored => compliant - it.next(); - System.out.println("numberList.size()"); - } - } -} diff --git a/src/test/files/AvoidMultipleIfElseStatement.java b/src/test/files/AvoidMultipleIfElseStatement.java deleted file mode 100644 index 52bc42ac..00000000 --- a/src/test/files/AvoidMultipleIfElseStatement.java +++ /dev/null @@ -1,283 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -class AvoidMultipleIfElseStatementCheck { - -// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// // -// // NON COMPLIANT use cases -// // -// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - // NON COMPLIANT - // USE CASE : Non compliant use case to check if following is NON OK : - // - two uses of the same variable - // - usage of the same variable on different levels of IF statements - public int shouldBeCompliantBecauseVariableUsedMaximumTwiceInComposedElseStatements() - { - int nb1 = 0; - - if (nb1 == 1) { - nb1 = 2; - } else { - if (nb1 == 2) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 1; - } - } - - return nb1; - } - - // NON COMPLIANT - // USE CASE : non compliant use case to check if a variable is not used max twice on several IF / ELSE statements - // at the same level - public int shouldBeNotCompliantBecauseVariablesUsedMaximumTwiceAndDifferentsVariablesUsed() - { - int nb1 = 0; - int nb2 = 0; - int nb3 = 0; - - if (nb3 == 1 - && nb3 == 2 - && nb3 == 3) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 1; - } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb2 = 2; - } - - if (nb2 == 2) { - nb1 = 3; - } else { - nb1 = 4; - } - - return nb1; - } - - // NON COMPLIANT - // USE CASE : NON compliant use case to check if following is NOT COMPLIANT : - // one variable is used maximum in two IF / ELSE / ELSEIF statements - public int shouldBeNotCompliantBecauseVariablesIsUsedMoreThanTwice() - { - int nb1 = 0; - - if (nb1 == 1) { - nb1 = 2; - } else { - nb1 = 3; - } - - if (nb1 == 2) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 4; - } - - return nb1; - } - - // NON COMPLIANT - // USE CASE : NON compliant use case to check if following is NOT OK : - // - same variable used maximum twice : no compliant because 2 IFs and 1 ELSE - public int shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInIfStatementsAtDifferentsLevels() - { - int nb1 = 0; - - if (nb1 == 1) { - if (nb1 == 2) { - nb1 = 1; - } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 3; - } - } else { - nb1 = 2; - } - - return nb1; - } - - - // NON COMPLIANT - // USE CASE : non compliant use case to check if following is NOT OK : - // - two uses of the same variable : use thre times with 2 IFs and 1 ELSE - // - usage of the same variable on different levels of IF statements - public int shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseStatements() - { - int nb1 = 0; - - if (nb1 == 1) { - nb1 = 2; - } else { - if (nb1 == 2) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 1; - } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 3; - } - } - - return nb1; - } - - // NON COMPLIANT - // USE CASE : non compliant use case to check if following is NOT OK : - // - two uses of the same variable : use thre times with 2 IFs and 1 ELSE - // - usage of the same variable on different levels of IF statements - public int shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseStatementsScenario2() - { - int nb1 = 0; - - if (nb1 == 1) { - if (nb1 == 3) { - nb1 = 4; - } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 5; - } - } else { - if (nb1 == 2) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 1; - } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 3; - } - } - - return nb1; - } - - - // NON COMPLIANT - // USE CASE : non compliant use case to check if following is NOT OK : - // - two uses of the same variable : use thre times with 2 IFs and 1 ELSE - // - usage of the same variable on different levels of IF statements - public int shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseStatementsScenario3() - { - int nb1 = 0; - int nb2 = 0; - - if (nb1 == 1) { - if (nb1 == 3) { - nb1 = 4; - } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 5; - } - } else if (nb2 == 2) { - if (nb1 == 4) { - nb1 = 5; - } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 6; - } - } - - return nb1; - } - - // NON COMPLIANT - // USE CASE : non compliant use case to check if following is NOT OK : - // - two uses of the same variable : use thre times with 2 IFs and 1 ELSE - // - usage of the same variable on different levels of IF statements - public int shouldBeNotCompliantBecauseVariableUsedMoreThanTwiceInComposedElseStatementsScenario4() - { - int nb1 = 0; - int nb2 = 0; - - if (nb1 == 1) { - if (nb2 == 3) { - nb1 = 4; - } else { - nb1 = 5; - } - } else if (nb2 == 2) { - if (nb1 == 3) { - nb1 = 4; - } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 5; - } - } - - return nb1; - } - - // NON COMPLIANT - // USE CASE : NON compliant use case to check if following is NOT OK : - // - the same variable must used maximum twice - // - usage of the same variable on different levels of IF / ELSE statements - public int shouldBeNotCompliantBecauseVariableUsedMaximumTwiceInComposedElseStatements() - { - int nb1 = 0; - - if (nb1 == 1) { - nb1 = 2; - } else { - if (nb1 == 2) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 1; - } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - if (nb1 == 3) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 4; - } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb1 = 5; - } - } - } - - return nb1; - } - - // NON COMPLIANT - // USE CASE : NON compliant use case to check if following is NOT OK : - // - more than twice uses of the same variable - // - usage of the same variable on different kind of test statements (IF and ELSEIF) - public int shouldBeNotCompliantBecauseTheSameVariableIsUsedMoreThanTwice() // NOT Compliant - { - int nb1 = 0; - int nb2 = 10; - - if (nb1 == 1) { - nb2 = 1; - } else if (nb1 == nb2) { - nb2 = 2; - } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb2 = 4; - } - - return nb2; - } - - // NON COMPLIANT - // USE CASE : NON compliant use case to check if following is NOT OK : - // - more than twice uses of the same variable - // - usage of the same variable on different kind of test statements (IF and ELSEIF) - public int shouldBeNotCompliantBecauseTheSameVariableIsUsedManyTimes() // NOT Compliant - { - int nb1 = 0; - int nb2 = 10; - int nb3 = 11; - - if (nb1 == 1) { - nb2 = 1; - } else if (nb1 == nb2) { - nb2 = 2; - } else if (nb3 == nb1) { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb2 = 3; - } else { // Noncompliant {{Use a switch statement instead of multiple if-else if possible}} - nb2 = 4; - } - - return nb2; - } - -} diff --git a/src/test/files/AvoidMultipleIfElseStatementCompareMethod.java b/src/test/files/AvoidMultipleIfElseStatementCompareMethod.java deleted file mode 100644 index fdf00d60..00000000 --- a/src/test/files/AvoidMultipleIfElseStatementCompareMethod.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -class AvoidMultipleIfElseStatementCompareMethod { - - public int compare(FieldVo o1, FieldVo o2) { - - if (o1.getIdBlock().equals(o2.getIdBlock())) { - if (o1.getIdField().equals(o2.getIdField())) { - return 0; - } - // First original - if (o1.isOriginal() && !o2.isOriginal()) { - return -1; - } else if (!o1.isOriginal() && o2.isOriginal()) { - return 1; - } - // First min posgafld - Long result = o1.getColumnPos() - o2.getColumnPos(); - if (result != 0) { - return result.intValue(); - } - - // First min ordgaflc - result = o1.getIndex() - o2.getIndex(); - return result.intValue(); - } - // First BQRY block - if (o1.getIdBlock().startsWith("BQRY") && !o2.getIdBlock().startsWith("BQRY")) { - return -1; - } else if (!o1.getIdBlock().startsWith("BQRY") && o2.getIdBlock().startsWith("BQRY")) { - return 1; - } - // If both block don't start with BQRY, sort alpha with String.compareTo method - return o1.getIdBlock().compareTo(o2.getIdBlock()); - } - -} diff --git a/src/test/files/AvoidMultipleIfElseStatementInterface.java b/src/test/files/AvoidMultipleIfElseStatementInterface.java deleted file mode 100644 index c9c76041..00000000 --- a/src/test/files/AvoidMultipleIfElseStatementInterface.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -interface AvoidMultipleIfElseStatementCheck { - - TransactionMetaData initMetaData(ITransactionFoundation transactionFoundation) throws ProgramException, MnemonicTemplateShellException; - -} diff --git a/src/test/files/AvoidMultipleIfElseStatementNoIssue.java b/src/test/files/AvoidMultipleIfElseStatementNoIssue.java deleted file mode 100644 index 53e72dd1..00000000 --- a/src/test/files/AvoidMultipleIfElseStatementNoIssue.java +++ /dev/null @@ -1,274 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -class AvoidMultipleIfElseStatementCheckNoIssue { - - // inital RULES : please see HTML description file of this rule (resources directory) - - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // - // COMPLIANT use cases - // - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// - ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - // COMPLIANT - // USE CASE : compliant use case to check if a variable is used maximum twice on several IF / ELSE statements - // at the same level AND no problem with several IF staments at the same level using different variables - public int shouldBeCompliantBecauseVariablesUsedMaximumTwiceAndDifferentsVariablesUsed() - { - int nb1 = 0; - int nb2 = 0; - int nb3 = 0; - - if (nb3 != 1 && nb1 > 1) { - nb1 = 1; - } else { - nb2 = 2; - } - - if (nb2 == 2) { - nb1 = 3; - } else { - nb1 = 4; - } - - return nb1; - } - - // COMPLIANT - // USE CASE : compliant use case to check if a variable is used maximum twice on several IF / ELSE statements - // at the same level AND no problem with several IF staments at the same level using different variables - public int shouldBeCompliantBecauseVariablesUsedMaximumTwiceAndDifferentsVariablesUsedAtDiffLevels() - { - int nb1 = 0; - int nb2 = 0; - int nb3 = 0; - - if (nb1 < 1) { - if (nb2 == 2) { - nb3 = 3; - } else { - nb3 = 4; - } - } else { - nb2 = 2; - } - - if (nb3 >= 1) { - if (nb2 == 2) { - nb1 = 3; - } else { - nb1 = 4; - } - } else { - nb1 = 2; - } - - return nb1; - } - - // COMPLIANT - // USE CASE : compliant use case to check if a variable is used maximum twice on several IF / ELSE statements - // at the same level AND no problem with several IF staments at the same level using different variables - public int shouldBeCompliantBecauseVariablesUsedMaximumTwiceAndDiffVariablesUsedAtDiffLevelsScenario2() - { - int nb1 = 0; - int nb2 = 0; - int nb3 = 0; - - if (nb1 <= 1) { - if (nb2 == 2) { - if (nb3 == 2) { - nb3 = 3; - } else { - nb3 = 4; - } - } else { - nb3 = 4; - } - } else { - nb2 = 2; - } - - if (nb3 == 1) { - if (nb2 == 2) { - nb1 = 3; - } else { - nb1 = 4; - } - } else { - nb1 = 2; - } - - return nb1; - } - - // COMPLIANT - // USE CASE : compliant use case to check if one variable is used maximum twice in different IF statements - public int shouldBeCompliantBecauseVariableUsedMaximumTwiceInIfStatements() - { - int nb1 = 0; - - if (nb1 == 1) { - nb1 = 1; - } - - if (nb1 == 2) { - nb1 = 3; - } - - return nb1; - } - - // COMPLIANT - // USE CASE : compliant use case to check if following is OK : - // - two uses of the same variable - // - usage of the same variable on different levels of IF statements - public int shouldBeCompliantBecauseSereralVariablesUsedMaximumTwiceInComposedElseStatements() - { - int nb1 = 0; - int nb2 = 0; - int nb3 = 0; - - if (nb1 == 1) { - nb1 = 2; - } else { - if (nb2 == 2) { - nb1 = 1; - } else { - if (nb3 == 4) { - nb1 = 3; - } else { - nb1 = 6; - } - } - } - - return nb1; - } - - // COMPLIANT - // USE CASE : compliant use case to check if following is OK : - // - two uses of the same variable - // - usage of the same variable on different kind of test statements (IF and ELSEIF) - public int shouldBeCompliantBecauseVariableUsedMaximumTwiceInIfOrElseIfStatements() // Compliant - { - int nb1 = 0; - int nb2 = 10; - - if (nb1 == 1) { - nb2 = 1; - } else if (nb1 == nb2) { - nb2 = 2; - } - - return nb2; - } - - // COMPLIANT - // USE CASE : compliant use case to check if following is OK : - // - two uses of the same variable - // - usage of the same variable on different kind of test statements (IF and ELSEIF) - public int shouldBeCompliantBecauseSeveralVariablesUsedMaximumTwiceInIfOrElseIfStatements() // Compliant - { - int nb1 = 0; - int nb2 = 10; - int nb3 = 3; - int nb4 = 1; - int nb5 = 2; - - if (nb1 == 1) { - nb2 = 1; - } else if (nb3 == nb2) { - nb2 = 2; - } else if (nb4 == nb5) { - nb2 = 4; - } else { - nb2 = 3; - } - - return nb2; - } - - // COMPLIANT - // USE CASE : Compliant use case to check if following is OK : - // - usage of the same variable on different levels of IF statements but with incompatible type for a switch - public float shouldBeCompliantBecauseVariableHasNotCompatibleTypeFloatForSwitch() - { - float nb1 = 0.0f; - - if (nb1 > 1) { - nb1 = 2.1f; - } else { - if (nb1 > 2) { - nb1 = 1.1f; - } - } - - return nb1; - } - - // COMPLIANT - // USE CASE : Compliant use case to check if following is OK : - // - usage of the same variable on different levels of IF statements but with incompatible type for a switch - public double shouldBeCompliantBecauseVariableHasNotCompatibleTypeDoubleForSwitch() - { - double nb1 = 0.0; - - if (nb1 > 1) { - nb1 = 2.1; - } else { - if (nb1 > 2) { - nb1 = 1.1; - } - } - - return nb1; - } - - // COMPLIANT - // USE CASE : Compliant use case to check if following is OK : - // - usage of the same variable on different levels of IF statements but with instanceof keys - // - with a variable used 4 times - public int shouldBeCompliantBecauseVariableUsed4TimesWithInstanceOfKeys() - { - int nb1 = 0; - Object obj = new Object(); - - if (obj instanceof String) { - nb1 = 1; - } else { - if (obj instanceof Integer) { - nb1 = 2; - } else { - if (obj instanceof Double) { - nb1 = 3; - } else { - nb1 = 4; - } - } - } - - return nb1; - } - - -} diff --git a/src/test/files/AvoidMultipleIfElseStatementNotBlock.java b/src/test/files/AvoidMultipleIfElseStatementNotBlock.java deleted file mode 100644 index 46a5691f..00000000 --- a/src/test/files/AvoidMultipleIfElseStatementNotBlock.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -class AvoidMultipleIfElseStatementNotBlock { - - public boolean equals(Object obj) { - if (this == obj) - return true; - } - -} diff --git a/src/test/files/AvoidRegexPatternNotStatic.java b/src/test/files/AvoidRegexPatternNotStatic.java deleted file mode 100644 index 76387635..00000000 --- a/src/test/files/AvoidRegexPatternNotStatic.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.regex.Pattern; - -public class AvoidRegexPatternNotStatic { - - public boolean foo() { - final Pattern pattern = Pattern.compile("foo"); // Noncompliant {{Avoid using Pattern.compile() in a non-static context.}} - return pattern.matcher("foo").find(); - } -} diff --git a/src/test/files/AvoidRegexPatternNotStaticValid1.java b/src/test/files/AvoidRegexPatternNotStaticValid1.java deleted file mode 100644 index 638ffd96..00000000 --- a/src/test/files/AvoidRegexPatternNotStaticValid1.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.regex.Pattern; - -public class AvoidRegexPatternNotStaticValid1 { - - private static final Pattern pattern = Pattern.compile("foo"); // Compliant - - public boolean foo() { - return pattern.matcher("foo").find(); - } -} diff --git a/src/test/files/AvoidRegexPatternNotStaticValid2.java b/src/test/files/AvoidRegexPatternNotStaticValid2.java deleted file mode 100644 index 0ef9517c..00000000 --- a/src/test/files/AvoidRegexPatternNotStaticValid2.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.regex.Pattern; - -public class AvoidRegexPatternNotStaticValid2 { - - private final Pattern pattern = Pattern.compile("foo"); // Compliant - - public boolean foo() { - return pattern.matcher("foo").find(); - } -} diff --git a/src/test/files/AvoidRegexPatternNotStaticValid3.java b/src/test/files/AvoidRegexPatternNotStaticValid3.java deleted file mode 100644 index f1378974..00000000 --- a/src/test/files/AvoidRegexPatternNotStaticValid3.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.regex.Pattern; - -public class AvoidRegexPatternNotStaticValid3 { - - private final Pattern pattern; - - public AvoidRegexPatternNotStaticValid3() { - pattern = Pattern.compile("foo"); // Compliant - } - - public boolean foo() { - return pattern.matcher("foo").find(); - } -} diff --git a/src/test/files/AvoidSQLRequestInLoopCheck.java b/src/test/files/AvoidSQLRequestInLoopCheck.java deleted file mode 100644 index f75e0c92..00000000 --- a/src/test/files/AvoidSQLRequestInLoopCheck.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -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 {{Avoid SQL request in loop}} - - // 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 {{Avoid SQL request in loop}} - - // 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 {{Avoid SQL request in loop}} - - // 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/src/test/files/AvoidSetConstantInBatchUpdateCheck.java b/src/test/files/AvoidSetConstantInBatchUpdateCheck.java deleted file mode 100644 index d7b8013b..00000000 --- a/src/test/files/AvoidSetConstantInBatchUpdateCheck.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.math.BigDecimal; -import java.sql.PreparedStatement; -import java.util.regex.Pattern; -import java.util.stream.IntStream; -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 < data.length; i++) { - stmt.setInt(1, data[i]); - - stmt.setBoolean(2, true); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setByte(3, (byte) 3); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setByte(4, 'v'); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setShort(5, (short) 5); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setInt(6, 6); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setLong(7, (long) 7); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setLong(7, 7l); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setFloat(8, (float) 8.); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setFloat(8, 8.f); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setDouble(9, 9.); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setDouble(9, 9.); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setString(10, "10"); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setBigDecimal(11, BigDecimal.valueOf(.77)); // Noncompliant {{Avoid setting constants in batch update}} - stmt.addBatch(); - } - int[] nr = stmt.executeBatch(); - logger.log("{} rows updated", IntStream.of(nr).sum()); - con.close(); - } - - - int[] batchInsertInForeachLoop(DummyClass[] data) { - - try (Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle")) { - PreparedStatement stmt = con.prepareStatement("insert into Emp values(?,?,?,?,?,?,?,?,?,?,?,?,?)"); - for (DummyClass o : data) { - stmt.setInt(1, o.getField1()); - stmt.setBoolean(2, Boolean.valueOf("false")); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setByte(3, o.getField3()); - stmt.setByte(4, 'v'); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setShort(5, (short) 5); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setInt(6, 6); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setLong(7, 7); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setFloat(8, (float) 8.); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setDouble(9, o.getField4()); - stmt.setString(10, o.getField2()); - stmt.setBigDecimal(11, BigDecimal.valueOf(11)); // Noncompliant {{Avoid setting constants in batch update}} - stmt.addBatch(); - } - return stmt.executeBatch(); - } - } - - - int[] batchInsertInWhileLoop(DummyClass[] data) { - - try (Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle")) { - PreparedStatement stmt = con.prepareStatement("insert into Emp values(?,?,?,?,?,?,?,?,?,?,?,?,?)"); - int i = 0; - while (i < data.length) { - DummyClass o = data[i]; - stmt.setInt(1, o.getField1()); - stmt.setBoolean(2, Boolean.TRUE); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setByte(3, o.getField3()); - stmt.setByte(4, Byte.MAX_VALUE); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setShort(5, Short.MIN_VALUE); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setInt(6, Integer.MAX_VALUE); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setLong(7, Long.MIN_VALUE); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setFloat(8, Float.MAX_VALUE); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setDouble(9, Double.MIN_VALUE); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setString(10, o.getField2()); - stmt.setBigDecimal(11, BigDecimal.TEN); // Noncompliant {{Avoid setting constants in batch update}} - stmt.addBatch(); - i++; - } - return stmt.executeBatch(); - } - } - - int[] batchInsertInWhileLoop(DummyClass[] data) { - if (data.length == 0) { - return new int[]{}; - } - try (Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle")) { - PreparedStatement stmt = con.prepareStatement("insert into Emp values(?,?,?,?,?,?,?,?,?,?,?,?,?)"); - int i = 0; - do { - DummyClass o = data[i]; - stmt.setInt(1, o.getField1()); - stmt.setBoolean(2, Boolean.valueOf(true)); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setByte(3, o.getField3()); - stmt.setByte(4, Byte.valueOf((byte) 3)); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setByte(4, Character.valueOf('1')); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setShort(5, Short.valueOf((short) 55)); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setInt(6, Integer.valueOf("222")); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setLong(7, Long.valueOf(0)); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setFloat(8, Float.valueOf(.33)); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setDouble(9, Double.valueOf(22)); // Noncompliant {{Avoid setting constants in batch update}} - stmt.setString(10, o.getField2()); - stmt.setBigDecimal(11, BigDecimal.valueOf(11)); // Noncompliant {{Avoid setting constants in batch update}} - stmt.addBatch(); - i++; - } while (i < data.length); - return stmt.executeBatch(); - } - } - - class DummyClass { - - public int getField1() { - return 0; - } - - public String getField2() { - return ""; - } - - public byte getField3() { - return 'A'; - } - - public double getField4() { - return .1; } - } - - -} diff --git a/src/test/files/AvoidSpringRepositoryCallInLoopCheck.java b/src/test/files/AvoidSpringRepositoryCallInLoopCheck.java deleted file mode 100644 index 3d386901..00000000 --- a/src/test/files/AvoidSpringRepositoryCallInLoopCheck.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.jpa.repository.JpaRepository; - -import java.util.*; - -public class AvoidSpringRepositoryCallInLoopCheck { - @Autowired - private EmployeeRepository employeeRepository; - - public List smellGetAllEmployeesByIds(List ids) { - List employees = new ArrayList<>(); - for (Integer id : ids) { - Optional employee = employeeRepository.findById(id); // Noncompliant {{Avoid Spring repository call in loop or stream}} - if (employee.isPresent()) { - employees.add(employee.get()); - } - } - return employees; - } - - public class Employee { - private Integer id; - private String name; - - public Employee(Integer id, String name) { - this.id = id; - this.name = name; - } - - public Integer getId() { return id; } - public String getName() { return name; } - } - - public interface EmployeeRepository extends JpaRepository { - - } - -} diff --git a/src/test/files/AvoidSpringRepositoryCallInStreamCheck.java b/src/test/files/AvoidSpringRepositoryCallInStreamCheck.java deleted file mode 100644 index 6acf592f..00000000 --- a/src/test/files/AvoidSpringRepositoryCallInStreamCheck.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.jpa.repository.JpaRepository; - -import java.util.*; -import java.util.stream.Collectors; -import java.util.stream.IntStream; -import java.util.stream.Stream; - -public class AvoidSpringRepositoryCallInStreamCheck { - - @Autowired - private EmployeeRepository employeeRepository; - - public void smellGetAllEmployeesByIdsForEach() { - List employees = new ArrayList<>(); - Stream stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); - stream.forEach(id -> { - Optional employee = employeeRepository.findById(id); // Noncompliant {{Avoid Spring repository call in loop or stream}} - if (employee.isPresent()) { - employees.add(employee.get()); - } - }); - } - - public void smellGetAllEmployeesByIdsForEachOrdered() { - List employees = new ArrayList<>(); - Stream stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); - stream.forEachOrdered(id -> { - Optional employee = employeeRepository.findById(id); // Noncompliant {{Avoid Spring repository call in loop or stream}} - if (employee.isPresent()) { - employees.add(employee.get()); - } - }); - } - - public List smellGetAllEmployeesByIdsMap() { - List employees = new ArrayList<>(); - Stream stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); - return stream.map(id -> { - Optional employee = employeeRepository.findById(id); // Noncompliant {{Avoid Spring repository call in loop or stream}} - if (employee.isPresent()) { - employees.add(employee.get()); - } - }) - .collect(Collectors.toList()); - } - - public List smellGetAllEmployeesByIdsPeek() { - List employees = new ArrayList<>(); - Stream stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); - return stream.peek(id -> { - Optional employee = employeeRepository.findById(id); // Noncompliant {{Avoid Spring repository call in loop or stream}} - if (employee.isPresent()) { - employees.add(employee.get()); - } - }) - .collect(Collectors.toList()); - } - - public List smellGetAllEmployeesByIdsWithOptional(List ids) { - List employees = new ArrayList<>(); - return ids - .stream() - .map(element -> { - Employee empl = new Employee(); - employees.add(empl); - return employeeRepository.findById(element).orElse(empl);// Noncompliant {{Avoid Spring repository call in loop or stream}} - }) - .collect(Collectors.toList()); - } - - public List smellGetAllEmployeesByIds(List ids) { - Stream stream = ids.stream(); - return stream.map(element -> { - Employee empl = new Employee(); - employees.add(empl); - return employeeRepository.findById(element);// Noncompliant {{Avoid Spring repository call in loop or stream}} - }) - .collect(Collectors.toList()); - } - - public List smellGetAllEmployeesByIdsWithoutStream(List ids) { - return employeeRepository.findAllById(ids); // Compliant - } - - public List smellDeleteEmployeeById(List ids) { - Stream stream = ids.stream(); - return stream.map(element -> { - Employee empl = new Employee(); - employees.add(empl); - return employeeRepository.deleteById(element);// Noncompliant {{Avoid Spring repository call in loop or stream}} - }) - .collect(Collectors.toList()); - } - - public List smellGetAllEmployeesByIdsWithSeveralMethods(List ids) { - Stream stream = ids.stream(); - return stream.map(element -> { - Employee empl = new Employee(); - return employeeRepository.findById(element).orElse(empl).anotherMethod().anotherOne();// Noncompliant {{Avoid Spring repository call in loop or stream}} - }) - .collect(Collectors.toList()); - } - - public class Employee { - private Integer id; - private String name; - - public Employee(Integer id, String name) { - this.id = id; - this.name = name; - } - - public Integer getId() { return id; } - public String getName() { return name; } - } - - public interface EmployeeRepository extends JpaRepository { - } -} diff --git a/src/test/files/AvoidStatementForDMLQueries.java b/src/test/files/AvoidStatementForDMLQueries.java deleted file mode 100644 index a056dbbe..00000000 --- a/src/test/files/AvoidStatementForDMLQueries.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.*; -import java.sql.PreparedStatement; - - -class AvoidStatementForDMLQueries { - AvoidStatementForDMLQueries(AvoidStatementForDMLQueries mc) { - } - - public void insert() { - Statement statement = connection.createStatement(); - statement.executeUpdate("INSERT INTO persons(id, name) VALUES(2, 'Toto')"); // Noncompliant {{You must not use Statement for a DML query}} - } -} diff --git a/src/test/files/AvoidUsageOfStaticCollections.java b/src/test/files/AvoidUsageOfStaticCollections.java deleted file mode 100644 index f14de6aa..00000000 --- a/src/test/files/AvoidUsageOfStaticCollections.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.*; - -/** - * Not compliant - */ -public class AvoidUsageOfStaticCollections { - - public static final List LIST = new ArrayList(); // Noncompliant {{Avoid usage of static collections.}} - - public static final Set SET = new HashSet(); // Noncompliant {{Avoid usage of static collections.}} - - public static final Map MAP = new HashMap(); // Noncompliant {{Avoid usage of static collections.}} - - public AvoidUsageOfStaticCollections() { - } - -} diff --git a/src/test/files/AvoidUsageOfStaticCollectionsGoodWay.java b/src/test/files/AvoidUsageOfStaticCollectionsGoodWay.java deleted file mode 100644 index e7a60938..00000000 --- a/src/test/files/AvoidUsageOfStaticCollectionsGoodWay.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.*; - -/** - * Compliant - */ -public class AvoidUsageOfStaticCollectionsGoodWay { - public static volatile AvoidUsageOfStaticCollectionsGoodWay INSTANCE = new AvoidUsageOfStaticCollectionsGoodWay(); - - public final List LIST = new ArrayList(); // Compliant - public final Set SET = new HashSet(); // Compliant - public final Map MAP = new HashMap(); // Compliant - - private AvoidUsageOfStaticCollectionsGoodWay() { - } -} diff --git a/src/test/files/FreeResourcesOfAutoCloseableInterface.java b/src/test/files/FreeResourcesOfAutoCloseableInterface.java deleted file mode 100644 index 08ba1622..00000000 --- a/src/test/files/FreeResourcesOfAutoCloseableInterface.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.io.*; - -class FreeResourcesOfAutoCloseableInterface { - FreeResourcesOfAutoCloseableInterface(FreeResourcesOfAutoCloseableInterface mc) { - - } - - public void foo1() { - String fileName = "./FreeResourcesOfAutoCloseableInterface.java"; - try (FileReader fr = new FileReader(fileName); - BufferedReader br = new BufferedReader(fr)) { - } catch (IOException e) { - System.err.println(e.getMessage()); - } - } - - public void foo2() { - String fileName = "./FreeResourcesOfAutoCloseableInterface.java"; - try { // Noncompliant - FileReader fr = new FileReader(fileName); - BufferedReader br = new BufferedReader(fr); - System.out.printl(br.readLine()); - } catch (IOException e) { - System.err.println(e.getMessage()); - } finally { - if (fr) { - org.close(); - } - if (br) { - br.close(); - } - } - } -} diff --git a/src/test/files/InitializeBufferWithAppropriateSize.java b/src/test/files/InitializeBufferWithAppropriateSize.java deleted file mode 100644 index a83c21e7..00000000 --- a/src/test/files/InitializeBufferWithAppropriateSize.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.Statement; - -class InitializeBufferWithAppropriateSize { - InitializeBufferWithAppropriateSize(InitializeBufferWithAppropriateSize mc) { - } - - public void testBufferCompliant() { - StringBuffer stringBuffer = new StringBuffer(16); - } - - public void testBufferCompliant2() { - StringBuffer stringBuffer = new StringBuffer(Integer.valueOf(16)); - } - - public void testBufferNonCompliant() { - StringBuffer stringBuffer = new StringBuffer(); // Noncompliant {{Initialize StringBuilder or StringBuffer with appropriate size}} - } - - public void testBuilderCompliant() { - StringBuilder stringBuilder = new StringBuilder(16); - } - - public void testBuilderNonCompliant() { - StringBuilder stringBuilder = new StringBuilder(); // Noncompliant {{Initialize StringBuilder or StringBuffer with appropriate size}} - } -} diff --git a/src/test/files/MakeNonReassignedVariablesConstants.java b/src/test/files/MakeNonReassignedVariablesConstants.java deleted file mode 100644 index bef640d4..00000000 --- a/src/test/files/MakeNonReassignedVariablesConstants.java +++ /dev/null @@ -1,69 +0,0 @@ -import java.util.logging.Logger; - -public class MakeNonReassignedVariablesConstants { - - private final Logger logger = Logger.getLogger(""); // Compliant - - private Object myNonFinalAndNotReassignedObject = new Object(); // Noncompliant {{The variable is never reassigned and can be 'final'}} - private Object myNonFinalAndReassignedObject = new Object(); // Compliant - private final Object myFinalAndNotReassignedObject = new Object(); // Compliant - - private static final String CONSTANT = "toto"; // Compliant - private String varDefinedInClassNotReassigned = "0"; // Noncompliant {{The variable is never reassigned and can be 'final'}} - private String varDefinedInClassNotUsed = "0"; // Noncompliant {{The variable is never reassigned and can be 'final'}} - private String varDefinedInClassReassigned = "0"; // Compliant - private String varDefinedInConstructorReassigned = "1"; // Compliant - - public MakeNonReassignedVariablesConstants() { - varDefinedInConstructorReassigned = "3"; - logger.info(varDefinedInConstructorReassigned); - } - - void localVariableReassigned() { - String y1 = "10"; // Compliant - final String PI = "3.14159"; // Compliant - - y1 = "titi"; - - logger.info(y1); - logger.info(PI); - } - - void localVariableIncrement() { - String y2 = "10"; // Compliant - y2 += "titi"; - logger.info(y2); - } - - void localIntVariableIncrement() { - int y3 = 10; // Compliant - ++y3; - logger.info(y3+""); - } - - void localVariableNotReassigned() { - String y4 = "10"; // Noncompliant {{The variable is never reassigned and can be 'final'}} - final String PI2 = "3.14159"; // Compliant - - logger.info(y4); - logger.info(PI2); - } - - void classVariableReassigned() { - varDefinedInClassReassigned = "1"; - - logger.info(varDefinedInClassReassigned); - logger.info(varDefinedInClassNotReassigned); - logger.info(CONSTANT); - } - - void classVariableReassignedBis() { - varDefinedInClassReassigned = "2"; // method to avoid sonarqube error asking for moving class variable "varDefinedInClassReassigned" to local variable method - myNonFinalAndReassignedObject = new Object(); - - logger.info(varDefinedInClassReassigned); - logger.info(myNonFinalAndReassignedObject.toString()); - logger.info(myFinalAndNotReassignedObject.toString()); - } - -} \ No newline at end of file diff --git a/src/test/files/NoFunctionCallWhenDeclaringForLoop.java b/src/test/files/NoFunctionCallWhenDeclaringForLoop.java deleted file mode 100644 index 42326070..00000000 --- a/src/test/files/NoFunctionCallWhenDeclaringForLoop.java +++ /dev/null @@ -1,119 +0,0 @@ -package org.greencodeinitiative.creedengo.java.integration.tests;/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -import java.util.Iterator; -import java.util.List; -import java.util.ListIterator; -import java.util.Arrays; -class NoFunctionCallWhenDeclaringForLoop { - - public int getMyValue() { - return 6; - } - - public int incrementeMyValue(final int i) { - return i + 100; - } - - public void test1() { - for (int i = 0; i < 20; ++i) { - System.out.println(i); - final boolean b = getMyValue() > 6; - System.out.println(b); - } - } - - public void test2() { - final String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; - for (final String i : cars) { - System.out.println(i); - } - - } - - // compliant, the function is called only once in the initialization so it's not a performance issue - public void test3() { - for (int i = getMyValue(); i < 20; ++i) { - System.out.println(i); - final boolean b = getMyValue() > 6; - System.out.println(b); - } - } - - public void test4() { - for (int i = 0; i < getMyValue(); ++i) { // Noncompliant {{Do not call a function when declaring a for-type loop}} - System.out.println(i); - final boolean b = getMyValue() > 6; - System.out.println(b); - } - } - - public void test5() { - for (final int i = 0; i < getMyValue(); incrementeMyValue(i)) { // Noncompliant {{Do not call a function when declaring a for-type loop}} - System.out.println(i); - final boolean b = getMyValue() > 6; - System.out.println(b); - } - } - - public void test6() { - for (int i = getMyValue(); i < getMyValue(); ++i) { // Noncompliant {{Do not call a function when declaring a for-type loop}} - System.out.println(i); - final boolean b = getMyValue() > 6; - System.out.println(b); - } - } - - // compliant, iterators are allowed to be called in a for loop - public void test7() { - final List joursSemaine = Arrays.asList("Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche"); - - String jour = null; - // iterator is allowed - for (final Iterator iterator = joursSemaine.iterator(); iterator.hasNext(); jour = iterator.next()) { - System.out.println(jour); - } - - // subclass of iterator is allowed - for (final ListIterator iterator = joursSemaine.listIterator(); iterator.hasNext(); jour = iterator.next()) { - System.out.println(jour); - } - - // iterator called in an indirect way is allowed - for (final OtherClassWithIterator otherClass = new OtherClassWithIterator(joursSemaine.iterator()); otherClass.iterator.hasNext(); jour = otherClass.iterator.next()) { - System.out.println(jour); - } - // but using a method that returns an iterator causes an issue - for (final OtherClassWithIterator otherClass = new OtherClassWithIterator(joursSemaine.iterator()); otherClass.getIterator().hasNext(); jour = otherClass.getIterator().next()) { // Noncompliant {{Do not call a function when declaring a for-type loop}} - System.out.println(jour); - } - - } - -} - -class OtherClassWithIterator { - public final Iterator iterator; - - public OtherClassWithIterator(Iterator iterator){ - this.iterator = iterator; - } - - public Iterator getIterator(){ - return iterator; - } -} diff --git a/src/test/files/OptimizeReadFileExceptionCheck.java b/src/test/files/OptimizeReadFileExceptionCheck.java deleted file mode 100644 index 2bafe97a..00000000 --- a/src/test/files/OptimizeReadFileExceptionCheck.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.Arrays; -import java.util.List; - -class ReadFile { - ReadFile(ReadFile readFile) { - } - - public void readPreferences(String filename) { - //... - InputStream in = null; - try { - in = new FileInputStream(filename); // Noncompliant {{Optimize Read File Exceptions}} - } catch (FileNotFoundException e) { - logger.log(e); - } - //... - } -} diff --git a/src/test/files/OptimizeReadFileExceptionCheck2.java b/src/test/files/OptimizeReadFileExceptionCheck2.java deleted file mode 100644 index 4d1e7614..00000000 --- a/src/test/files/OptimizeReadFileExceptionCheck2.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.Arrays; -import java.util.List; - -class ReadFile { - ReadFile(ReadFile readFile) { - } - - public void readPreferences(String filename) { - //... - try (InputStream in = new FileInputStream(filename)) { // Noncompliant {{Optimize Read File Exceptions}} - logger.log("my log"); - } catch (FileNotFoundException e) { - logger.log(e); - } - //... - } -} diff --git a/src/test/files/OptimizeReadFileExceptionCheck3.java b/src/test/files/OptimizeReadFileExceptionCheck3.java deleted file mode 100644 index 7b9292f0..00000000 --- a/src/test/files/OptimizeReadFileExceptionCheck3.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.Arrays; -import java.util.List; - -class ReadFile { - ReadFile(ReadFile readFile) { - } - - public void readPreferences(String filename) { - //... - try (InputStream in = new FileInputStream(filename)) { // Noncompliant {{Optimize Read File Exceptions}} - logger.log("my log"); - } catch (IOException e) { - logger.log(e); - } - //... - } -} diff --git a/src/test/files/OptimizeReadFileExceptionCheck4.java b/src/test/files/OptimizeReadFileExceptionCheck4.java deleted file mode 100644 index 5096450f..00000000 --- a/src/test/files/OptimizeReadFileExceptionCheck4.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.Arrays; -import java.util.List; - -class ReadFile { - ReadFile(ReadFile readFile) { - } - - public void readPreferences(String filename) { - //... - try (InputStream in = new FileInputStream(filename)) { // Noncompliant {{Optimize Read File Exceptions}} - logger.log("my log"); - } catch (Exception e) { - logger.log(e); - } - //... - } -} diff --git a/src/test/files/OptimizeReadFileExceptionCheck5.java b/src/test/files/OptimizeReadFileExceptionCheck5.java deleted file mode 100644 index 8754e265..00000000 --- a/src/test/files/OptimizeReadFileExceptionCheck5.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.greencodeinitiative.creedengo.java.checks; - -import java.util.Arrays; -import java.util.List; - -class ReadFile { - ReadFile(ReadFile readFile) { - } - - public void readPreferences(String filename) { - //... - try (InputStream in = new FileInputStream(filename)) { // Noncompliant {{Optimize Read File Exceptions}} - logger.log("my log"); - } catch (Throwable e) { - logger.log(e); - } - //... - } -} diff --git a/src/test/files/UseOptionalOrElseGetVsOrElse.java b/src/test/files/UseOptionalOrElseGetVsOrElse.java deleted file mode 100644 index 20a0decc..00000000 --- a/src/test/files/UseOptionalOrElseGetVsOrElse.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs - * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -import java.util.Optional; - -class UseOptionalOrElseGetVsOrElse { - - private static Optional variable = Optional.empty(); - - public static final String NAME = Optional.of("creedengo").orElse(getUnpredictedMethod()); // Noncompliant {{Use optional orElseGet instead of orElse.}} - - public static final String NAME2 = Optional.of("creedengo").orElseGet(() -> getUnpredictedMethod()); // Compliant - - public static final String NAME3 = variable.orElse(getUnpredictedMethod()); // Compliant - - private static String getUnpredictedMethod() { - return "unpredicted"; - } - -} diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/ArrayCopyCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/ArrayCopyCheckTest.java index 49deaa19..33800457 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/ArrayCopyCheckTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/ArrayCopyCheckTest.java @@ -28,7 +28,7 @@ class ArrayCopyCheckTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/ArrayCopyCheck.java") + .onFile(System.getProperty("testfiles.path") + "/ArrayCopyCheck.java") .withCheck(new ArrayCopyCheck()) .verifyIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidFullSQLRequestCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidFullSQLRequestCheckTest.java index 889721b1..d311b524 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidFullSQLRequestCheckTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidFullSQLRequestCheckTest.java @@ -25,7 +25,7 @@ class AvoidFullSQLRequestCheckTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidFullSQLRequestCheck.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidFullSQLRequestCheck.java") .withCheck(new AvoidFullSQLRequest()) .verifyIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInLoopTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInLoopTest.java index 4d4b3fd6..6a5752a9 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInLoopTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInLoopTest.java @@ -24,7 +24,7 @@ class AvoidGettingSizeCollectionInLoopTest { @Test void testBadForLoop() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidGettingSizeCollectionInForLoopBad.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidGettingSizeCollectionInForLoopBad.java") .withCheck(new AvoidGettingSizeCollectionInLoop()) .verifyIssues(); } @@ -32,7 +32,7 @@ void testBadForLoop() { @Test void testIgnoredForLoop() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidGettingSizeCollectionInForLoopIgnored.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidGettingSizeCollectionInForLoopIgnored.java") .withCheck(new AvoidGettingSizeCollectionInLoop()) .verifyNoIssues(); } @@ -40,7 +40,7 @@ void testIgnoredForLoop() { @Test void testGoodForLoop() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidGettingSizeCollectionInForLoopGood.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidGettingSizeCollectionInForLoopGood.java") .withCheck(new AvoidGettingSizeCollectionInLoop()) .verifyNoIssues(); } @@ -48,7 +48,7 @@ void testGoodForLoop() { @Test void testBadWhileFoop() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidGettingSizeCollectionInWhileLoopBad.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidGettingSizeCollectionInWhileLoopBad.java") .withCheck(new AvoidGettingSizeCollectionInLoop()) .verifyIssues(); } @@ -56,7 +56,7 @@ void testBadWhileFoop() { @Test void testIgnoredWhileFoop() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidGettingSizeCollectionInWhileLoopIgnored.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidGettingSizeCollectionInWhileLoopIgnored.java") .withCheck(new AvoidGettingSizeCollectionInLoop()) .verifyNoIssues(); } @@ -64,7 +64,7 @@ void testIgnoredWhileFoop() { @Test void testGoodWhileLoop() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidGettingSizeCollectionInWhileLoopGood.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidGettingSizeCollectionInWhileLoopGood.java") .withCheck(new AvoidGettingSizeCollectionInLoop()) .verifyNoIssues(); } @@ -72,7 +72,7 @@ void testGoodWhileLoop() { @Test void testIgnoredForEachLoop() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidGettingSizeCollectionInForEachLoopIgnored.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidGettingSizeCollectionInForEachLoopIgnored.java") .withCheck(new AvoidGettingSizeCollectionInLoop()) .verifyNoIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatementTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatementTest.java index f63326e5..535021f9 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatementTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidMultipleIfElseStatementTest.java @@ -24,11 +24,11 @@ class AvoidMultipleIfElseStatementTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidMultipleIfElseStatement.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidMultipleIfElseStatement.java") .withCheck(new AvoidMultipleIfElseStatement()) .verifyIssues(); CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidMultipleIfElseStatementNoIssue.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidMultipleIfElseStatementNoIssue.java") .withCheck(new AvoidMultipleIfElseStatement()) .verifyNoIssues(); } @@ -36,7 +36,7 @@ void test() { @Test void testInterfaceMethodStatement() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidMultipleIfElseStatementInterface.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidMultipleIfElseStatementInterfaceNoIssue.java") .withCheck(new AvoidMultipleIfElseStatement()) .verifyNoIssues(); } @@ -44,7 +44,7 @@ void testInterfaceMethodStatement() { @Test void testNotBlockStatement() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidMultipleIfElseStatementNotBlock.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidMultipleIfElseStatementNoBlockNoIssue.java") .withCheck(new AvoidMultipleIfElseStatement()) .verifyNoIssues(); } @@ -52,7 +52,7 @@ void testNotBlockStatement() { @Test void testCompareMethod() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidMultipleIfElseStatementCompareMethod.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidMultipleIfElseStatementCompareMethodNoIssue.java") .withCheck(new AvoidMultipleIfElseStatement()) .verifyNoIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticTest.java index c654160c..e1df2a68 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidRegexPatternNotStaticTest.java @@ -25,7 +25,7 @@ class AvoidRegexPatternNotStaticTest { @Test void testHasIssues() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidRegexPatternNotStatic.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidRegexPatternNotStatic.java") .withCheck(new AvoidRegexPatternNotStatic()) .verifyIssues(); } @@ -34,9 +34,9 @@ void testHasIssues() { void testHasNoIssues() { CheckVerifier.newVerifier() .onFiles( - "src/test/files/AvoidRegexPatternNotStaticValid1.java", - "src/test/files/AvoidRegexPatternNotStaticValid2.java", - "src/test/files/AvoidRegexPatternNotStaticValid3.java" + System.getProperty("testfiles.path") + "/AvoidRegexPatternNotStaticValid1.java", + System.getProperty("testfiles.path") + "/AvoidRegexPatternNotStaticValid2.java", + System.getProperty("testfiles.path") + "/AvoidRegexPatternNotStaticValid3.java" ) .withCheck(new AvoidRegexPatternNotStatic()) .verifyNoIssues(); diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSQLRequestInLoopCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSQLRequestInLoopCheckTest.java index bd64a6a7..e2804544 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSQLRequestInLoopCheckTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSQLRequestInLoopCheckTest.java @@ -25,7 +25,7 @@ class AvoidSQLRequestInLoopCheckTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidSQLRequestInLoopCheck.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidSQLRequestInLoopCheck.java") .withCheck(new AvoidSQLRequestInLoop()) .verifyIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSetConstantInBatchInsertTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSetConstantInBatchInsertTest.java index 9dffe6f9..07bdecd2 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSetConstantInBatchInsertTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSetConstantInBatchInsertTest.java @@ -25,7 +25,7 @@ class AvoidSetConstantInBatchInsertTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidSetConstantInBatchUpdateCheck.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidSetConstantInBatchUpdateCheck.java") .withCheck(new AvoidSetConstantInBatchUpdate()) .verifyIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java index 8041f0a7..c681e8b3 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInLoopCheckTest.java @@ -26,7 +26,7 @@ class AvoidSpringRepositoryCallInLoopCheckTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidSpringRepositoryCallInLoopCheck.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidSpringRepositoryCallInLoopCheck.java") .withCheck(new AvoidSpringRepositoryCallInLoopOrStreamCheck()) .withClassPath(FilesUtils.getClassPath("target/test-jars")) .verifyIssues(); diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInStreamCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInStreamCheckTest.java index 1be55d7e..825d36cc 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInStreamCheckTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidSpringRepositoryCallInStreamCheckTest.java @@ -26,7 +26,7 @@ class AvoidSpringRepositoryCallInStreamCheckTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidSpringRepositoryCallInStreamCheck.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidSpringRepositoryCallInStreamCheck.java") .withCheck(new AvoidSpringRepositoryCallInLoopOrStreamCheck()) .withClassPath(FilesUtils.getClassPath("target/test-jars")) .verifyIssues(); diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidStatementForDMLQueriesTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidStatementForDMLQueriesTest.java index 3caac84a..926e3045 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidStatementForDMLQueriesTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidStatementForDMLQueriesTest.java @@ -24,7 +24,7 @@ class AvoidStatementForDMLQueriesTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidStatementForDMLQueries.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidStatementForDMLQueries.java") .withCheck(new AvoidStatementForDMLQueries()) .verifyIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollectionsTests.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollectionsTests.java index 6512a044..ea3af849 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollectionsTests.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/AvoidUsageOfStaticCollectionsTests.java @@ -25,7 +25,7 @@ class AvoidUsageOfStaticCollectionsTests { @Test void testHasIssues() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidUsageOfStaticCollections.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidUsageOfStaticCollections.java") .withCheck(new AvoidUsageOfStaticCollections()) .verifyIssues(); } @@ -33,7 +33,7 @@ void testHasIssues() { @Test void testNoIssues() { CheckVerifier.newVerifier() - .onFile("src/test/files/AvoidUsageOfStaticCollectionsGoodWay.java") + .onFile(System.getProperty("testfiles.path") + "/AvoidUsageOfStaticCollectionsGoodWay.java") .withCheck(new AvoidUsageOfStaticCollections()) .verifyNoIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java index aecce2d7..d816d485 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/FreeResourcesOfAutoCloseableInterfaceTest.java @@ -25,7 +25,7 @@ class FreeResourcesOfAutoCloseableInterfaceTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/FreeResourcesOfAutoCloseableInterface.java") + .onFile(System.getProperty("testfiles.path") + "/FreeResourcesOfAutoCloseableInterface.java") .withCheck(new FreeResourcesOfAutoCloseableInterface()) .withJavaVersion(7) .verifyIssues(); @@ -34,7 +34,7 @@ void test() { @Test void test_no_java_version() { CheckVerifier.newVerifier() - .onFile("src/test/files/FreeResourcesOfAutoCloseableInterface.java") + .onFile(System.getProperty("testfiles.path") + "/FreeResourcesOfAutoCloseableInterface.java") .withCheck(new FreeResourcesOfAutoCloseableInterface()) .verifyIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheckTest.java index d11de845..08824fff 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheckTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/IncrementCheckTest.java @@ -25,7 +25,7 @@ class IncrementCheckTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/IncrementCheck.java") + .onFile(System.getProperty("testfiles.path") + "/gci67/IncrementCheck.java") .withCheck(new IncrementCheck()) .verifyIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/InitializeBufferWithAppropriateSizeTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/InitializeBufferWithAppropriateSizeTest.java index 0261b8f9..54dc10e8 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/InitializeBufferWithAppropriateSizeTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/InitializeBufferWithAppropriateSizeTest.java @@ -25,7 +25,7 @@ class InitializeBufferWithAppropriateSizeTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/InitializeBufferWithAppropriateSize.java") + .onFile(System.getProperty("testfiles.path") + "/InitializeBufferWithAppropriateSize.java") .withCheck(new InitializeBufferWithAppropriateSize()) .verifyIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstantsTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstantsTest.java index 837a632e..2fe316fa 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstantsTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstantsTest.java @@ -25,7 +25,7 @@ class MakeNonReassignedVariablesConstantsTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/MakeNonReassignedVariablesConstants.java") + .onFile(System.getProperty("testfiles.path") + "/MakeNonReassignedVariablesConstants.java") .withCheck(new MakeNonReassignedVariablesConstants()) .verifyIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java index e4dff73e..3473045a 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/NoFunctionCallWhenDeclaringForLoopTest.java @@ -24,7 +24,7 @@ class NoFunctionCallWhenDeclaringForLoopTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/NoFunctionCallWhenDeclaringForLoop.java") + .onFile(System.getProperty("testfiles.path") + "/NoFunctionCallWhenDeclaringForLoop.java") .withCheck(new NoFunctionCallWhenDeclaringForLoop()) .verifyIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheckTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheckTest.java index d3f1def0..d37fe3aa 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheckTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/OptimizeReadFileExceptionCheckTest.java @@ -25,7 +25,7 @@ class OptimizeReadFileExceptionCheckTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/OptimizeReadFileExceptionCheck.java") + .onFile(System.getProperty("testfiles.path") + "/OptimizeReadFileExceptionCheck.java") .withCheck(new OptimizeReadFileExceptions()) .verifyIssues(); } @@ -33,7 +33,7 @@ void test() { @Test void test2() { CheckVerifier.newVerifier() - .onFile("src/test/files/OptimizeReadFileExceptionCheck2.java") + .onFile(System.getProperty("testfiles.path") + "/OptimizeReadFileExceptionCheck2.java") .withCheck(new OptimizeReadFileExceptions()) .verifyIssues(); } @@ -41,7 +41,7 @@ void test2() { @Test void test3() { CheckVerifier.newVerifier() - .onFile("src/test/files/OptimizeReadFileExceptionCheck3.java") + .onFile(System.getProperty("testfiles.path") + "/OptimizeReadFileExceptionCheck3.java") .withCheck(new OptimizeReadFileExceptions()) .verifyIssues(); } @@ -49,7 +49,7 @@ void test3() { @Test void test4() { CheckVerifier.newVerifier() - .onFile("src/test/files/OptimizeReadFileExceptionCheck4.java") + .onFile(System.getProperty("testfiles.path") + "/OptimizeReadFileExceptionCheck4.java") .withCheck(new OptimizeReadFileExceptions()) .verifyIssues(); } @@ -57,7 +57,7 @@ void test4() { @Test void test5() { CheckVerifier.newVerifier() - .onFile("src/test/files/OptimizeReadFileExceptionCheck5.java") + .onFile(System.getProperty("testfiles.path") + "/OptimizeReadFileExceptionCheck5.java") .withCheck(new OptimizeReadFileExceptions()) .verifyIssues(); } diff --git a/src/test/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElseTest.java b/src/test/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElseTest.java index 2edc2711..dc12fb3a 100644 --- a/src/test/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElseTest.java +++ b/src/test/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElseTest.java @@ -24,7 +24,7 @@ class UseOptionalOrElseGetVsOrElseTest { @Test void test() { CheckVerifier.newVerifier() - .onFile("src/test/files/UseOptionalOrElseGetVsOrElse.java") + .onFile(System.getProperty("testfiles.path") + "/UseOptionalOrElseGetVsOrElse.java") .withCheck(new UseOptionalOrElseGetVsOrElse()) .verifyIssues(); }