From 4de02425709f29a0c4917b37162ab3b210cc0b1c Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 11 Jan 2024 08:52:30 +0100 Subject: [PATCH] `MissingOverrideAnnotation`: Ignore Kotlin sources Fixes: #234 --- .../staticanalysis/MissingOverrideAnnotation.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/org/openrewrite/staticanalysis/MissingOverrideAnnotation.java b/src/main/java/org/openrewrite/staticanalysis/MissingOverrideAnnotation.java index 5cd3e26fc..3d5150c2f 100644 --- a/src/main/java/org/openrewrite/staticanalysis/MissingOverrideAnnotation.java +++ b/src/main/java/org/openrewrite/staticanalysis/MissingOverrideAnnotation.java @@ -25,6 +25,7 @@ import org.openrewrite.java.service.AnnotationService; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.TypeUtils; +import org.openrewrite.kotlin.tree.K; import java.time.Duration; import java.util.Collections; @@ -70,6 +71,12 @@ public TreeVisitor getVisitor() { private class MissingOverrideAnnotationVisitor extends JavaIsoVisitor { private final AnnotationMatcher OVERRIDE_ANNOTATION = new AnnotationMatcher("@java.lang.Override"); + @Override + public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { + // Kotlin has a dedicated `override` keyword which is enforced by the compiler + return super.isAcceptable(sourceFile, ctx) && !(sourceFile instanceof K.CompilationUnit); + } + private Cursor getCursorToParentScope(Cursor cursor) { return cursor.dropParentUntil(is -> is instanceof J.NewClass || is instanceof J.ClassDeclaration); }