Skip to content

Commit

Permalink
Improve scanning performance of MethodNameCasing
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Jan 9, 2024
1 parent 3aaa11c commit 0a4e1ac
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
@EqualsAndHashCode(callSuper = true)
public class MethodNameCasing extends ScanningRecipe<List<ChangeMethodName>> {

private static final Pattern STANDARD_METHOD_NAME = Pattern.compile("^[a-z][a-zA-Z0-9]*$");
private static final Pattern SNAKE_CASE = Pattern.compile("^[a-zA-Z0-9]+_\\w+$");

@Option(displayName = "Apply recipe to test source set",
description = "Changes only apply to main by default. `includeTestSources` will apply the recipe to `test` source files.",
required = false)
Expand Down Expand Up @@ -80,8 +83,6 @@ public List<ChangeMethodName> getInitialValue(ExecutionContext ctx) {

@Override
public TreeVisitor<?, ExecutionContext> getScanner(List<ChangeMethodName> changes) {
Pattern standardMethodName = Pattern.compile("^[a-z][a-zA-Z0-9]*$");
Pattern snakeCase = Pattern.compile("^[a-zA-Z0-9]+_\\w+$");
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J preVisit(J tree, ExecutionContext ctx) {
Expand All @@ -108,13 +109,13 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
enclosingClass.getType() != null &&
!method.isConstructor() &&
!TypeUtils.isOverride(method.getMethodType()) &&
!standardMethodName.matcher(method.getSimpleName()).matches() &&
!STANDARD_METHOD_NAME.matcher(method.getSimpleName()).matches() &&
!method.getSimpleName().startsWith("_")) {
StringBuilder standardized = new StringBuilder();
String normalized = VariableNameUtils.normalizeName(method.getSimpleName());
char[] name = normalized.toCharArray();

if (snakeCase.matcher(normalized).matches()) {
if (SNAKE_CASE.matcher(normalized).matches()) {
standardized.append(NameCaseConvention.format(NameCaseConvention.LOWER_CAMEL, normalized));
} else {
for (int i = 0; i < name.length; i++) {
Expand Down Expand Up @@ -167,7 +168,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor(List<ChangeMethodName> change
@Override
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree);
JavaSourceFile cu = (JavaSourceFile) tree;
for (ChangeMethodName changeMethodName : changes) {
cu = (JavaSourceFile) changeMethodName.getVisitor().visitNonNull(cu, ctx);
}
Expand Down

0 comments on commit 0a4e1ac

Please sign in to comment.