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

Skip nested classes for document examples #3798

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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<AtomicBoolean>() {
@Override
Expand Down