Skip to content

Commit

Permalink
Cap the lengths of generate names in the ReplaceDuplicateStringLitera…
Browse files Browse the repository at this point in the history
…ls recipe (#374)

* Cap the lengths of generate names

We could generate some unreasonably long variable names. By default, we will cap variable names at 100 characters but people can change this was desired.

We will crop the generated variable name to the nearest "_" if there is one available

* Apply suggestions from code review

* Use ternary for `.substring` instead of similar if/else

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
lkerford and timtebeek authored Oct 22, 2024
1 parent 12689e7 commit 5b19b47
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public class ReplaceDuplicateStringLiterals extends Recipe {
@Nullable
Boolean includeTestSources;

@Option(displayName = "Maximum length of the generate variable names",
description = "By default this is set to 100 characters",
required = false)
@Nullable
Integer maxVariableLength = 100;

@Override
public String getDisplayName() {
return "Replace duplicate `String` literals";
Expand Down Expand Up @@ -181,7 +187,12 @@ private String transformToVariableName(String valueOfLiteral) {
prevIsLower = Character.isLowerCase(c);
}
}
return VariableNameUtils.normalizeName(newName.toString());
String newNameString = newName.toString();
while (newNameString.length() > maxVariableLength){
int indexOf = newNameString.lastIndexOf("_");
newNameString = newNameString.substring(0, indexOf > -1 ? indexOf : maxVariableLength);
}
return VariableNameUtils.normalizeName(newNameString);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,32 @@ class A {
);
}

@Test
void generatedNameIsVeryLong() {
rewriteRun(
//language=java
java(
"""
package org.foo;
class A {
final String val1 = "ThisIsAnUnreasonablyLongVariableNameItGoesOnAndOnForAVeryLongTimeItMightNeverEndWhoIsToKnowHowLongItWillKeepGoingAndGoing";
final String val2 = "ThisIsAnUnreasonablyLongVariableNameItGoesOnAndOnForAVeryLongTimeItMightNeverEndWhoIsToKnowHowLongItWillKeepGoingAndGoing";
final String val3 = "ThisIsAnUnreasonablyLongVariableNameItGoesOnAndOnForAVeryLongTimeItMightNeverEndWhoIsToKnowHowLongItWillKeepGoingAndGoing";
}
""",
"""
package org.foo;
class A {
private static final String THIS_IS_AN_UNREASONABLY_LONG_VARIABLE_NAME_IT_GOES_ON_AND_ON_FOR_AVERY_LONG_TIME_IT_MIGHT_NEVER_END = "ThisIsAnUnreasonablyLongVariableNameItGoesOnAndOnForAVeryLongTimeItMightNeverEndWhoIsToKnowHowLongItWillKeepGoingAndGoing";
final String val1 = THIS_IS_AN_UNREASONABLY_LONG_VARIABLE_NAME_IT_GOES_ON_AND_ON_FOR_AVERY_LONG_TIME_IT_MIGHT_NEVER_END;
final String val2 = THIS_IS_AN_UNREASONABLY_LONG_VARIABLE_NAME_IT_GOES_ON_AND_ON_FOR_AVERY_LONG_TIME_IT_MIGHT_NEVER_END;
final String val3 = THIS_IS_AN_UNREASONABLY_LONG_VARIABLE_NAME_IT_GOES_ON_AND_ON_FOR_AVERY_LONG_TIME_IT_MIGHT_NEVER_END;
}
"""
)
);
}

@Test
void replaceRedundantLiteralInMethodInvocation() {
rewriteRun(
Expand Down

0 comments on commit 5b19b47

Please sign in to comment.