1919import lombok .Getter ;
2020import lombok .NonNull ;
2121import lombok .RequiredArgsConstructor ;
22+ import org .jspecify .annotations .Nullable ;
2223import org .openrewrite .Cursor ;
2324import org .openrewrite .ExecutionContext ;
2425import org .openrewrite .analysis .dataflow .Dataflow ;
3536
3637import static org .openrewrite .java .migrate .joda .templates .TimeClassNames .JODA_CLASS_PATTERN ;
3738
38- public class JodaTimeScanner extends ScopeAwareVisitor {
39+ class JodaTimeScanner extends ScopeAwareVisitor {
3940
4041 @ Getter
4142 private final JodaTimeRecipe .Accumulator acc ;
4243
4344 private final Map <NamedVariable , Set <NamedVariable >> varDependencies = new HashMap <>();
4445 private final Map <JavaType , Set <String >> unsafeVarsByType = new HashMap <>();
4546
46- public JodaTimeScanner (JodaTimeRecipe .Accumulator acc , LinkedList <VariablesInScope > scopes ) {
47- super (scopes );
48- this .acc = acc ;
49- }
50-
5147 public JodaTimeScanner (JodaTimeRecipe .Accumulator acc ) {
52- this (acc , new LinkedList <>());
48+ super (new LinkedList <>());
49+ this .acc = acc ;
5350 }
5451
5552 @ Override
@@ -68,7 +65,7 @@ public NamedVariable visitVariable(NamedVariable variable, ExecutionContext ctx)
6865 if (!variable .getType ().isAssignableFrom (JODA_CLASS_PATTERN )) {
6966 return variable ;
7067 }
71- // TODO: handle class variables && method parameters
68+ // TODO: handle class variables
7269 if (isClassVar (variable )) {
7370 acc .getUnsafeVars ().add (variable );
7471 return variable ;
@@ -93,7 +90,7 @@ public NamedVariable visitVariable(NamedVariable variable, ExecutionContext ctx)
9390 List <Expression > sinks = findSinks (cursor );
9491
9592 Cursor currentScope = getCurrentScope ();
96- new AddSafeCheckMarker (sinks ).visit (currentScope .getValue (), ctx , currentScope .getParent ());
93+ new AddSafeCheckMarker (sinks ).visit (currentScope .getValue (), ctx , currentScope .getParentOrThrow ());
9794 processMarkersOnExpression (sinks , variable );
9895 return variable ;
9996 }
@@ -113,7 +110,7 @@ public J.Assignment visitAssignment(J.Assignment assignment, ExecutionContext ct
113110 NamedVariable variable = mayBeVar .get ();
114111 Cursor varScope = findScope (variable );
115112 List <Expression > sinks = findSinks (new Cursor (getCursor (), assignment .getAssignment ()));
116- new AddSafeCheckMarker (sinks ).visit (varScope .getValue (), ctx , varScope .getParent ());
113+ new AddSafeCheckMarker (sinks ).visit (varScope .getValue (), ctx , varScope .getParentOrThrow ());
117114 processMarkersOnExpression (sinks , variable );
118115 return assignment ;
119116 }
@@ -217,7 +214,7 @@ private SafeCheckMarker getMarker(Expression expr, ExecutionContext ctx) {
217214 Expression boundaryExpr = boundary .getValue ();
218215 J j = new JodaTimeVisitor (new JodaTimeRecipe .Accumulator (), false , scopes )
219216 .visit (boundaryExpr , ctx , boundary .getParentTreeCursor ());
220- Set <NamedVariable > referencedVars = new HashSet <>();
217+ Set <@ Nullable NamedVariable > referencedVars = new HashSet <>();
221218 new FindVarReferences ().visit (expr , referencedVars , getCursor ().getParentTreeCursor ());
222219 AtomicBoolean hasJodaType = new AtomicBoolean ();
223220 new HasJodaType ().visit (j , hasJodaType );
@@ -257,10 +254,10 @@ private Optional<Cursor> findArgumentExprCursor() {
257254 }
258255 }
259256
260- private class FindVarReferences extends JavaIsoVisitor <Set <NamedVariable >> {
257+ private class FindVarReferences extends JavaIsoVisitor <Set <@ Nullable NamedVariable >> {
261258
262259 @ Override
263- public J .Identifier visitIdentifier (J .Identifier ident , Set <NamedVariable > vars ) {
260+ public J .Identifier visitIdentifier (J .Identifier ident , Set <@ Nullable NamedVariable > vars ) {
264261 if (!isJodaExpr (ident ) || ident .getFieldType () == null ) {
265262 return ident ;
266263 }
0 commit comments