Skip to content

Commit 7caa68b

Browse files
Aman-Baliyanromani
authored andcommitted
Issue #144: Update HexLiteral for hex Float
1 parent 28a3671 commit 7caa68b

File tree

12 files changed

+196
-119
lines changed

12 files changed

+196
-119
lines changed

config/pitest-suppressions.xml

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -595,15 +595,6 @@
595595
<lineContent>return &quot;HexLiteralCase Recipe&quot;;</lineContent>
596596
</mutation>
597597

598-
<mutation unstable="false">
599-
<sourceFile>HexLiteralCase.java</sourceFile>
600-
<mutatedClass>org.checkstyle.autofix.recipe.HexLiteralCase$HexLiteralCaseVisitor</mutatedClass>
601-
<mutatedMethod>isAtViolationLocation</mutatedMethod>
602-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.returns.BooleanTrueReturnValsMutator</mutator>
603-
<description>replaced boolean return with true for org/checkstyle/autofix/recipe/HexLiteralCase$HexLiteralCaseVisitor::isAtViolationLocation</description>
604-
<lineContent>return violations.stream().anyMatch(violation -&gt; {</lineContent>
605-
</mutation>
606-
607598
<mutation unstable="false">
608599
<sourceFile>HexLiteralCase.java</sourceFile>
609600
<mutatedClass>org.checkstyle.autofix.recipe.HexLiteralCase$HexLiteralCaseVisitor</mutatedClass>
@@ -613,15 +604,6 @@
613604
<lineContent>&amp;&amp; absolutePath.endsWith(sourcePath);</lineContent>
614605
</mutation>
615606

616-
<mutation unstable="false">
617-
<sourceFile>HexLiteralCase.java</sourceFile>
618-
<mutatedClass>org.checkstyle.autofix.recipe.HexLiteralCase$HexLiteralCaseVisitor</mutatedClass>
619-
<mutatedMethod>lambda$isAtViolationLocation$0</mutatedMethod>
620-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_IF</mutator>
621-
<description>removed conditional - replaced equality check with true</description>
622-
<lineContent>&amp;&amp; violation.getColumn() == column</lineContent>
623-
</mutation>
624-
625607
<mutation unstable="false">
626608
<sourceFile>HexLiteralCase.java</sourceFile>
627609
<mutatedClass>org.checkstyle.autofix.recipe.HexLiteralCase$HexLiteralCaseVisitor</mutatedClass>
@@ -631,78 +613,6 @@
631613
<lineContent>final Path absolutePath = violation.getFilePath().toAbsolutePath();</lineContent>
632614
</mutation>
633615

634-
<mutation unstable="false">
635-
<sourceFile>HexLiteralCase.java</sourceFile>
636-
<mutatedClass>org.checkstyle.autofix.recipe.HexLiteralCase$HexLiteralCaseVisitor</mutatedClass>
637-
<mutatedMethod>lambda$isAtViolationLocation$0</mutatedMethod>
638-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_IF</mutator>
639-
<description>removed conditional - replaced equality check with true</description>
640-
<lineContent>return violation.getLine() == line</lineContent>
641-
</mutation>
642-
643-
<mutation unstable="false">
644-
<sourceFile>HexLiteralCase.java</sourceFile>
645-
<mutatedClass>org.checkstyle.autofix.recipe.HexLiteralCase$HexLiteralCaseVisitor</mutatedClass>
646-
<mutatedMethod>lambda$isAtViolationLocation$0</mutatedMethod>
647-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.returns.BooleanTrueReturnValsMutator</mutator>
648-
<description>replaced boolean return with true for org/checkstyle/autofix/recipe/HexLiteralCase$HexLiteralCaseVisitor::lambda$isAtViolationLocation$0</description>
649-
<lineContent>return violation.getLine() == line</lineContent>
650-
</mutation>
651-
652-
<mutation unstable="false">
653-
<sourceFile>HexLiteralCase.java</sourceFile>
654-
<mutatedClass>org.checkstyle.autofix.recipe.HexLiteralCase$HexLiteralCaseVisitor</mutatedClass>
655-
<mutatedMethod>shouldProcessLiteral</mutatedMethod>
656-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE</mutator>
657-
<description>removed conditional - replaced equality check with false</description>
658-
<lineContent>&amp;&amp; (literal.getType() == JavaType.Primitive.Long</lineContent>
659-
</mutation>
660-
661-
<mutation unstable="false">
662-
<sourceFile>HexLiteralCase.java</sourceFile>
663-
<mutatedClass>org.checkstyle.autofix.recipe.HexLiteralCase$HexLiteralCaseVisitor</mutatedClass>
664-
<mutatedMethod>shouldProcessLiteral</mutatedMethod>
665-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE</mutator>
666-
<description>removed conditional - replaced equality check with false</description>
667-
<lineContent>&amp;&amp; (valueSource.startsWith(HEX_PREFIX)</lineContent>
668-
</mutation>
669-
670-
<mutation unstable="false">
671-
<sourceFile>HexLiteralCase.java</sourceFile>
672-
<mutatedClass>org.checkstyle.autofix.recipe.HexLiteralCase$HexLiteralCaseVisitor</mutatedClass>
673-
<mutatedMethod>shouldProcessLiteral</mutatedMethod>
674-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_IF</mutator>
675-
<description>removed conditional - replaced equality check with true</description>
676-
<lineContent>&amp;&amp; isAtViolationLocation(literal);</lineContent>
677-
</mutation>
678-
679-
<mutation unstable="false">
680-
<sourceFile>HexLiteralCase.java</sourceFile>
681-
<mutatedClass>org.checkstyle.autofix.recipe.HexLiteralCase$HexLiteralCaseVisitor</mutatedClass>
682-
<mutatedMethod>shouldProcessLiteral</mutatedMethod>
683-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_IF</mutator>
684-
<description>removed conditional - replaced equality check with true</description>
685-
<lineContent>return valueSource != null</lineContent>
686-
</mutation>
687-
688-
<mutation unstable="false">
689-
<sourceFile>HexLiteralCase.java</sourceFile>
690-
<mutatedClass>org.checkstyle.autofix.recipe.HexLiteralCase$HexLiteralCaseVisitor</mutatedClass>
691-
<mutatedMethod>shouldProcessLiteral</mutatedMethod>
692-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_IF</mutator>
693-
<description>removed conditional - replaced equality check with true</description>
694-
<lineContent>|| literal.getType() == JavaType.Primitive.Int)</lineContent>
695-
</mutation>
696-
697-
<mutation unstable="false">
698-
<sourceFile>HexLiteralCase.java</sourceFile>
699-
<mutatedClass>org.checkstyle.autofix.recipe.HexLiteralCase$HexLiteralCaseVisitor</mutatedClass>
700-
<mutatedMethod>shouldProcessLiteral</mutatedMethod>
701-
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_IF</mutator>
702-
<description>removed conditional - replaced equality check with true</description>
703-
<lineContent>|| valueSource.startsWith(HEX_PREFIX.toUpperCase(Locale.ROOT)))</lineContent>
704-
</mutation>
705-
706616
<mutation unstable="false">
707617
<sourceFile>HexLiteralCase.java</sourceFile>
708618
<mutatedClass>org.checkstyle.autofix.recipe.HexLiteralCase$HexLiteralCaseVisitor</mutatedClass>

src/main/java/org/checkstyle/autofix/recipe/HexLiteralCase.java

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,47 +76,75 @@ public J.Literal visitLiteral(J.Literal literal, ExecutionContext executionConte
7676
J.Literal result = super.visitLiteral(literal, executionContext);
7777
final String valueSource = result.getValueSource();
7878

79-
if (shouldProcessLiteral(result, valueSource)) {
80-
final String newValueSource = convertLowercaseHexToUppercase(valueSource);
79+
if (shouldProcessLongAndIntLiteral(result)) {
80+
final String newValueSource =
81+
convertLowercaseHexToUppercaseForLongAndInt(valueSource);
82+
result = result.withValueSource(newValueSource);
83+
}
84+
85+
if (shouldProcessFloatAndDoubleLiteral(result)) {
86+
final String newValueSource =
87+
convertLowercaseHexToUppercaseForFloatAndDouble(valueSource);
8188
result = result.withValueSource(newValueSource);
8289
}
8390

8491
return result;
8592
}
8693

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
94+
private boolean shouldProcessLongAndIntLiteral(J.Literal literal) {
95+
return (literal.getType() == JavaType.Primitive.Long
10096
|| literal.getType() == JavaType.Primitive.Int)
10197
&& isAtViolationLocation(literal);
10298
}
10399

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);
100+
private boolean shouldProcessFloatAndDoubleLiteral(J.Literal literal) {
101+
return (literal.getType() == JavaType.Primitive.Float
102+
|| literal.getType() == JavaType.Primitive.Double)
103+
&& isAtViolationLocation(literal);
104+
}
105+
106+
private String convertLowercaseHexToUppercaseForLongAndInt(String valueSource) {
107+
final int lIndex = valueSource.indexOf("l");
108+
final StringBuilder result = new StringBuilder();
109+
110+
result.append(valueSource.substring(0, HEX_PREFIX.length()));
111+
112+
if (lIndex != -1) {
113+
result.append(valueSource.substring(HEX_PREFIX.length(), lIndex)
114+
.toUpperCase(Locale.ROOT));
115+
result.append(valueSource.substring(lIndex));
116+
}
117+
else {
118+
result.append(valueSource.substring(HEX_PREFIX.length())
119+
.toUpperCase(Locale.ROOT));
120+
}
121+
122+
String finalResult = result.toString();
114123

115124
// Avoid extra cycle by skipping identical replacements
116-
if (result.equals(valueSource)) {
117-
result = valueSource;
125+
if (finalResult.equals(valueSource)) {
126+
finalResult = valueSource;
118127
}
119-
return result;
128+
return finalResult;
129+
}
130+
131+
private String convertLowercaseHexToUppercaseForFloatAndDouble(String valueSource) {
132+
final String lowercaseValueSource = valueSource.toLowerCase(Locale.ROOT);
133+
final int pIndex = lowercaseValueSource.indexOf("p");
134+
final StringBuilder result = new StringBuilder();
135+
136+
result.append(valueSource.substring(0, HEX_PREFIX.length()));
137+
result.append(valueSource.substring(HEX_PREFIX.length(), pIndex)
138+
.toUpperCase(Locale.ROOT));
139+
result.append(valueSource.substring(pIndex));
140+
141+
String finalResult = result.toString();
142+
143+
// Avoid extra cycle by skipping identical replacements
144+
if (finalResult.equals(valueSource)) {
145+
finalResult = valueSource;
146+
}
147+
return finalResult;
120148
}
121149

122150
private boolean isAtViolationLocation(J.Literal literal) {

src/test/java/org/checkstyle/autofix/recipe/HexLiteralCaseTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ protected String getSubpackage() {
3030
void hexLiteral(ReportParser parser) throws Exception {
3131
verify(parser, "HexLiteralCase");
3232
}
33+
34+
@RecipeTest
35+
void hexLiteralTwo(ReportParser parser) throws Exception {
36+
verify(parser, "HexLiteralCaseTwo");
37+
}
38+
39+
@RecipeTest
40+
void hexLiteralThree(ReportParser parser) throws Exception {
41+
verify(parser, "HexLiteralCaseThree");
42+
}
3343
}

src/test/resources/org/checkstyle/autofix/recipe/hexliterialcase/hexliteralcase/DiffHexLiteralCase.diff

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--- src/test/resources/org/checkstyle/autofix/recipe/hexliterialcase/hexliteralcase/InputHexLiteralCase.java
22
+++ src/test/resources/org/checkstyle/autofix/recipe/hexliterialcase/hexliteralcase/OutputHexLiteralCase.java
3-
@@ -9,24 +9,21 @@
3+
@@ -9,29 +9,26 @@
44

55
package org.checkstyle.autofix.recipe.hexliterialcase.hexliteralcase;
66

@@ -36,3 +36,9 @@
3636
}
3737
private static void functionCall2(int first, int second) {
3838
System.out.println("First: " + first);
39+
System.out.println("Second: " + second);
40+
}
41+
42+
- long l3 = 0xABcl; // violation 'Should use uppercase hexadecimal letters.'
43+
+ long l3 = 0xABCl;
44+
}

src/test/resources/org/checkstyle/autofix/recipe/hexliterialcase/hexliteralcase/InputHexLiteralCase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ private static void functionCall2(int first, int second) {
3333
System.out.println("Second: " + second);
3434
}
3535

36+
long l3 = 0xABcl; // violation 'Should use uppercase hexadecimal letters.'
3637
}

src/test/resources/org/checkstyle/autofix/recipe/hexliterialcase/hexliteralcase/OutputHexLiteralCase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ private static void functionCall2(int first, int second) {
3030
System.out.println("Second: " + second);
3131
}
3232

33+
long l3 = 0xABCl;
3334
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--- src/test/resources/org/checkstyle/autofix/recipe/hexliterialcase/hexliteralcasethree/InputHexLiteralCaseThree.java
2+
+++ src/test/resources/org/checkstyle/autofix/recipe/hexliterialcase/hexliteralcasethree/OutputHexLiteralCaseThree.java
3+
@@ -14,9 +14,9 @@
4+
5+
package org.checkstyle.autofix.recipe.hexliterialcase.hexliteralcasethree;
6+
7+
-public class InputHexLiteralCaseThree {
8+
- int x = 0xabc + 0xdef; // violation 'Should use uppercase hexadecimal letters.'
9+
- int y = 0xab; // violation 'Should use uppercase hexadecimal letters.'
10+
+public class OutputHexLiteralCaseThree {
11+
+ int x = 0xabc + 0xDEF;
12+
+ int y = 0xAB;
13+
14+
- float f = 0xab.1p1f + 0xb.1p1f; // violation 'Should use uppercase hexadecimal letters.'
15+
+ float f = 0xab.1p1f + 0xB.1p1f;
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*xml
2+
<module name="Checker">
3+
<module name="com.puppycrawl.tools.checkstyle.filters.SuppressionXpathSingleFilter">
4+
<property name="checks" value="HexLiteralCase"/>
5+
<property name="query" value="//NUM_INT[@text='0xabc']"/>
6+
<property name="query" value="//NUM_FLOAT[@text='0xab.1p1f']"/>
7+
</module>
8+
<module name="TreeWalker">
9+
<module name="com.puppycrawl.tools.checkstyle.checks.HexLiteralCaseCheck"/>
10+
</module>
11+
</module>
12+
13+
*/
14+
15+
package org.checkstyle.autofix.recipe.hexliterialcase.hexliteralcasethree;
16+
17+
public class InputHexLiteralCaseThree {
18+
int x = 0xabc + 0xdef; // violation 'Should use uppercase hexadecimal letters.'
19+
int y = 0xab; // violation 'Should use uppercase hexadecimal letters.'
20+
21+
float f = 0xab.1p1f + 0xb.1p1f; // violation 'Should use uppercase hexadecimal letters.'
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*xml
2+
<module name="Checker">
3+
<module name="com.puppycrawl.tools.checkstyle.filters.SuppressionXpathSingleFilter">
4+
<property name="checks" value="HexLiteralCase"/>
5+
<property name="query" value="//NUM_INT[@text='0xabc']"/>
6+
<property name="query" value="//NUM_FLOAT[@text='0xab.1p1f']"/>
7+
</module>
8+
<module name="TreeWalker">
9+
<module name="com.puppycrawl.tools.checkstyle.checks.HexLiteralCaseCheck"/>
10+
</module>
11+
</module>
12+
13+
*/
14+
15+
package org.checkstyle.autofix.recipe.hexliterialcase.hexliteralcasethree;
16+
17+
public class OutputHexLiteralCaseThree {
18+
int x = 0xabc + 0xDEF;
19+
int y = 0xAB;
20+
21+
float f = 0xab.1p1f + 0xB.1p1f;
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--- src/test/resources/org/checkstyle/autofix/recipe/hexliterialcase/hexliteralcasetwo/InputHexLiteralCaseTwo.java
2+
+++ src/test/resources/org/checkstyle/autofix/recipe/hexliterialcase/hexliteralcasetwo/OutputHexLiteralCaseTwo.java
3+
@@ -9,12 +9,12 @@
4+
5+
package org.checkstyle.autofix.recipe.hexliterialcase.hexliteralcasetwo;
6+
7+
-public class InputHexLiteralCaseTwo {
8+
- float f = 0x1.fp1f; // violation 'Should use uppercase hexadecimal letters.'
9+
- double d = 0x1abcde.fp1d; // violation 'Should use uppercase hexadecimal letters.'
10+
+public class OutputHexLiteralCaseTwo {
11+
+ float f = 0x1.Fp1f;
12+
+ double d = 0x1ABCDE.Fp1d;
13+
14+
- float f1 = 0x1a_e_f.1p1f; // violation 'Should use uppercase hexadecimal letters.'
15+
- double d1 = 0x1a_b_c_d_e.1p1d; // violation 'Should use uppercase hexadecimal letters.'
16+
+ float f1 = 0x1A_E_F.1p1f;
17+
+ double d1 = 0x1A_B_C_D_E.1p1d;
18+
19+
- float f2 = 0xab.1P1f; // violation 'Should use uppercase hexadecimal letters.'
20+
+ float f2 = 0xAB.1P1f;
21+
}

0 commit comments

Comments
 (0)