Skip to content

Commit

Permalink
ReplaceDuplicateStringLiterals: More tweaks
Browse files Browse the repository at this point in the history
Should slightly improve performance and readability.
  • Loading branch information
knutwannheden committed Jul 12, 2024
1 parent 7686b53 commit a8270e4
Showing 1 changed file with 7 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,8 @@ private String transformToVariableName(String valueOfLiteral) {
});
}

private static boolean isPrivateStaticFinalVariable(J.VariableDeclarations declaration) {
return declaration.hasModifier(J.Modifier.Type.Private) &&
declaration.hasModifier(J.Modifier.Type.Static) &&
declaration.hasModifier(J.Modifier.Type.Final);
private static boolean isPrivateStaticFinalVariable(J.VariableDeclarations.NamedVariable variable) {
return variable.getVariableType() != null && variable.getVariableType().hasFlags(Flag.Private, Flag.Static, Flag.Final);
}

@Value
Expand All @@ -212,8 +210,7 @@ public J.Annotation visitAnnotation(J.Annotation annotation, Integer integer) {
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, Integer integer) {
J.VariableDeclarations.NamedVariable v = super.visitVariable(variable, integer);
Cursor parentScope = getCursor().dropParentUntil(is -> is instanceof J.ClassDeclaration || is instanceof J.MethodDeclaration);
J.VariableDeclarations declaration = getCursor().firstEnclosingOrThrow(J.VariableDeclarations.class);
boolean privateStaticFinalVariable = isPrivateStaticFinalVariable(declaration);
boolean privateStaticFinalVariable = isPrivateStaticFinalVariable(variable);
// `private static final String`(s) are handled separately by `FindExistingPrivateStaticFinalFields`.
if (parentScope.getValue() instanceof J.MethodDeclaration ||
parentScope.getValue() instanceof J.ClassDeclaration &&
Expand All @@ -238,7 +235,7 @@ public J.Literal visitLiteral(J.Literal literal, Integer integer) {

Cursor parent = getCursor().dropParentUntil(is -> is instanceof J.ClassDeclaration ||
is instanceof J.Annotation ||
is instanceof J.VariableDeclarations ||
is instanceof J.VariableDeclarations.NamedVariable ||
is instanceof J.NewClass ||
is instanceof J.MethodInvocation);
// EnumValue can accept constructor arguments, including string literals
Expand All @@ -247,11 +244,9 @@ public J.Literal visitLiteral(J.Literal literal, Integer integer) {
return literal;
}

if ((parent.getValue() instanceof J.VariableDeclarations &&
((J.VariableDeclarations) parent.getValue()).hasModifier(J.Modifier.Type.Final) &&
!(((J.VariableDeclarations) parent.getValue()).hasModifier(J.Modifier.Type.Private) && ((J.VariableDeclarations) parent.getValue()).hasModifier(J.Modifier.Type.Static))) ||
parent.getValue() instanceof J.NewClass ||
parent.getValue() instanceof J.MethodInvocation) {
if ((parent.getValue() instanceof J.VariableDeclarations.NamedVariable && !isPrivateStaticFinalVariable(parent.getValue())) ||
parent.getValue() instanceof J.NewClass ||
parent.getValue() instanceof J.MethodInvocation) {

result.duplicateLiterals.computeIfAbsent(((String) literal.getValue()), k -> new ArrayList<>(1)).add(literal);
}
Expand Down

0 comments on commit a8270e4

Please sign in to comment.