1515 */
1616package org .openrewrite .java .migrate .jakarta ;
1717
18+ import lombok .Value ;
1819import org .jspecify .annotations .Nullable ;
1920import org .openrewrite .ExecutionContext ;
2021import org .openrewrite .ScanningRecipe ;
2122import org .openrewrite .Tree ;
2223import org .openrewrite .TreeVisitor ;
23- import org .openrewrite .java .JavaIsoVisitor ;
24+ import org .openrewrite .java .marker . JavaProject ;
2425import org .openrewrite .java .search .FindAnnotations ;
2526import org .openrewrite .java .tree .J ;
2627import org .openrewrite .marker .SearchResult ;
2728
28- import java .util .concurrent .atomic .AtomicBoolean ;
29+ import java .util .HashSet ;
30+ import java .util .Set ;
2931
30- public class HasNoJakartaNullAnnotations extends ScanningRecipe <AtomicBoolean > {
32+ public class HasNoJakartaNullAnnotations extends ScanningRecipe <HasNoJakartaNullAnnotations . Accumulator > {
3133 @ Override
3234 public String getDisplayName () {
3335 return "Project has no Jakarta null annotations" ;
@@ -38,38 +40,43 @@ public String getDescription() {
3840 return "Search for @Nonnull and @Nullable annotations, mark all source as found if no annotations are found." ;
3941 }
4042
43+ @ Value
44+ public static class Accumulator {
45+ Set <JavaProject > projectsWithDependency ;
46+ }
47+
4148 @ Override
42- public AtomicBoolean getInitialValue (ExecutionContext ctx ) {
43- return new AtomicBoolean ( );
49+ public Accumulator getInitialValue (ExecutionContext ctx ) {
50+ return new Accumulator ( new HashSet <>() );
4451 }
4552
4653 @ Override
47- public TreeVisitor <?, ExecutionContext > getScanner (AtomicBoolean acc ) {
48- return new JavaIsoVisitor < ExecutionContext >() {
54+ public TreeVisitor <?, ExecutionContext > getScanner (HasNoJakartaNullAnnotations . Accumulator acc ) {
55+ return new TreeVisitor < Tree , ExecutionContext >() {
4956 @ Override
50- public J . CompilationUnit visitCompilationUnit ( J . CompilationUnit cu , ExecutionContext ctx ) {
51- J . CompilationUnit c = super . visitCompilationUnit ( cu , ctx ) ;
52- if (! acc . get () ) {
53- if ((! FindAnnotations . find ( c , "@jakarta.annotation.Nonnull" , true ). isEmpty ()) ||
54- ( !FindAnnotations .find (c , "@jakarta.annotation.Nullable " , true ).isEmpty ())) {
55- acc . set ( true );
56- }
57+ public Tree visit ( @ Nullable Tree tree , ExecutionContext ctx ) {
58+ assert tree != null ;
59+ if (tree instanceof J ) {
60+ tree . getMarkers (). findFirst ( JavaProject . class )
61+ . filter ( __ -> !FindAnnotations .find (( J ) tree , "@jakarta.annotation.Nonnull " , true ).isEmpty () ||
62+ ! FindAnnotations . find (( J ) tree , "@jakarta.annotation.Nullable" , true ). isEmpty ())
63+ . ifPresent ( it -> acc . getProjectsWithDependency (). add ( it ));
5764 }
58- return cu ;
65+ return tree ;
5966 }
6067 };
6168 }
6269
6370 @ Override
64- public TreeVisitor <?, ExecutionContext > getVisitor (AtomicBoolean acc ) {
71+ public TreeVisitor <?, ExecutionContext > getVisitor (HasNoJakartaNullAnnotations . Accumulator acc ) {
6572 return new TreeVisitor <Tree , ExecutionContext >() {
6673 @ Override
6774 public Tree visit (@ Nullable Tree tree , ExecutionContext ctx ) {
6875 assert tree != null ;
69- if (! acc . get ()) {
70- return SearchResult . found ( tree , "Project has no Jakarta null annotations" );
71- }
72- return tree ;
76+ return tree . getMarkers (). findFirst ( JavaProject . class )
77+ . filter ( it -> ! acc . getProjectsWithDependency (). contains ( it ))
78+ . map ( __ -> SearchResult . found ( tree , "Project has no Jakarta null annotations" ))
79+ . orElse ( tree ) ;
7380 }
7481 };
7582 }
0 commit comments