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

org.openrewrite.staticanalysis.FinalClass is wrong for nested sub classes #372

Open
Bananeweizen opened this issue Oct 16, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Bananeweizen
Copy link
Contributor

org.openrewrite.staticanalysis.FinalClass works on wrong assumptions. A non-final class with no public constructor can have nested static sub classes, and in that case it cannot be made final.

What is the smallest, simplest way to reproduce the problem?

public class Reproducer {

    private Reproducer(final String name) {
        //...
    }

    public static final class Sub extends Reproducer {

        public Sub() {
            super("subclass");
        }
    }
}

What did you expect to see?

no change

What did you see instead?

final added to the top level class

Are you interested in contributing a fix to OpenRewrite?

Maybe. Have to look for how to get the information that there are nested sub classes.

@Bananeweizen Bananeweizen added the bug Something isn't working label Oct 16, 2024
@timtebeek
Copy link
Contributor

Thanks for reporting this variant! To detect sub classes you can either expand this loop

for (Statement s : cd.getBody().getStatements()) {
if (s instanceof J.MethodDeclaration && ((J.MethodDeclaration) s).isConstructor()) {
J.MethodDeclaration constructor = (J.MethodDeclaration) s;
constructorCount++;
if (!constructor.hasModifier(J.Modifier.Type.Private)) {
allPrivate = false;
}
}
if (constructorCount > 0 && !allPrivate) {
return cd;
}
}

Or add a new JavaIsoVisitor<>() { ... } that overrides visitClassDeclaration, and visit on the outer classDeclaration.getBody() and set an AtomicBoolean if any matching nested class declarations are found.

@timtebeek timtebeek moved this to Backlog in OpenRewrite Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Backlog
Development

No branches or pull requests

2 participants