-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(consensus,framework,actuator): use Locale.ROOT for case-insensitive #6698
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
halibobo1205
wants to merge
1
commit into
tronprotocol:develop
Choose a base branch
from
halibobo1205:fix/locale-sensitive-case
base: develop
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 all commits
Commits
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
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
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
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,13 @@ | ||
| if (!JavaVersion.current().isJava11Compatible()) { | ||
| // ErrorProne core requires JDK 11+; skip this module on JDK 8 | ||
| tasks.withType(JavaCompile).configureEach { enabled = false } | ||
| tasks.withType(Jar).configureEach { enabled = false } | ||
| } else { | ||
| dependencies { | ||
| compileOnly "com.google.errorprone:error_prone_annotations:${errorproneVersion}" | ||
| compileOnly "com.google.errorprone:error_prone_check_api:${errorproneVersion}" | ||
| compileOnly "com.google.errorprone:error_prone_core:${errorproneVersion}" | ||
| compileOnly "com.google.auto.service:auto-service:1.1.1" | ||
| annotationProcessor "com.google.auto.service:auto-service:1.1.1" | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
errorprone/src/main/java/errorprone/StringCaseLocaleUsageMethodRef.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,56 @@ | ||
| package errorprone; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import com.google.errorprone.BugPattern; | ||
| import com.google.errorprone.VisitorState; | ||
| import com.google.errorprone.bugpatterns.BugChecker; | ||
| import com.google.errorprone.matchers.Description; | ||
| import com.google.errorprone.util.ASTHelpers; | ||
| import com.sun.source.tree.MemberReferenceTree; | ||
| import com.sun.tools.javac.code.Symbol; | ||
| import com.sun.tools.javac.code.Type; | ||
|
|
||
| /** | ||
| * Flags method references {@code String::toLowerCase} and {@code String::toUpperCase} | ||
| * that resolve to the no-arg overload (which uses {@code Locale.getDefault()}). | ||
| * | ||
| * <p>The built-in ErrorProne {@code StringCaseLocaleUsage} checker only catches | ||
| * direct method invocations ({@code s.toLowerCase()}), not method references | ||
| * ({@code String::toLowerCase}). This checker closes that gap. | ||
| */ | ||
| @AutoService(BugChecker.class) | ||
| @BugPattern( | ||
| name = "StringCaseLocaleUsageMethodRef", | ||
| summary = "String::toLowerCase and String::toUpperCase method references use " | ||
| + "Locale.getDefault(). Replace with a lambda that specifies Locale.ROOT, " | ||
| + "e.g. s -> s.toLowerCase(Locale.ROOT).", | ||
| severity = BugPattern.SeverityLevel.ERROR | ||
| ) | ||
| public class StringCaseLocaleUsageMethodRef extends BugChecker | ||
| implements BugChecker.MemberReferenceTreeMatcher { | ||
|
|
||
| @Override | ||
| public Description matchMemberReference(MemberReferenceTree tree, VisitorState state) { | ||
| String name = tree.getName().toString(); | ||
| if (!"toLowerCase".equals(name) && !"toUpperCase".equals(name)) { | ||
| return Description.NO_MATCH; | ||
| } | ||
| // Verify the qualifier type is java.lang.String | ||
| Type qualifierType = ((com.sun.tools.javac.tree.JCTree) tree.getQualifierExpression()) | ||
| .type; | ||
| if (qualifierType == null) { | ||
| return Description.NO_MATCH; | ||
| } | ||
| if (!state.getTypes().isSameType( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [NIT] The idiomatic ErrorProne way to compare types is via if (!ASTHelpers.isSameType(qualifierType, state.getSymtab().stringType, state)) {
return Description.NO_MATCH;
}Or, using the standard supplier for an even shorter form: import static com.google.errorprone.suppliers.Suppliers.STRING_TYPE;
...
if (!ASTHelpers.isSameType(qualifierType, STRING_TYPE.get(state), state)) { ... }Functionally identical, but matches the convention used by ErrorProne's own built-in checkers — easier for future maintainers to recognize. |
||
| qualifierType, state.getSymtab().stringType)) { | ||
| return Description.NO_MATCH; | ||
| } | ||
| // Only flag the no-arg overload; the Locale-taking overload is safe | ||
| Symbol sym = ASTHelpers.getSymbol(tree); | ||
| if (sym instanceof Symbol.MethodSymbol | ||
| && ((Symbol.MethodSymbol) sym).getParameters().isEmpty()) { | ||
| return describeMatch(tree); | ||
| } | ||
| return Description.NO_MATCH; | ||
| } | ||
| } | ||
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.
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.
@halibobo1205,
[SHOULD] Avoid casting directly to
com.sun.tools.javac.tree.JCTreeto read the internal.typefield. ErrorProne already wraps this inASTHelpers.getType():Benefits:
com.sun.tools.javac.tree.JCTree(requires--add-exportson JDK 9+ and is more likely to break across JDK versions).ASTHelpers.getSymbol(tree)style already used a few lines below.The remaining
com.sun.tools.javac.code.Symbol/Typeimports still belong here (that's the standard BugChecker SDK), but dropping the explicitJCTreecast is a clear win.