rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

Reduce duplication in KotlinFileChecker, GroovyFileChecker, ... with generic tree type instance of checker

Open timtebeek opened this issue 2 years ago • 1 comments

What problem are you trying to solve?

Reduce duplication, also going forward with other languages, in KotlinFileChecker and equivalents

                Preconditions.and(
                        Preconditions.not(new KotlinFileChecker<>()),
                        Preconditions.not(new GroovyFileChecker<>())),

Describe the solution you'd like

https://github.com/openrewrite/rewrite-static-analysis/pull/142/files#r1272998534

We could probably make this a bit more generic, so that it can cover even more ground.

@Value
public class IsInstanceOf<P> extends TreeVisitor<Tree, P> {
    Class<? extends Tree> treeType;

    @Nullable
    public Tree visit(@Nullable Tree tree, P p) {
        if (tree != null && treeType.isInstance(tree.getClass()) {
            return SearchResult.found(tree);
        }
        return tree;
    }
}

Then this may be a candidate for inclusion down in rewrite-core.

Have you considered any alternatives or workarounds?

We can also only keep this generic class in rewrite-static-analysis, since that depends on rewrite-kotlin which is not in rewrite-core. A lot of projects depend on rewrite-static-analysis further down, so that could help.

Additional context

Discovered on https://github.com/openrewrite/rewrite-static-analysis/pull/142

timtebeek avatar Jul 27 '23 08:07 timtebeek

Probably best to look at this once rewrite-kotlin has been merged into openrewrite/rewrite; that way the generic class, and it's two+ implementations will be available downstream.

timtebeek avatar Jul 27 '23 13:07 timtebeek