15
15
*/
16
16
package org .openrewrite .java .migrate .jakarta ;
17
17
18
+ import lombok .Value ;
18
19
import org .jspecify .annotations .Nullable ;
19
20
import org .openrewrite .ExecutionContext ;
20
21
import org .openrewrite .ScanningRecipe ;
21
22
import org .openrewrite .Tree ;
22
23
import org .openrewrite .TreeVisitor ;
23
- import org .openrewrite .java .JavaIsoVisitor ;
24
+ import org .openrewrite .java .marker . JavaProject ;
24
25
import org .openrewrite .java .search .FindAnnotations ;
25
26
import org .openrewrite .java .tree .J ;
26
27
import org .openrewrite .marker .SearchResult ;
27
28
28
- import java .util .concurrent .atomic .AtomicBoolean ;
29
+ import java .util .HashSet ;
30
+ import java .util .Set ;
29
31
30
- public class HasNoJakartaNullAnnotations extends ScanningRecipe <AtomicBoolean > {
32
+ public class HasNoJakartaNullAnnotations extends ScanningRecipe <HasNoJakartaNullAnnotations . Accumulator > {
31
33
@ Override
32
34
public String getDisplayName () {
33
35
return "Project has no Jakarta null annotations" ;
@@ -38,38 +40,43 @@ public String getDescription() {
38
40
return "Search for @Nonnull and @Nullable annotations, mark all source as found if no annotations are found." ;
39
41
}
40
42
43
+ @ Value
44
+ public static class Accumulator {
45
+ Set <JavaProject > projectsWithDependency ;
46
+ }
47
+
41
48
@ Override
42
- public AtomicBoolean getInitialValue (ExecutionContext ctx ) {
43
- return new AtomicBoolean ( );
49
+ public Accumulator getInitialValue (ExecutionContext ctx ) {
50
+ return new Accumulator ( new HashSet <>() );
44
51
}
45
52
46
53
@ 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 >() {
49
56
@ 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 ));
57
64
}
58
- return cu ;
65
+ return tree ;
59
66
}
60
67
};
61
68
}
62
69
63
70
@ Override
64
- public TreeVisitor <?, ExecutionContext > getVisitor (AtomicBoolean acc ) {
71
+ public TreeVisitor <?, ExecutionContext > getVisitor (HasNoJakartaNullAnnotations . Accumulator acc ) {
65
72
return new TreeVisitor <Tree , ExecutionContext >() {
66
73
@ Override
67
74
public Tree visit (@ Nullable Tree tree , ExecutionContext ctx ) {
68
75
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 ) ;
73
80
}
74
81
};
75
82
}
0 commit comments