-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Custom CheckStyle Rule #9: Thrown exception use ClientLogger #4566
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
Merged
Changes from 29 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
4cac1b9
remove java code isImple check but move to suppression and add only c…
9dbeae5
resolve conflict
401bdff
init drapft
ac53586
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
d0fb4d1
remove no checking in implementation and track 2 packages
d44fa72
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
3ce4a05
Custom checkstyle rule init
922d5f0
service code changes due to the new rule
66ca9a1
throw client logger check feedback
186ca53
non serializable suppression
64ad11c
josh and serkanta changes request
6a40f58
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
2472dcc
update changes
69172a8
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
07f6795
no test checking in ThrowFromClientLoggerCheck
a9ed323
revert all test files
26a187c
reslove suppression conflict
ce80f3c
fix return check and updates suppression file
89f690e
revert samples folder
23f5f29
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
78328bb
use leaveToken instead of self-tree-traversal
315d21f
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
6783b06
more documents and fix a few error from service side
98c7a18
resolve conflict
90d6133
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
2f4515d
resolve conflict
b6a6068
fixes for logger for all service, missing @ServiceClientBuilder for s…
b733ddf
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
d6375fd
minor fix for comment
9f5b68d
resolve conflict
74e02af
fixes on some suppression and updates error message
e37db8d
resolve conflict
006adeb
add a minor change
806f352
revert bas
ad6ec32
remove new line
c649846
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
3b3eac1
fixes some checkstyle erros
f64330c
Merge branch 'master' into CS-Logger
mssfang c560c49
resolve conflict
02aab40
reordered
94304ed
new line matters
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
110 changes: 110 additions & 0 deletions
110
...y-reports/src/main/java/com/azure/tools/checkstyle/checks/ThrowFromClientLoggerCheck.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,110 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.tools.checkstyle.checks; | ||
|
|
||
| import com.puppycrawl.tools.checkstyle.api.AbstractCheck; | ||
| import com.puppycrawl.tools.checkstyle.api.DetailAST; | ||
| import com.puppycrawl.tools.checkstyle.api.FullIdent; | ||
| import com.puppycrawl.tools.checkstyle.api.TokenTypes; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.Deque; | ||
|
|
||
| /** | ||
| * To throw an exception, Must throw it through a 'logger.logExceptionAsError', rather than by directly calling 'throw exception'. | ||
| * | ||
| * Skip check if throwing exception from | ||
| * <ol> | ||
| * <li>Static method</li> | ||
| * <li>Static class</li> | ||
| * <li>Constructor</li> | ||
| * </ol> | ||
| */ | ||
| public class ThrowFromClientLoggerCheck extends AbstractCheck { | ||
mssfang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private static final String LOGGER_LOG_EXCEPTION_AS_ERROR = "logger.logExceptionAsError"; | ||
| private static final String LOGGER_LOG_EXCEPTION_AS_WARNING = "logger.logExceptionAsWarning"; | ||
| private static final String THROW_lOGGER_EXCEPTION_MESSAGE = "Directly throwing an exception is disallowed. Must throw through either ''%s'' or ''%s''."; | ||
mssfang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // A container stores the static status of class, skip this ThrowFromClientLoggerCheck if the class is static | ||
| private final Deque<Boolean> classStaticDeque = new ArrayDeque<>(); | ||
| // A container stores the static status of method, skip this ThrowFromClientLoggerCheck if the method is static | ||
| private final Deque<Boolean> methodStaticDeque = new ArrayDeque<>(); | ||
| // The variable is used to indicate if current node is still inside of constructor. | ||
| private boolean isInConstructor = false; | ||
|
|
||
| @Override | ||
| public int[] getDefaultTokens() { | ||
| return getRequiredTokens(); | ||
| } | ||
|
|
||
| @Override | ||
| public int[] getAcceptableTokens() { | ||
| return getRequiredTokens(); | ||
| } | ||
|
|
||
| @Override | ||
| public int[] getRequiredTokens() { | ||
| return new int[] { | ||
| TokenTypes.CLASS_DEF, | ||
| TokenTypes.CTOR_DEF, | ||
| TokenTypes.LITERAL_THROW, | ||
| TokenTypes.METHOD_DEF | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public void leaveToken(DetailAST token) { | ||
| switch (token.getType()) { | ||
| case TokenTypes.CLASS_DEF: | ||
| classStaticDeque.pop(); | ||
| break; | ||
| case TokenTypes.CTOR_DEF: | ||
| isInConstructor = false; | ||
| break; | ||
| case TokenTypes.METHOD_DEF: | ||
| methodStaticDeque.pop(); | ||
| break; | ||
| default: | ||
| // Checkstyle complains if there's no default block in switch | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void visitToken(DetailAST token) { | ||
| switch (token.getType()) { | ||
| case TokenTypes.CLASS_DEF: | ||
| DetailAST modifiersToken = token.findFirstToken(TokenTypes.MODIFIERS); | ||
| classStaticDeque.addLast(modifiersToken.branchContains(TokenTypes.LITERAL_STATIC)); | ||
| break; | ||
| case TokenTypes.CTOR_DEF: | ||
| isInConstructor = true; | ||
| break; | ||
| case TokenTypes.METHOD_DEF: | ||
| DetailAST methodModifiersToken = token.findFirstToken(TokenTypes.MODIFIERS); | ||
| methodStaticDeque.addLast(methodModifiersToken.branchContains(TokenTypes.LITERAL_STATIC)); | ||
| break; | ||
| case TokenTypes.LITERAL_THROW: | ||
| // Skip check if the throw exception from static class, constructor or static method | ||
| if (classStaticDeque.isEmpty() || classStaticDeque.peekLast() || isInConstructor | ||
| || methodStaticDeque.isEmpty() || methodStaticDeque.peekLast()) { | ||
| return; | ||
| } | ||
| DetailAST methodCallToken = token.findFirstToken(TokenTypes.EXPR).findFirstToken(TokenTypes.METHOD_CALL); | ||
| if (methodCallToken == null) { | ||
| log(token, String.format(THROW_lOGGER_EXCEPTION_MESSAGE, LOGGER_LOG_EXCEPTION_AS_ERROR, LOGGER_LOG_EXCEPTION_AS_WARNING)); | ||
| return; | ||
| } | ||
|
|
||
| String methodCallName = FullIdent.createFullIdent(methodCallToken.findFirstToken(TokenTypes.DOT)).getText(); | ||
| if (!LOGGER_LOG_EXCEPTION_AS_ERROR.equals(methodCallName) && !LOGGER_LOG_EXCEPTION_AS_WARNING.equals(methodCallName)) { | ||
| log(token, String.format(THROW_lOGGER_EXCEPTION_MESSAGE, LOGGER_LOG_EXCEPTION_AS_ERROR, LOGGER_LOG_EXCEPTION_AS_WARNING)); | ||
| } | ||
| break; | ||
| default: | ||
| // Checkstyle complains if there's no default block in switch | ||
| break; | ||
| } | ||
| } | ||
| } | ||
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
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.
Is this no longer necessary as you are instead configuring it via suppressions?
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.
Yes. you are right.