|
| 1 | +/////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | +// checkstyle-openrewrite-recipes: Automatically fix Checkstyle violations with OpenRewrite. |
| 3 | +// Copyright (C) 2025 The Checkstyle OpenRewrite Recipes Authors |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +/////////////////////////////////////////////////////////////////////////////////////////////// |
| 17 | + |
| 18 | +package org.checkstyle.autofix.recipe; |
| 19 | + |
| 20 | +import java.nio.file.Path; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Locale; |
| 23 | + |
| 24 | +import org.checkstyle.autofix.PositionHelper; |
| 25 | +import org.checkstyle.autofix.parser.CheckstyleViolation; |
| 26 | +import org.openrewrite.ExecutionContext; |
| 27 | +import org.openrewrite.Recipe; |
| 28 | +import org.openrewrite.TreeVisitor; |
| 29 | +import org.openrewrite.java.JavaIsoVisitor; |
| 30 | +import org.openrewrite.java.tree.J; |
| 31 | +import org.openrewrite.java.tree.JavaType; |
| 32 | + |
| 33 | +/** |
| 34 | + * Fixes Checkstyle HexLiteralCase violations by replacing hexadecimal lowercase literals |
| 35 | + * with uppercase literals. |
| 36 | + */ |
| 37 | +public class HexLiteralCase extends Recipe { |
| 38 | + |
| 39 | + private final List<CheckstyleViolation> violations; |
| 40 | + |
| 41 | + public HexLiteralCase(List<CheckstyleViolation> violations) { |
| 42 | + this.violations = violations; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String getDisplayName() { |
| 47 | + return "HexLiteralCase Recipe"; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getDescription() { |
| 52 | + return "Replace HexLiteral lowercase (a-f) with UpperCase (A-F) " |
| 53 | + + "to improve readability."; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 58 | + return new HexLiteralCase.HexLiteralCaseVisitor(); |
| 59 | + } |
| 60 | + |
| 61 | + private final class HexLiteralCaseVisitor extends JavaIsoVisitor<ExecutionContext> { |
| 62 | + |
| 63 | + private static final String HEX_PREFIX = "0x"; |
| 64 | + |
| 65 | + private Path sourcePath; |
| 66 | + |
| 67 | + @Override |
| 68 | + public J.CompilationUnit visitCompilationUnit( |
| 69 | + J.CompilationUnit cu, ExecutionContext executionContext) { |
| 70 | + this.sourcePath = cu.getSourcePath().toAbsolutePath(); |
| 71 | + return super.visitCompilationUnit(cu, executionContext); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public J.Literal visitLiteral(J.Literal literal, ExecutionContext executionContext) { |
| 76 | + J.Literal result = super.visitLiteral(literal, executionContext); |
| 77 | + final String valueSource = result.getValueSource(); |
| 78 | + |
| 79 | + if (shouldProcessLiteral(result, valueSource)) { |
| 80 | + final String newValueSource = convertLowercaseHexToUppercase(valueSource); |
| 81 | + result = result.withValueSource(newValueSource); |
| 82 | + } |
| 83 | + |
| 84 | + return result; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Determines whether the given literal should be processed based on its type and format. |
| 89 | + * |
| 90 | + * @param literal the {@link J.Literal} node to check |
| 91 | + * @param valueSource the source value of the literal as a string |
| 92 | + * @return {@code true} if the literal meets the criteria for processing, |
| 93 | + * {@code false} otherwise |
| 94 | + */ |
| 95 | + private boolean shouldProcessLiteral(J.Literal literal, String valueSource) { |
| 96 | + return valueSource != null |
| 97 | + && (valueSource.startsWith(HEX_PREFIX) |
| 98 | + || valueSource.startsWith(HEX_PREFIX.toUpperCase(Locale.ROOT))) |
| 99 | + && (literal.getType() == JavaType.Primitive.Long |
| 100 | + || literal.getType() == JavaType.Primitive.Int) |
| 101 | + && isAtViolationLocation(literal); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Converts any lowercase hexadecimal letters (a-f) to uppercase (A-F). |
| 106 | + * |
| 107 | + * @param valueSource the original literal source |
| 108 | + * @return a new uppercase version if modified, or {@code null} if no changes were made |
| 109 | + */ |
| 110 | + private String convertLowercaseHexToUppercase(String valueSource) { |
| 111 | + final String prefix = valueSource.substring(0, HEX_PREFIX.length()); |
| 112 | + String result = prefix |
| 113 | + + valueSource.substring(prefix.length()).toUpperCase(Locale.ROOT); |
| 114 | + |
| 115 | + // Avoid extra cycle by skipping identical replacements |
| 116 | + if (result.equals(valueSource)) { |
| 117 | + result = valueSource; |
| 118 | + } |
| 119 | + return result; |
| 120 | + } |
| 121 | + |
| 122 | + private boolean isAtViolationLocation(J.Literal literal) { |
| 123 | + final J.CompilationUnit cursor = getCursor().firstEnclosing(J.CompilationUnit.class); |
| 124 | + |
| 125 | + final int line = PositionHelper.computeLinePosition(cursor, literal, getCursor()); |
| 126 | + final int column = PositionHelper.computeColumnPosition(cursor, literal, getCursor()); |
| 127 | + |
| 128 | + return violations.stream().anyMatch(violation -> { |
| 129 | + final Path absolutePath = Path.of(violation.getFileName()).toAbsolutePath(); |
| 130 | + return violation.getLine() == line |
| 131 | + && violation.getColumn() == column |
| 132 | + && absolutePath.endsWith(sourcePath); |
| 133 | + }); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments