Skip to content

Commit

Permalink
openrewrite#141 minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin0x90 committed Oct 16, 2023
1 parent 9003c9f commit 1c005f4
Showing 1 changed file with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -114,7 +114,7 @@ private static class LombokValueToRecordVisitor extends JavaIsoVisitor<Execution

private static final String TO_STRING_MEMBER_DELIMITER = "\", \" +\n";

private static final Map<String, Set<String>> RECORD_TYPE_TO_MEMBERS = new HashMap<>();
private static final Map<String, Set<String>> RECORD_TYPE_TO_MEMBERS = new ConcurrentHashMap<>();

private static final String STANDARD_GETTER_PREFIX = "get";

Expand Down Expand Up @@ -148,8 +148,10 @@ private static boolean isMethodInvocationOnRecordTypeClassMember(final J.MethodI
return false;
}

final JavaType.Class classType = requireNonNull((JavaType.Class) expression.getType(),
"Class type must not be null");
final JavaType.Class classType = (JavaType.Class) expression.getType();
if (classType == null) {
return false;
}

final String methodName = methodInvocation.getName().getSimpleName();

Expand Down Expand Up @@ -182,13 +184,7 @@ private static String getterMethodNameToFluentMethodName(final String methodName
public J.ClassDeclaration visitClassDeclaration(final J.ClassDeclaration cd, final ExecutionContext ctx) {
J.ClassDeclaration classDeclaration = super.visitClassDeclaration(cd, ctx);


if (isRecord(classDeclaration)
|| !hasOnlyLombokValueAnnotation(classDeclaration)
|| hasGenericTypeParameter(classDeclaration)
|| hasExplicitMethods(classDeclaration)
|| hasExplicitConstructor(classDeclaration)
) {
if (!isRelevantClass(classDeclaration)) {
return classDeclaration;
}

Expand Down Expand Up @@ -218,6 +214,15 @@ public J.ClassDeclaration visitClassDeclaration(final J.ClassDeclaration cd, fin
return maybeAutoFormat(cd, classDeclaration, ctx);
}

private static boolean isRelevantClass(final J.ClassDeclaration classDeclaration) {
return !isRecord(classDeclaration)
&& hasOnlyLombokValueAnnotation(classDeclaration)
&& !hasGenericTypeParameter(classDeclaration)
&& !hasExplicitMethods(classDeclaration)
&& !hasExplicitConstructor(classDeclaration)
&& classDeclaration.getType() != null;
}

private J.ClassDeclaration addExactToStringMethod(final J.ClassDeclaration classDeclaration,
final List<J.VariableDeclarations> memberVariables) {
return classDeclaration.withBody(TO_STRING_TEMPLATE
Expand Down Expand Up @@ -268,9 +273,9 @@ private static boolean hasGenericTypeParameter(final J.ClassDeclaration classDec
return typeParameters != null && !typeParameters.isEmpty();
}

private static JavaType.Class buildRecordType(final J.ClassDeclaration cd) {
requireNonNull(cd.getType(), "Class type must not be null");
final String className = requireNonNull(cd.getType().getFullyQualifiedName(),
private static JavaType.Class buildRecordType(final J.ClassDeclaration classDeclaration) {
requireNonNull(classDeclaration.getType(), "Class type must not be null");
final String className = requireNonNull(classDeclaration.getType().getFullyQualifiedName(),
"Fully qualified name of class must not be null");

return JavaType.ShallowClass.build(className)
Expand Down

0 comments on commit 1c005f4

Please sign in to comment.