-
Notifications
You must be signed in to change notification settings - Fork 5
Issue #118: Implement HexLiterialCase Recipe #119
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 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
Some comments aren't visible on the classic Files Changed page.
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
136 changes: 136 additions & 0 deletions
136
src/main/java/org/checkstyle/autofix/recipe/HexLiteralCase.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,136 @@ | ||
| /////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite. | ||
| // Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| /////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| package org.checkstyle.autofix.recipe; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
|
|
||
| import org.checkstyle.autofix.PositionHelper; | ||
| import org.checkstyle.autofix.parser.CheckstyleViolation; | ||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.JavaType; | ||
|
|
||
| /** | ||
| * Fixes Checkstyle HexLiteralCase violations by replacing hexadecimal lowercase literals | ||
| * with uppercase literals. | ||
| */ | ||
| public class HexLiteralCase extends Recipe { | ||
|
|
||
| private final List<CheckstyleViolation> violations; | ||
|
|
||
| public HexLiteralCase(List<CheckstyleViolation> violations) { | ||
| this.violations = violations; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "HexLiteralCase Recipe"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Replace HexLiteral lowercase (a-f) with UpperCase (A-F) " | ||
| + "to improve readability."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return new HexLiteralCase.HexLiteralCaseVisitor(); | ||
| } | ||
|
|
||
| private final class HexLiteralCaseVisitor extends JavaIsoVisitor<ExecutionContext> { | ||
|
|
||
| private static final String HEX_PREFIX = "0x"; | ||
|
|
||
| private Path sourcePath; | ||
|
|
||
| @Override | ||
| public J.CompilationUnit visitCompilationUnit( | ||
| J.CompilationUnit cu, ExecutionContext executionContext) { | ||
| this.sourcePath = cu.getSourcePath().toAbsolutePath(); | ||
| return super.visitCompilationUnit(cu, executionContext); | ||
| } | ||
|
|
||
| @Override | ||
| public J.Literal visitLiteral(J.Literal literal, ExecutionContext executionContext) { | ||
| J.Literal result = super.visitLiteral(literal, executionContext); | ||
| final String valueSource = result.getValueSource(); | ||
|
|
||
| if (shouldProcessLiteral(result, valueSource)) { | ||
| final String newValueSource = convertLowercaseHexToUppercase(valueSource); | ||
| result = result.withValueSource(newValueSource); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Determines whether the given literal should be processed based on its type and format. | ||
| * | ||
| * @param literal the {@link J.Literal} node to check | ||
| * @param valueSource the source value of the literal as a string | ||
| * @return {@code true} if the literal meets the criteria for processing, | ||
| * {@code false} otherwise | ||
| */ | ||
| private boolean shouldProcessLiteral(J.Literal literal, String valueSource) { | ||
| return valueSource != null | ||
| && (valueSource.startsWith(HEX_PREFIX) | ||
| || valueSource.startsWith(HEX_PREFIX.toUpperCase(Locale.ROOT))) | ||
| && (literal.getType() == JavaType.Primitive.Long | ||
| || literal.getType() == JavaType.Primitive.Int) | ||
| && isAtViolationLocation(literal); | ||
| } | ||
|
|
||
| /** | ||
| * Converts any lowercase hexadecimal letters (a-f) to uppercase (A-F). | ||
| * | ||
| * @param valueSource the original literal source | ||
| * @return a new uppercase version if modified, or {@code null} if no changes were made | ||
| */ | ||
| private String convertLowercaseHexToUppercase(String valueSource) { | ||
| final String prefix = valueSource.substring(0, HEX_PREFIX.length()); | ||
| String result = prefix | ||
| + valueSource.substring(prefix.length()).toUpperCase(Locale.ROOT); | ||
|
|
||
| // Avoid extra cycle by skipping identical replacements | ||
| if (result.equals(valueSource)) { | ||
| result = valueSource; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private boolean isAtViolationLocation(J.Literal literal) { | ||
| final J.CompilationUnit cursor = getCursor().firstEnclosing(J.CompilationUnit.class); | ||
|
|
||
| final int line = PositionHelper.computeLinePosition(cursor, literal, getCursor()); | ||
| final int column = PositionHelper.computeColumnPosition(cursor, literal, getCursor()); | ||
|
|
||
| return violations.stream().anyMatch(violation -> { | ||
| final Path absolutePath = Path.of(violation.getFileName()).toAbsolutePath(); | ||
| return violation.getLine() == line | ||
| && violation.getColumn() == column | ||
| && absolutePath.endsWith(sourcePath); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
src/test/java/org/checkstyle/autofix/recipe/HexLiteralCaseTest.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,33 @@ | ||
| /////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite. | ||
| // Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| /////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| package org.checkstyle.autofix.recipe; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class HexLiteralCaseTest extends AbstractRecipeTestSupport { | ||
|
|
||
| @Override | ||
| protected String getSubpackage() { | ||
| return "hexliterialcase"; | ||
| } | ||
|
|
||
| @Test | ||
| void hexLiteral() throws Exception { | ||
| verify("HexLiteralCase"); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
...rces/org/checkstyle/autofix/recipe/hexliterialcase/hexliteralcase/DiffHexLiteralCase.diff
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,38 @@ | ||
| --- src/test/resources/org/checkstyle/autofix/recipe/hexliterialcase/hexliteralcase/InputHexLiteralCase.java | ||
| +++ src/test/resources/org/checkstyle/autofix/recipe/hexliterialcase/hexliteralcase/OutputHexLiteralCase.java | ||
| @@ -9,24 +9,21 @@ | ||
|
|
||
| package org.checkstyle.autofix.recipe.hexliterialcase.hexliteralcase; | ||
|
|
||
| -public class InputHexLiteralCase { | ||
| - int h = 0xb; // violation 'Should use uppercase hexadecimal letters.' | ||
| +public class OutputHexLiteralCase { | ||
| + int h = 0xB; | ||
| int w = 0xB; | ||
| - long d = 0Xf123L; // violation 'Should use uppercase hexadecimal letters.' | ||
| - byte b1 = 0x1b; // violation 'Should use uppercase hexadecimal letters.' | ||
| + long d = 0XF123L; | ||
| + byte b1 = 0x1B; | ||
| byte b2 = 0x1B; | ||
| - short s2 = 0xF5f; // violation 'Should use uppercase hexadecimal letters.' | ||
| - int i1 = 0x11 + 0xabc; // violation 'Should use uppercase hexadecimal letters.' | ||
| - long l1 = 0x7fff_ffff_ffff_ffffL; | ||
| - // violation above 'Should use uppercase hexadecimal letters.' | ||
| - long l2 = 0x7FFF_AAA_bBB_DDDL; // violation 'Should use uppercase hexadecimal letters.' | ||
| - // violation below 'Should use uppercase hexadecimal letters.' | ||
| - int val = 0x1b, sum = 0xff; // violation 'Should use uppercase hexadecimal letters.' | ||
| - int x1 = 223, x2 = 0xa1; // violation 'Should use uppercase hexadecimal letters.' | ||
| + short s2 = 0xF5F; | ||
| + int i1 = 0x11 + 0xABC; | ||
| + long l1 = 0x7FFF_FFFF_FFFF_FFFFL; | ||
| + long l2 = 0x7FFF_AAA_BBB_DDDL; | ||
| + int val = 0x1B, sum = 0xFF; | ||
| + int x1 = 223, x2 = 0xA1; | ||
|
|
||
| private static void functionCall() { | ||
| - // violation below 'Should use uppercase hexadecimal letters.' | ||
| - functionCall2(0x7fff, 0xab); // violation 'Should use uppercase hexadecimal letters.' | ||
| + functionCall2(0x7FFF, 0xAB); | ||
| } | ||
| private static void functionCall2(int first, int second) { | ||
| System.out.println("First: " + first); |
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.