Skip to content

Commit

Permalink
Blunt adaptation for C# in FinalizePrivateFields
Browse files Browse the repository at this point in the history
Tests are not that easy yet.
  • Loading branch information
knutwannheden committed Aug 24, 2024
1 parent cf3dced commit 8e693d2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
implementation("org.openrewrite:rewrite-java")
implementation("org.openrewrite:rewrite-groovy:${rewriteVersion}")
implementation("org.openrewrite:rewrite-kotlin:${rewriteVersion}")
implementation("org.openrewrite:rewrite-csharp:${rewriteVersion}")
implementation("org.openrewrite.meta:rewrite-analysis:${rewriteVersion}")
implementation("org.apache.commons:commons-text:latest.release")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.csharp.tree.Cs;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.service.AnnotationService;
Expand Down Expand Up @@ -53,6 +54,17 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
private Set<JavaType.Variable> privateFieldsToBeFinalized = new HashSet<>();

@Nullable
private SourceFile topLevel;

@Override
public @Nullable J visit(@Nullable Tree tree, ExecutionContext ctx) {
if (topLevel == null && tree instanceof SourceFile) {
topLevel = (SourceFile) tree;
}
return super.visit(tree, ctx);
}

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
if (!service(AnnotationService.class).getAllAnnotations(getCursor()).isEmpty()) {
Expand Down Expand Up @@ -99,8 +111,8 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
return type != null ? v.withVariableType(type.withFlags(
Flag.bitMapToFlags(type.getFlagsBitMap() | Flag.Final.getBitMask()))) : null;
})).withModifiers(ListUtils.concat(mv.getModifiers(),
new J.Modifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, null,
J.Modifier.Type.Final, emptyList()))), ctx);
new J.Modifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, topLevel instanceof Cs ? "readonly" : "final",
topLevel instanceof Cs ? J.Modifier.Type.LanguageExtension : J.Modifier.Type.Final, emptyList()))), ctx);
}

return mv;
Expand All @@ -124,6 +136,7 @@ private List<J.VariableDeclarations.NamedVariable> collectPrivateFields(Cursor c
.map(J.VariableDeclarations.class::cast)
.filter(mv -> mv.hasModifier(J.Modifier.Type.Private)
&& !mv.hasModifier(J.Modifier.Type.Final)
&& !(topLevel instanceof Cs || mv.getModifiers().stream().noneMatch(m -> "readonly".equals(m.getKeyword())))
&& !mv.hasModifier(J.Modifier.Type.Volatile))
.filter(mv -> !anyAnnotationApplied(new Cursor(bodyCursor, mv)))
.map(J.VariableDeclarations::getVariables)
Expand Down

0 comments on commit 8e693d2

Please sign in to comment.