Skip to content

Conversation

@Anmol202005
Copy link
Collaborator

Fixes: #80.

@Anmol202005 Anmol202005 marked this pull request as draft August 29, 2025 04:36
@Anmol202005 Anmol202005 marked this pull request as ready for review August 31, 2025 17:41
@Anmol202005
Copy link
Collaborator Author

@rdiachenko kindly review.

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new LocalVariableVisitor();
return new JavaIsoVisitor<>() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we create this anon impl JavaIsoVisitor, after that LocalVariableVisitor and MarkViolationVisitor

It looks like all of these objects have the same generic extension, and I am curious, instead of having three visitor objects, can we have only one?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it true, that you have to visit each variable declaration twice?

The first time to mark everything, the second time to add modifiers. If so, maybe we can have two sequential visitors

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we visit each variable twice.

Copy link
Collaborator Author

@Anmol202005 Anmol202005 Sep 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the correct approach IMO.
kindly see: https://docs.openrewrite.org/authoring-recipes/multiple-visitors

}

public static class FinalLocalVariableMarker implements Marker {
private final UUID id;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need id? couldn't find usages of this field

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marker interface contains abstract method like getId() which we need to implement.
Since Id is not used i have updated the marker class as :

public static class FinalLocalVariableMarker implements Marker {

        public FinalLocalVariableMarker() {}

        @Override
        public UUID getId() {
            return null;
        }

        @Override
        public <M extends Marker> M withId(UUID id) {
            return null;
        }
    }

}

@Override
public J.Block visitBlock(J.Block block, ExecutionContext executionContext) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand why we should specifically handle visiting blocks. My assumption was that any variable declaration node would be visited in visitVariableDeclarations?

Could you please provide an example of a variable declaration that won't be handled by visitVariableDeclarations but will be handled by visitBlock

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kindly see: #80

example:

int a = 1, b = 2, c = 3; // 1 violation
...
a = 7;
b = 8;

multiple variable declaration can not be handled by visitVariableDeclarations.

}

private final class LocalVariableVisitor extends JavaIsoVisitor<ExecutionContext> {
private final class MarkViolationVisitor extends JavaIsoVisitor<ExecutionContext> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely need more documentation around these two visitors. The logic looks cumbersome, and I think we should provide some details on how it works

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/checkstyle/autofix/recipe/FinalLocalVariable.java
    • lines 28-27
    • lines 243-244

@Anmol202005 Anmol202005 force-pushed the tryTrial branch 2 times, most recently from 9374d70 to d85995b Compare September 1, 2025 04:34
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/checkstyle/autofix/recipe/FinalLocalVariable.java
    • lines 249-249

Copy link
Member

@rdiachenko rdiachenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Anmol202005 could you go to Checkstyle, take all test inputs from finallocalvariable subpackage in tests, and put them into this PR? Lots of cases are already covered by those test inputs, let's reuse them and check the recipe works expected.
image

@Anmol202005
Copy link
Collaborator Author

@rdiachenko done. Kindly review

Copy link
Member

@rdiachenko rdiachenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@rdiachenko rdiachenko assigned timurt and unassigned rdiachenko Sep 4, 2025
Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok to merge.

@Anmol202005 , do we test compilation of Input and Output files by maven compilation ?

I see we did not, we definetely should before this project goes to massive development.
example of how we did this in main project: https://github.com/checkstyle/checkstyle/blob/1f71110827d1b2b71f4a2434ab20253d344fcbb0/pom.xml#L1462-L1473 please add in separate issue.

We shold be confident that after auto update is done, code is still compilable.

@Anmol202005
Copy link
Collaborator Author

Anmol202005 commented Sep 6, 2025

ok to merge.

@Anmol202005 , do we test compilation of Input and Output files by maven compilation ?

I see we did not, we definetely should before this project goes to massive development. example of how we did this in main project: https://github.com/checkstyle/checkstyle/blob/1f71110827d1b2b71f4a2434ab20253d344fcbb0/pom.xml#L1462-L1473 please add in separate issue.

We shold be confident that after auto update is done, code is still compilable.

@rdiachenko @romani
created issue: #109

@romani romani merged commit fe50883 into checkstyle:main Sep 6, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix FinalLocalVariable Recipe for Multiple Variable Declarations

4 participants