-
-
Notifications
You must be signed in to change notification settings - Fork 25
Implement analyzer for secrets
#141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
manumafe98
merged 7 commits into
exercism:main
from
manumafe98:ImplementAnalyzerForSecrets
Mar 11, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
81b6733
Adding Secrets Analyzer with Comments
manumafe98 a525a40
Adding analyzer tests
manumafe98 14b9042
Adding smoke tests
manumafe98 63b9311
Fixing smoke tests indenting and prefer bitwise not comment javadoc
manumafe98 9bbc36a
Applying suggestions
manumafe98 3c3686d
Adding extra scenario to match all essential comments
manumafe98 48ca228
Updting visit analyzer method to only add one essential commit
manumafe98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/analyzer/exercises/secrets/AvoidConditionalLogic.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package analyzer.exercises.secrets; | ||
|
|
||
| import analyzer.Comment; | ||
|
|
||
| /** | ||
| * @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/secrets/avoid_conditional_logic.md">Markdown Template</a> | ||
| */ | ||
| class AvoidConditionalLogic extends Comment { | ||
|
|
||
| @Override | ||
| public String getKey() { | ||
| return "java.secrets.avoid_conditional_logic"; | ||
| } | ||
|
|
||
| @Override | ||
| public Type getType() { | ||
| return Type.ACTIONABLE; | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/analyzer/exercises/secrets/PreferBitwiseNot.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package analyzer.exercises.secrets; | ||
|
|
||
| import analyzer.Comment; | ||
|
|
||
| /** | ||
| * @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/secrets/prefer_bitwise_not.md">Markdown Template</a> | ||
| */ | ||
| class PreferBitwiseNot extends Comment { | ||
|
|
||
| @Override | ||
| public String getKey() { | ||
| return "java.secrets.prefer_bitwise_not"; | ||
| } | ||
|
|
||
| @Override | ||
| public Type getType() { | ||
| return Type.INFORMATIVE; | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
src/main/java/analyzer/exercises/secrets/SecretsAnalyzer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package analyzer.exercises.secrets; | ||
|
|
||
| import com.github.javaparser.ast.CompilationUnit; | ||
| import com.github.javaparser.ast.body.MethodDeclaration; | ||
| import com.github.javaparser.ast.expr.BinaryExpr; | ||
| import com.github.javaparser.ast.expr.ConditionalExpr; | ||
| import com.github.javaparser.ast.expr.UnaryExpr; | ||
| import com.github.javaparser.ast.stmt.IfStmt; | ||
| import com.github.javaparser.ast.stmt.Statement; | ||
| import com.github.javaparser.ast.visitor.VoidVisitorAdapter; | ||
|
|
||
| import analyzer.Analyzer; | ||
| import analyzer.OutputCollector; | ||
| import analyzer.Solution; | ||
| import analyzer.comments.ExemplarSolution; | ||
|
|
||
| /** | ||
| * The {@link SecretsAnalyzer} is the analyzer implementation for the {@code secrets} practice exercise. | ||
| * It extends from the {@link VoidVisitorAdapter} and uses the visitor pattern to traverse each compilation unit. | ||
| * | ||
| * @see <a href="https://github.com/exercism/java/tree/main/exercises/concept/secrets">The secrets exercise on the Java track</a> | ||
| */ | ||
| public class SecretsAnalyzer extends VoidVisitorAdapter<OutputCollector> implements Analyzer { | ||
| private static final String EXERCISE_NAME = "Secrets"; | ||
| private static final String SHIFT_BACK = "shiftBack"; | ||
| private static final String SET_BITS = "setBits"; | ||
| private static final String FLIP_BITS = "flipBits"; | ||
| private static final String CLEAR_BITS = "clearBits"; | ||
| private boolean essentialCommentAdded = false; | ||
|
|
||
| @Override | ||
| public void analyze(Solution solution, OutputCollector output) { | ||
| for (CompilationUnit compilationUnit : solution.getCompilationUnits()) { | ||
| compilationUnit.accept(this, output); | ||
| } | ||
|
|
||
| if (output.getComments().isEmpty()) { | ||
| output.addComment(new ExemplarSolution(EXERCISE_NAME)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void visit(MethodDeclaration node, OutputCollector output) { | ||
|
|
||
| if (!essentialCommentAdded && node.getNameAsString().equals(SHIFT_BACK) && doesNotUseOperator(node, BinaryExpr.Operator.UNSIGNED_RIGHT_SHIFT)) { | ||
| output.addComment(new UseBitwiseOperator(">>>", SHIFT_BACK)); | ||
| essentialCommentAdded = true; | ||
| } | ||
|
|
||
| if (!essentialCommentAdded && node.getNameAsString().equals(SET_BITS) && doesNotUseOperator(node, BinaryExpr.Operator.BINARY_OR)) { | ||
| output.addComment(new UseBitwiseOperator("|", SET_BITS)); | ||
| essentialCommentAdded = true; | ||
| } | ||
|
|
||
| if (!essentialCommentAdded && node.getNameAsString().equals(FLIP_BITS) && doesNotUseOperator(node, BinaryExpr.Operator.XOR)) { | ||
| output.addComment(new UseBitwiseOperator("^", FLIP_BITS)); | ||
| essentialCommentAdded = true; | ||
| } | ||
|
|
||
| if (!essentialCommentAdded && node.getNameAsString().equals(CLEAR_BITS) && doesNotUseOperator(node, BinaryExpr.Operator.BINARY_AND)) { | ||
| output.addComment(new UseBitwiseOperator("&", CLEAR_BITS)); | ||
| essentialCommentAdded = true; | ||
| } | ||
|
|
||
| if (!essentialCommentAdded && node.getNameAsString().equals(CLEAR_BITS) && !doesNotUseOperator(node, BinaryExpr.Operator.BINARY_AND) && doesNotImplementBitwiseNot(node)) { | ||
| output.addComment(new PreferBitwiseNot()); | ||
| } | ||
|
|
||
| if (!essentialCommentAdded && hasConditional(node)) { | ||
| output.addComment(new AvoidConditionalLogic()); | ||
| } | ||
|
|
||
| super.visit(node, output); | ||
| } | ||
|
|
||
| private static boolean doesNotUseOperator(MethodDeclaration node, BinaryExpr.Operator operator) { | ||
| return node.findAll(BinaryExpr.class, x -> x.getOperator() == operator).isEmpty(); | ||
| } | ||
|
|
||
| private static boolean doesNotImplementBitwiseNot(MethodDeclaration node) { | ||
| return node.findAll(UnaryExpr.class, x -> x.getOperator() == UnaryExpr.Operator.BITWISE_COMPLEMENT).isEmpty(); | ||
| } | ||
|
|
||
| private static boolean hasConditional(MethodDeclaration node) { | ||
| return node.getBody() | ||
| .map(body -> body.getStatements().stream() | ||
| .anyMatch(SecretsAnalyzer::isConditionalExpresion)) | ||
| .orElse(false); | ||
| } | ||
|
|
||
| private static boolean isConditionalExpresion(Statement statement) { | ||
| return !statement.findAll(IfStmt.class).isEmpty() || !statement.findAll(ConditionalExpr.class).isEmpty(); | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
src/main/java/analyzer/exercises/secrets/UseBitwiseOperator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package analyzer.exercises.secrets; | ||
|
|
||
| import analyzer.Comment; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/secrets/use_bitwise_operator.md">Markdown Template</a> | ||
| */ | ||
| class UseBitwiseOperator extends Comment { | ||
| private final String operatorToUse; | ||
| private final String calledMethod; | ||
|
|
||
| public UseBitwiseOperator(String operatorToUse, String calledMethod) { | ||
| this.operatorToUse = operatorToUse; | ||
| this.calledMethod = calledMethod; | ||
| } | ||
|
|
||
| @Override | ||
| public String getKey() { | ||
| return "java.secrets.use_bitwise_operator"; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, String> getParameters() { | ||
| return Map.of( | ||
| "operatorToUse", this.operatorToUse, | ||
| "calledMethod", this.calledMethod | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public Type getType() { | ||
| return Type.ESSENTIAL; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/test/resources/analyzer/AnalyzerIntegrationTest.secrets.ExemplarSolution.approved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "comments": [ | ||
| { | ||
| "comment": "java.general.exemplar", | ||
| "params": { | ||
| "exerciseName": "Secrets" | ||
| }, | ||
| "type": "celebratory" | ||
| } | ||
| ] | ||
| } |
17 changes: 17 additions & 0 deletions
17
...s/analyzer/AnalyzerIntegrationTest.secrets.NotUsingAnyOfTheExpectedOperators.approved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "comments": [ | ||
| { | ||
| "comment": "java.secrets.use_bitwise_operator", | ||
| "params": { | ||
| "calledMethod": "shiftBack", | ||
| "operatorToUse": "\u003e\u003e\u003e" | ||
| }, | ||
| "type": "essential" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/test/resources/analyzer/AnalyzerIntegrationTest.secrets.NotUsingBitwiseAnd.approved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "comments": [ | ||
| { | ||
| "comment": "java.secrets.use_bitwise_operator", | ||
| "params": { | ||
| "calledMethod": "clearBits", | ||
| "operatorToUse": "\u0026" | ||
| }, | ||
| "type": "essential" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/test/resources/analyzer/AnalyzerIntegrationTest.secrets.NotUsingBitwiseNot.approved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "comments": [ | ||
| { | ||
| "comment": "java.secrets.prefer_bitwise_not", | ||
| "params": {}, | ||
| "type": "informative" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/test/resources/analyzer/AnalyzerIntegrationTest.secrets.NotUsingBitwiseOr.approved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "comments": [ | ||
| { | ||
| "comment": "java.secrets.use_bitwise_operator", | ||
| "params": { | ||
| "calledMethod": "setBits", | ||
| "operatorToUse": "|" | ||
| }, | ||
| "type": "essential" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/test/resources/analyzer/AnalyzerIntegrationTest.secrets.NotUsingBitwiseXor.approved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "comments": [ | ||
| { | ||
| "comment": "java.secrets.use_bitwise_operator", | ||
| "params": { | ||
| "calledMethod": "flipBits", | ||
| "operatorToUse": "^" | ||
| }, | ||
| "type": "essential" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
17 changes: 17 additions & 0 deletions
17
...esources/analyzer/AnalyzerIntegrationTest.secrets.NotUsingUnsignedRightShift.approved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "comments": [ | ||
| { | ||
| "comment": "java.secrets.use_bitwise_operator", | ||
| "params": { | ||
| "calledMethod": "shiftBack", | ||
| "operatorToUse": "\u003e\u003e\u003e" | ||
| }, | ||
| "type": "essential" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/test/resources/analyzer/AnalyzerIntegrationTest.secrets.UsingIfStatement.approved.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "comments": [ | ||
| { | ||
| "comment": "java.secrets.avoid_conditional_logic", | ||
| "params": {}, | ||
| "type": "actionable" | ||
| }, | ||
| { | ||
| "comment": "java.general.feedback_request", | ||
| "params": {}, | ||
| "type": "informative" | ||
| } | ||
| ] | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/test/resources/scenarios/secrets/ExemplarSolution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package scenarios.secrets; | ||
|
|
||
| public class Secrets { | ||
| public static int shiftBack(int value, int amount) { | ||
| return value >>> amount; | ||
| } | ||
|
|
||
| public static int setBits(int value, int mask) { | ||
| return value | mask; | ||
| } | ||
|
|
||
| public static int flipBits(int value, int mask) { | ||
| return value ^ mask; | ||
| } | ||
|
|
||
| public static int clearBits(int value, int mask) { | ||
| return value & ~mask; | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/test/resources/scenarios/secrets/NotUsingAnyOfTheExpectedOperators.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package scenarios.secrets; | ||
|
|
||
| public class Secrets { | ||
| public static int shiftBack(int value, int amount) { | ||
| return (value >> amount) & ~(Integer.MIN_VALUE >> (amount - 1)); | ||
| } | ||
|
|
||
| public static int setBits(int value, int mask) { | ||
| return value + mask - (value & mask); | ||
| } | ||
|
|
||
| public static int flipBits(int value, int mask) { | ||
| return (value & ~mask) | (~value & mask); | ||
| } | ||
|
|
||
| public static int clearBits(int value, int mask) { | ||
| return ~(~value | mask); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.