Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurens-W committed Nov 27, 2024
1 parent 0e0b4e9 commit a126d0e
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 39 deletions.
41 changes: 2 additions & 39 deletions src/main/java/org/openrewrite/staticanalysis/MethodNameCasing.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -40,8 +41,6 @@
public class MethodNameCasing extends ScanningRecipe<List<MethodNameCasing.MethodNameChange>> {

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 @@ -89,13 +88,6 @@ public TreeVisitor<?, ExecutionContext> getScanner(List<MethodNameChange> change
public J preVisit(J tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
scope = tree.getId();
JavaSourceFile cu = (JavaSourceFile) tree;
Optional<JavaSourceSet> 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);
}
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.openrewrite.staticanalysis.nameconvention;

public interface NameConvention {

String applyNameConvention(String normalizedName);
}
Original file line number Diff line number Diff line change
@@ -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<String, NameConvention> 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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2020 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;

0 comments on commit a126d0e

Please sign in to comment.