|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.microsoft.azure.toolkit.intellij.java.sdk.analyzer; |
| 5 | + |
| 6 | +import com.intellij.codeInspection.LocalInspectionTool; |
| 7 | +import com.intellij.codeInspection.ProblemsHolder; |
| 8 | +import com.intellij.psi.JavaElementVisitor; |
| 9 | +import com.intellij.psi.PsiDeclarationStatement; |
| 10 | +import com.intellij.psi.PsiElement; |
| 11 | +import com.intellij.psi.PsiElementVisitor; |
| 12 | +import com.intellij.psi.PsiExpression; |
| 13 | +import com.intellij.psi.PsiMethodCallExpression; |
| 14 | +import com.intellij.psi.PsiReferenceExpression; |
| 15 | +import com.intellij.psi.PsiType; |
| 16 | +import com.intellij.psi.PsiVariable; |
| 17 | +import com.microsoft.azure.toolkit.intellij.java.sdk.models.RuleConfig; |
| 18 | +import com.microsoft.azure.toolkit.intellij.java.sdk.utils.HelperUtils; |
| 19 | +import com.microsoft.azure.toolkit.intellij.java.sdk.utils.RuleConfigLoader; |
| 20 | +import java.util.Map; |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | + |
| 23 | +/** |
| 24 | + * This class extends the LocalInspectionTool and is used to inspect the usage of Azure SDK ServiceBusReceiver & |
| 25 | + * ServiceBusProcessor clients in the code. It checks if the auto-complete feature is disabled for the clients. If the |
| 26 | + * auto-complete feature is not disabled, a problem is registered with the ProblemsHolder. |
| 27 | + */ |
| 28 | +public class DisableAutoCompleteCheck extends LocalInspectionTool { |
| 29 | + |
| 30 | + /** |
| 31 | + * Build the visitor for the inspection. This visitor will be used to traverse the PSI tree. |
| 32 | + * |
| 33 | + * @param holder The holder for the problems found |
| 34 | + * |
| 35 | + * @return The visitor for the inspection. This is not used anywhere else in the code. |
| 36 | + */ |
| 37 | + @NotNull |
| 38 | + @Override |
| 39 | + public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { |
| 40 | + return new DisableAutoCompleteVisitor(holder, RuleConfigLoader.getInstance().getRuleConfigs()); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * This class extends the JavaElementVisitor and is used to visit the Java elements in the code. It checks for the |
| 45 | + * usage of Azure SDK ServiceBusReceiver & ServiceBusProcessor clients and whether the auto-complete feature is |
| 46 | + * disabled. If the auto-complete feature is not disabled, a problem is registered with the ProblemsHolder. |
| 47 | + */ |
| 48 | + static class DisableAutoCompleteVisitor extends JavaElementVisitor { |
| 49 | + |
| 50 | + private static RuleConfig RULE_CONFIG; |
| 51 | + private static boolean SKIP_WHOLE_RULE; |
| 52 | + private final ProblemsHolder holder; |
| 53 | + |
| 54 | + DisableAutoCompleteVisitor(ProblemsHolder holder, Map<String, RuleConfig> ruleConfigs) { |
| 55 | + this.holder = holder; |
| 56 | + initializeRuleConfig(ruleConfigs); |
| 57 | + } |
| 58 | + |
| 59 | + private void initializeRuleConfig(Map<String, RuleConfig> ruleConfigs) { |
| 60 | + if (RULE_CONFIG == null) { |
| 61 | + final String ruleName = "DisableAutoCompleteCheck"; |
| 62 | + RULE_CONFIG = ruleConfigs.get(ruleName); |
| 63 | + SKIP_WHOLE_RULE = RULE_CONFIG.isSkipRuleCheck(); |
| 64 | + } |
| 65 | + } |
| 66 | + /** |
| 67 | + * This method is used to visit the declaration statements in the code. It checks for the declaration of Azure |
| 68 | + * SDK ServiceBusReceiver & ServiceBusProcessor clients and whether the auto-complete feature is disabled. If |
| 69 | + * the auto-complete feature is not disabled, a problem is registered with the ProblemsHolder. |
| 70 | + * |
| 71 | + * @param statement The declaration statement to visit |
| 72 | + */ |
| 73 | + @Override |
| 74 | + public void visitDeclarationStatement(PsiDeclarationStatement statement) { |
| 75 | + |
| 76 | + if (SKIP_WHOLE_RULE) { |
| 77 | + return; |
| 78 | + } |
| 79 | + super.visitDeclarationStatement(statement); |
| 80 | + |
| 81 | + // Get the declared elements |
| 82 | + PsiElement[] elements = statement.getDeclaredElements(); |
| 83 | + |
| 84 | + // Get the variable declaration |
| 85 | + if (elements.length > 0 && elements[0] instanceof PsiVariable) { |
| 86 | + PsiVariable variable = (PsiVariable) elements[0]; |
| 87 | + |
| 88 | + // Process the variable declaration |
| 89 | + processVariableDeclaration(variable); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * This method is used to process the variable declaration. It checks for the declaration of Azure SDK |
| 95 | + * ServiceBusReceiver & ServiceBusProcessor clients and whether the auto-complete feature is disabled. If the |
| 96 | + * auto-complete feature is not disabled, a problem is registered with the ProblemsHolder. |
| 97 | + * |
| 98 | + * @param variable The variable to process |
| 99 | + */ |
| 100 | + private void processVariableDeclaration(PsiVariable variable) { |
| 101 | + |
| 102 | + // Retrieve the client name (left side of the declaration) |
| 103 | + PsiType clientType = variable.getType(); |
| 104 | + |
| 105 | + // Check the assignment part (right side) |
| 106 | + PsiExpression initializer = variable.getInitializer(); |
| 107 | + |
| 108 | + // Check if the client type is an Azure SDK client |
| 109 | + if (HelperUtils.isAzurePackage(clientType.getCanonicalText())) { |
| 110 | + if (HelperUtils.checkIfInScope(RULE_CONFIG.getScopeToCheck(), clientType.getPresentableText())) { |
| 111 | + if (initializer instanceof PsiMethodCallExpression) { |
| 112 | + if (!isAutoCompleteDisabled((PsiMethodCallExpression) initializer)) { |
| 113 | + holder.registerProblem(initializer, RULE_CONFIG.getAntiPatternMessage()); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * This method is used to check if the auto-complete feature is disabled. It iterates up the chain of method |
| 122 | + * calls to check if the auto-complete feature is disabled. |
| 123 | + * |
| 124 | + * @param methodCallExpression The method call expression to check |
| 125 | + * |
| 126 | + * @return true if the auto-complete feature is disabled, false otherwise |
| 127 | + */ |
| 128 | + private static boolean isAutoCompleteDisabled(PsiMethodCallExpression methodCallExpression) { |
| 129 | + |
| 130 | + // Iterating up the chain of method calls |
| 131 | + PsiExpression qualifier = methodCallExpression.getMethodExpression().getQualifierExpression(); |
| 132 | + |
| 133 | + // Check if the method call chain has the method to check |
| 134 | + while (qualifier instanceof PsiMethodCallExpression) { |
| 135 | + |
| 136 | + if (qualifier instanceof PsiMethodCallExpression) { |
| 137 | + |
| 138 | + // Get the method expression |
| 139 | + PsiReferenceExpression methodExpression = |
| 140 | + ((PsiMethodCallExpression) qualifier).getMethodExpression(); |
| 141 | + |
| 142 | + // Get the method name |
| 143 | + String methodName = methodExpression.getReferenceName(); |
| 144 | + |
| 145 | + // Check if the method name is the method to check |
| 146 | + if (RULE_CONFIG.getUsagesToCheck().contains(methodName)) { |
| 147 | + return true; |
| 148 | + } |
| 149 | + } |
| 150 | + qualifier = ((PsiMethodCallExpression) qualifier).getMethodExpression().getQualifierExpression(); |
| 151 | + } |
| 152 | + // When the chain has been traversed and the method to check is not found |
| 153 | + return false; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments