From a126d0ec04ec9193345438253540355157d5734c Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Wed, 27 Nov 2024 13:46:25 +0100 Subject: [PATCH] WIP --- .../staticanalysis/MethodNameCasing.java | 41 +----------------- .../nameconvention/CsharpNameConvention.java | 42 +++++++++++++++++++ .../nameconvention/JavaNameConvention.java | 42 +++++++++++++++++++ .../nameconvention/NameConvention.java | 6 +++ .../nameconvention/NameConventionFactory.java | 26 ++++++++++++ .../nameconvention/package-info.java | 19 +++++++++ 6 files changed, 137 insertions(+), 39 deletions(-) create mode 100644 src/main/java/org/openrewrite/staticanalysis/nameconvention/CsharpNameConvention.java create mode 100644 src/main/java/org/openrewrite/staticanalysis/nameconvention/JavaNameConvention.java create mode 100644 src/main/java/org/openrewrite/staticanalysis/nameconvention/NameConvention.java create mode 100644 src/main/java/org/openrewrite/staticanalysis/nameconvention/NameConventionFactory.java create mode 100644 src/main/java/org/openrewrite/staticanalysis/nameconvention/package-info.java diff --git a/src/main/java/org/openrewrite/staticanalysis/MethodNameCasing.java b/src/main/java/org/openrewrite/staticanalysis/MethodNameCasing.java index 3efa9acd5..8e2cc8278 100644 --- a/src/main/java/org/openrewrite/staticanalysis/MethodNameCasing.java +++ b/src/main/java/org/openrewrite/staticanalysis/MethodNameCasing.java @@ -30,6 +30,7 @@ import org.openrewrite.java.tree.JavaSourceFile; import org.openrewrite.java.tree.JavaType; import org.openrewrite.java.tree.TypeUtils; +import org.openrewrite.staticanalysis.nameconvention.NameConventionFactory; import java.time.Duration; import java.util.*; @@ -40,8 +41,6 @@ public class MethodNameCasing extends ScanningRecipe> { 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) @@ -89,13 +88,6 @@ public TreeVisitor getScanner(List change public J preVisit(J tree, ExecutionContext ctx) { if (tree instanceof JavaSourceFile) { scope = tree.getId(); - JavaSourceFile cu = (JavaSourceFile) tree; - Optional sourceSet = cu.getMarkers().findFirst(JavaSourceSet.class); - if (!sourceSet.isPresent()) { - stopAfterPreVisit(); - } else if (!Boolean.TRUE.equals(includeTestSources) && !"main".equals(sourceSet.get().getName())) { - stopAfterPreVisit(); - } } return super.preVisit(tree, ctx); } @@ -113,37 +105,8 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex !method.isConstructor() && !simpleName.startsWith("_") && !STANDARD_METHOD_NAME.matcher(simpleName).matches()) { - StringBuilder standardized = new StringBuilder(); String normalized = VariableNameUtils.normalizeName(simpleName); - - if (SNAKE_CASE.matcher(normalized).matches()) { - standardized.append(NameCaseConvention.format(NameCaseConvention.LOWER_CAMEL, normalized)); - } else { - int nameLength = normalized.length(); - for (int i = 0; i < nameLength; i++) { - char c = normalized.charAt(i); - - if (i == 0) { - // the java specification requires identifiers to start with [a-zA-Z$_] - if (c != '$' && c != '_') { - standardized.append(Character.toLowerCase(c)); - } - } else { - if (!Character.isLetterOrDigit(c)) { - while (i < nameLength && (!Character.isLetterOrDigit(c) || c > 'z')) { - c = normalized.charAt(i++); - } - if (i < nameLength) { - standardized.append(Character.toUpperCase(c)); - } - } else { - standardized.append(c); - } - } - } - } - - String toName = standardized.toString(); + String toName = NameConventionFactory.getNameConvention(getCursor().firstEnclosing(SourceFile.class)).applyNameConvention(normalized); if (!StringUtils.isBlank(toName) && !StringUtils.isNumeric(toName) && !methodExists(method.getMethodType(), toName)) { changes.add(new MethodNameChange( diff --git a/src/main/java/org/openrewrite/staticanalysis/nameconvention/CsharpNameConvention.java b/src/main/java/org/openrewrite/staticanalysis/nameconvention/CsharpNameConvention.java new file mode 100644 index 000000000..f2189474a --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/nameconvention/CsharpNameConvention.java @@ -0,0 +1,42 @@ +package org.openrewrite.staticanalysis.nameconvention; + +import org.openrewrite.internal.NameCaseConvention; + +import java.util.regex.Pattern; + +class CsharpNameConvention implements NameConvention { + + private static final Pattern SNAKE_CASE = Pattern.compile("^[a-zA-Z0-9]+_\\w+$"); + + @Override + public String applyNameConvention(String normalizedName) { + StringBuilder result = new StringBuilder(); + if (SNAKE_CASE.matcher(normalizedName).matches()) { + result.append(NameCaseConvention.format(NameCaseConvention.UPPER_CAMEL, normalizedName)); + } else { + int nameLength = normalizedName.length(); + for (int i = 0; i < nameLength; i++) { + char c = normalizedName.charAt(i); + + if (i == 0) { + // the java specification requires identifiers to start with [a-zA-Z$_] + if (c != '$' && c != '_') { + result.append(Character.toUpperCase(c)); + } + } else { + if (!Character.isLetterOrDigit(c)) { + while (i < nameLength && (!Character.isLetterOrDigit(c) || c > 'z')) { + c = normalizedName.charAt(i++); + } + if (i < nameLength) { + result.append(Character.toUpperCase(c)); + } + } else { + result.append(c); + } + } + } + } + return result.toString(); + } +} diff --git a/src/main/java/org/openrewrite/staticanalysis/nameconvention/JavaNameConvention.java b/src/main/java/org/openrewrite/staticanalysis/nameconvention/JavaNameConvention.java new file mode 100644 index 000000000..9184d0402 --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/nameconvention/JavaNameConvention.java @@ -0,0 +1,42 @@ +package org.openrewrite.staticanalysis.nameconvention; + +import org.openrewrite.internal.NameCaseConvention; + +import java.util.regex.Pattern; + +class JavaNameConvention implements NameConvention { + + private static final Pattern SNAKE_CASE = Pattern.compile("^[a-zA-Z0-9]+_\\w+$"); + + @Override + public String applyNameConvention(String normalizedName) { + StringBuilder result = new StringBuilder(); + if (SNAKE_CASE.matcher(normalizedName).matches()) { + result.append(NameCaseConvention.format(NameCaseConvention.LOWER_CAMEL, normalizedName)); + } else { + int nameLength = normalizedName.length(); + for (int i = 0; i < nameLength; i++) { + char c = normalizedName.charAt(i); + + if (i == 0) { + // the java specification requires identifiers to start with [a-zA-Z$_] + if (c != '$' && c != '_') { + result.append(Character.toLowerCase(c)); + } + } else { + if (!Character.isLetterOrDigit(c)) { + while (i < nameLength && (!Character.isLetterOrDigit(c) || c > 'z')) { + c = normalizedName.charAt(i++); + } + if (i < nameLength) { + result.append(Character.toUpperCase(c)); + } + } else { + result.append(c); + } + } + } + } + return result.toString(); + } +} diff --git a/src/main/java/org/openrewrite/staticanalysis/nameconvention/NameConvention.java b/src/main/java/org/openrewrite/staticanalysis/nameconvention/NameConvention.java new file mode 100644 index 000000000..6a7f7d4bc --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/nameconvention/NameConvention.java @@ -0,0 +1,6 @@ +package org.openrewrite.staticanalysis.nameconvention; + +public interface NameConvention { + + String applyNameConvention(String normalizedName); +} diff --git a/src/main/java/org/openrewrite/staticanalysis/nameconvention/NameConventionFactory.java b/src/main/java/org/openrewrite/staticanalysis/nameconvention/NameConventionFactory.java new file mode 100644 index 000000000..0c22dd46d --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/nameconvention/NameConventionFactory.java @@ -0,0 +1,26 @@ +package org.openrewrite.staticanalysis.nameconvention; + +import fj.data.Java; +import org.openrewrite.SourceFile; +import org.openrewrite.csharp.tree.Cs; +import org.openrewrite.java.tree.J; + +import java.util.HashMap; +import java.util.Map; + +public class NameConventionFactory { + + static Map conventionMap = new HashMap<>(); + + public static NameConvention getNameConvention(SourceFile sourceFile) { + if (sourceFile instanceof Cs){ + conventionMap.computeIfAbsent("csharp", k -> new CsharpNameConvention()); + return conventionMap.get("csharp"); + } else if (sourceFile instanceof J) { + conventionMap.computeIfAbsent("java", k -> new JavaNameConvention()); + return conventionMap.get("java"); + } + return null; + } + +} diff --git a/src/main/java/org/openrewrite/staticanalysis/nameconvention/package-info.java b/src/main/java/org/openrewrite/staticanalysis/nameconvention/package-info.java new file mode 100644 index 000000000..cfa7f9ce9 --- /dev/null +++ b/src/main/java/org/openrewrite/staticanalysis/nameconvention/package-info.java @@ -0,0 +1,19 @@ +/* + * Copyright 2020 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +@NullMarked +package org.openrewrite.staticanalysis.nameconvention; + +import org.jspecify.annotations.NullMarked;