diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/SelectRecipeExamplesTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/SelectRecipeExamplesTest.java index 99889c1a12b..ebeaae33823 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/SelectRecipeExamplesTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/SelectRecipeExamplesTest.java @@ -412,4 +412,29 @@ void test1() { ) ); } + + @Test + void skipNestedClasses() { + rewriteRun( + java( + """ + import org.junit.jupiter.api.Nested; + import org.junit.jupiter.api.Test; + import org.openrewrite.test.RewriteTest; + + import static org.openrewrite.test.SourceSpecs.text; + + class OuterClass implements RewriteTest { + @Nested + class InnerClass { + @Test + void test1() { + rewriteRun(text("before", "after")); + } + } + } + """ + ) + ); + } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/recipes/SelectRecipeExamples.java b/rewrite-java/src/main/java/org/openrewrite/java/recipes/SelectRecipeExamples.java index aab1e056815..25820e3771b 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/recipes/SelectRecipeExamples.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/recipes/SelectRecipeExamples.java @@ -33,11 +33,10 @@ public class SelectRecipeExamples extends Recipe { private static final String DOCUMENT_EXAMPLE_ANNOTATION_FQN = "org.openrewrite.DocumentExample"; - private static final AnnotationMatcher TEST_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api" + - ".Test"); + private static final AnnotationMatcher TEST_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api.Test"); private static final AnnotationMatcher ISSUE_ANNOTATION_MATCHER = new AnnotationMatcher("@org.openrewrite.Issue"); - private static final AnnotationMatcher DISABLED_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter" + - ".api.Disabled"); + private static final AnnotationMatcher DISABLED_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api.Disabled"); + private static final AnnotationMatcher NESTED_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api.Nested"); private static final AnnotationMatcher DOCUMENT_EXAMPLE_ANNOTATION_MATCHER = new AnnotationMatcher("@" + DOCUMENT_EXAMPLE_ANNOTATION_FQN); @@ -101,6 +100,12 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, return method; } + J.ClassDeclaration clazz = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance).getValue(); + boolean insideNestedClass = clazz != null && clazz.getLeadingAnnotations().stream().anyMatch(NESTED_ANNOTATION_MATCHER::matches); + if (insideNestedClass) { + return method; + } + // a good recipe example should have both before and after. boolean isAGoodExample = new JavaIsoVisitor() { @Override