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

Adjust MethodNameCasing to work with C# #397

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
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()) {
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved
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;
timtebeek marked this conversation as resolved.
Show resolved Hide resolved

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;
timtebeek marked this conversation as resolved.
Show resolved Hide resolved

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;
timtebeek marked this conversation as resolved.
Show resolved Hide resolved

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;
timtebeek marked this conversation as resolved.
Show resolved Hide resolved

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) {
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
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;
Loading