forked from srnagar/azure-tools-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Stop then start on servicebus processor #23
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
Open
Njeriiii
wants to merge
8
commits into
azure-sdk-plugin
Choose a base branch
from
stop-then-start-on-servicebus-processor
base: azure-sdk-plugin
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
09a46bf
initial implementation
Njeriiii 6cfc780
complete implementation with test
Njeriiii 7941091
remove checks for 'close'
Njeriiii 7a2ba0c
refactoring to change logic
Njeriiii 0fcfab1
refactor to check for same method body
Njeriiii 0d0a1be
complete refactor with tests
Njeriiii e1776a6
merge with azure-sdk-plugin branch
Njeriiii 3be28cc
complete refactor to address edge cases
Njeriiii 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
147 changes: 147 additions & 0 deletions
147
...t/azure/toolkit/intellij/azure/sdk/buildtool/StopThenStartOnServiceBusProcessorCheck.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,147 @@ | ||
| package com.microsoft.azure.toolkit.intellij.azure.sdk.buildtool; | ||
|
|
||
| import com.intellij.codeInspection.LocalInspectionTool; | ||
| import com.intellij.codeInspection.ProblemsHolder; | ||
| import com.intellij.psi.JavaElementVisitor; | ||
| import com.intellij.psi.PsiElement; | ||
| import com.intellij.psi.PsiExpression; | ||
| import com.intellij.psi.PsiMethodCallExpression; | ||
| import com.intellij.psi.PsiReferenceExpression; | ||
| import com.intellij.psi.PsiType; | ||
| import com.intellij.psi.PsiVariable; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * This class is a LocalInspectionTool that checks if a stop method is called on a ServiceBusProcessorClient object, followed by a start method call on the same object. | ||
| * If this is the case, a problem is registered with the ProblemsHolder. | ||
| */ | ||
| public class StopThenStartOnServiceBusProcessorCheck extends LocalInspectionTool { | ||
|
|
||
| /** | ||
| * This method builds a visitor that visits the PsiMethodCallExpression and checks if a stop method is called on a ServiceBusProcessorClient object, followed by a start method call on the same object. | ||
| * If this is the case, a problem is registered with the ProblemsHolder. | ||
| * | ||
| * @param holder The ProblemsHolder to register the problem with | ||
| * @param isOnTheFly A boolean that indicates if the inspection is being run on the fly - not used in this implementation but required by the method signature | ||
| * @return A JavaElementVisitor that visits the PsiMethodCallExpression and checks if a stop method is called on a ServiceBusProcessorClient object, followed by a start method call on the same object | ||
| */ | ||
| @NotNull | ||
| @Override | ||
| public JavaElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { | ||
| return new StopThenStartOnServiceBusProcessorVisitor(holder); | ||
| } | ||
|
|
||
| /** | ||
| * This class is a JavaElementVisitor that visits the PsiMethodCallExpression and checks if a stop method is called on a ServiceBusProcessorClient object, followed by a start method call on the same object. | ||
| * If this is the case, a problem is registered with the ProblemsHolder. | ||
| */ | ||
| static class StopThenStartOnServiceBusProcessorVisitor extends JavaElementVisitor { | ||
|
|
||
| // Create a ProblemsHolder to register the problem with | ||
| private final ProblemsHolder holder; | ||
|
|
||
| // Create a map to store a boolean indicating if stop was called on the variable | ||
| private final Map<PsiVariable, Boolean> variableStateMap = new HashMap<>(); | ||
|
|
||
| // Define constants for string literals | ||
| private static final RuleConfig RULE_CONFIG; | ||
| private static final boolean SKIP_WHOLE_RULE; | ||
|
|
||
| // Load the rule configuration | ||
| static { | ||
| final String ruleName = "StopThenStartOnServiceBusProcessorCheck"; | ||
| RuleConfigLoader centralRuleConfigLoader = RuleConfigLoader.getInstance(); | ||
|
|
||
| // Get the RuleConfig object for the rule | ||
| RULE_CONFIG = centralRuleConfigLoader.getRuleConfig(ruleName); | ||
| SKIP_WHOLE_RULE = RULE_CONFIG.skipRuleCheck() || RULE_CONFIG.getClientsToCheck().isEmpty(); | ||
| } | ||
|
|
||
| /** | ||
| * This constructor initializes the Visitor with the ProblemsHolder | ||
| * | ||
| * @param holder The ProblemsHolder to register the problem with | ||
| */ | ||
| StopThenStartOnServiceBusProcessorVisitor(ProblemsHolder holder) { | ||
| this.holder = holder; | ||
| } | ||
|
|
||
| /** | ||
| * This method visits the PsiMethodCallExpression and checks if a stop method is called on a ServiceBusProcessorClient object, followed by a start method call on the same object. | ||
| * If this is the case, a problem is registered with the ProblemsHolder. | ||
| * | ||
| * @param expression The PsiMethodCallExpression to visit | ||
| */ | ||
| @Override | ||
| public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) { | ||
| super.visitMethodCallExpression(expression); | ||
|
|
||
| if (SKIP_WHOLE_RULE) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if the method being called is 'stop' or 'start' | ||
| PsiReferenceExpression methodExpression = expression.getMethodExpression(); | ||
| String methodName = methodExpression.getReferenceName(); | ||
|
|
||
| if (!(RULE_CONFIG.getMethodsToCheck().contains(methodName))) { | ||
| return; | ||
| } | ||
|
|
||
| // Get the qualifier of the method call - the object on which the method is called | ||
| PsiExpression qualifier = methodExpression.getQualifierExpression(); | ||
|
|
||
| if (!(qualifier instanceof PsiReferenceExpression)) { | ||
| return; | ||
| } | ||
| PsiElement reference = ((PsiReferenceExpression) qualifier).resolve(); | ||
|
|
||
| if (!(reference instanceof PsiVariable)) { | ||
| return; | ||
| } | ||
|
|
||
| // Get the variable that the method is called on | ||
| PsiVariable variable = (PsiVariable) reference; | ||
|
|
||
| // Check if the variable is a ServiceBusProcessorClient | ||
| if (!(isServiceBusProcessorClient(variable))) { | ||
| return; | ||
| } | ||
|
|
||
| // Boolean indicating if stop was called on the variable | ||
| Boolean wasStopCalled = variableStateMap.get(variable); | ||
|
|
||
| // If 'stop' is called, mark that 'stop' was called on the variable | ||
| if ("stop".equals(methodName)) { | ||
| variableStateMap.put(variable, true); // Mark that stop was called | ||
|
|
||
| // If 'close' is called, remove the variable from the map -- the resource is closed and the variable is no longer in use | ||
| } else if ("close".equals(methodName)) { | ||
| variableStateMap.remove(variable); // Remove the variable from the map | ||
|
|
||
| // If 'start' is called and 'stop' was called on the variable, register a problem | ||
| } else if ("start".equals(methodName) && Boolean.TRUE.equals(wasStopCalled)) { | ||
| holder.registerProblem(expression, RULE_CONFIG.getAntiPatternMessageMap().get("antiPatternMessage")); | ||
|
|
||
| // Reset the state after reporting the problem | ||
| variableStateMap.remove(variable); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * This method checks if the type of the variable is ServiceBusProcessorClient | ||
| * | ||
| * @param variable The variable to check | ||
| * @return A boolean indicating if the type of the variable is ServiceBusProcessorClient | ||
| */ | ||
| private static boolean isServiceBusProcessorClient(PsiVariable variable) { | ||
|
|
||
| PsiType type = variable.getType(); | ||
| String typeText = type.getCanonicalText(); | ||
| return typeText != null && typeText.contains(RULE_CONFIG.getClientsToCheck().get(0)) && typeText.startsWith(RuleConfig.AZURE_PACKAGE_NAME); | ||
| } | ||
| } | ||
| } | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how do we know if start was actually called before stop?
e.g. would is complain if I do
?