openrewrite / rewrite-static-analysis

OpenRewrite recipes for identifying and fixing static analysis issues.
Apache License 2.0
27 stars 43 forks source link

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

Open timtebeek opened 11 months ago

timtebeek commented 11 months ago

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 commented 11 months ago

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.