Skip to content
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

Cap the lengths of generate names in the ReplaceDuplicateStringLiterals recipe #374

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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