Skip to content

Commit 24c6e63

Browse files
committed
Minimize visibility & reduce overloaded constructors
1 parent 6af6b4a commit 24c6e63

File tree

6 files changed

+45
-58
lines changed

6 files changed

+45
-58
lines changed

src/main/java/org/openrewrite/java/migrate/joda/JodaTimeFlowSpec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JODA_CLASS_PATTERN;
2525

26-
public class JodaTimeFlowSpec extends DataFlowSpec {
26+
class JodaTimeFlowSpec extends DataFlowSpec {
2727

2828
@Override
2929
public boolean isSource(@NonNull DataFlowNode srcNode) {

src/main/java/org/openrewrite/java/migrate/joda/JodaTimeRecipe.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public JodaTimeScanner getScanner(Accumulator acc) {
4848

4949
@Override
5050
public JodaTimeVisitor getVisitor(Accumulator acc) {
51-
return new JodaTimeVisitor(acc);
51+
return new JodaTimeVisitor(acc, true, new LinkedList<>());
5252
}
5353

5454
@Getter

src/main/java/org/openrewrite/java/migrate/joda/JodaTimeScanner.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import lombok.Getter;
2020
import lombok.NonNull;
2121
import lombok.RequiredArgsConstructor;
22+
import org.jspecify.annotations.Nullable;
2223
import org.openrewrite.Cursor;
2324
import org.openrewrite.ExecutionContext;
2425
import org.openrewrite.analysis.dataflow.Dataflow;
@@ -35,21 +36,17 @@
3536

3637
import 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
}

src/main/java/org/openrewrite/java/migrate/joda/JodaTimeVisitor.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*;
3434

35-
public class JodaTimeVisitor extends ScopeAwareVisitor {
35+
class JodaTimeVisitor extends ScopeAwareVisitor {
3636

3737
private final boolean safeMigration;
3838
private final JodaTimeRecipe.Accumulator acc;
@@ -43,18 +43,6 @@ public JodaTimeVisitor(JodaTimeRecipe.Accumulator acc, boolean safeMigration, Li
4343
this.safeMigration = safeMigration;
4444
}
4545

46-
public JodaTimeVisitor(JodaTimeRecipe.Accumulator acc, boolean safeMigration) {
47-
this(acc, safeMigration, new LinkedList<>());
48-
}
49-
50-
public JodaTimeVisitor(JodaTimeRecipe.Accumulator acc) {
51-
this(acc, true);
52-
}
53-
54-
public JodaTimeVisitor() {
55-
this(new JodaTimeRecipe.Accumulator());
56-
}
57-
5846
@Override
5947
public @NonNull J visitCompilationUnit(@NonNull J.CompilationUnit cu, @NonNull ExecutionContext ctx) {
6048
maybeRemoveImport(JODA_DATE_TIME);

src/main/java/org/openrewrite/java/migrate/joda/ScopeAwareVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import java.util.Set;
3030

3131
@AllArgsConstructor
32-
public class ScopeAwareVisitor extends JavaVisitor<ExecutionContext> {
32+
class ScopeAwareVisitor extends JavaVisitor<ExecutionContext> {
3333
protected final LinkedList<VariablesInScope> scopes;
3434

3535
@Override

0 commit comments

Comments
 (0)