Skip to content

Avoid uncompilable output when migrating JobBuilderFactory/StepBuilderFactory#1036

Merged
timtebeek merged 2 commits into
mainfrom
spring-batch-jbf-sbf-guard
Jun 16, 2026
Merged

Avoid uncompilable output when migrating JobBuilderFactory/StepBuilderFactory#1036
timtebeek merged 2 commits into
mainfrom
spring-batch-jbf-sbf-guard

Conversation

@steve-aom-elliott

@steve-aom-elliott steve-aom-elliott commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Problem

When a class injects JobBuilderFactory or StepBuilderFactory via a multi-arg constructor, or exposes the factory through accessor methods, the current recipes produce uncompilable code:

Changes

Field deletion is now usage-aware (both recipes). Before deleting a JobBuilderFactory/StepBuilderFactory field, each visitor scans the class for type-aware references to the field outside the methods it will rewrite (constructors and methods containing .get(...)). The check uses J.Identifier.getFieldType() and confirms the declaring type matches the enclosing class, so local variables and parameters that happen to share the field's simple name are not false positives. If outside references exist (getters, setters, helper methods), the field is preserved and constructor parameter removal is skipped — the field, parameter, and body assignment remain wired up for the developer to clean up.

Constructor body cleanup moves in lockstep with parameter removal (both recipes). When a JobBuilderFactory/StepBuilderFactory parameter is dropped from a method declaration, any matching this.<field> = <removedParam>; body assignment is removed rather than being left referencing a parameter that no longer exists.

MigrateStepBuilderFactory gets a constructor carve-out. AddJobRepositoryVisitor.visitMethodDeclaration now rewrites constructors that take a StepBuilderFactory parameter, mirroring the existing behavior in MigrateJobBuilderFactory. Gated by the field-preservation guard above.

Tests

Five new tests across MigrateJobBuilderFactoryTest and MigrateStepBuilderFactoryTest:

…lderFactory`

When a class injects `JobBuilderFactory` or `StepBuilderFactory` via a multi-arg
constructor or exposes the factory through accessor methods, the existing recipe
deleted the field while leaving the constructor body assignment, accessor methods,
and (for `StepBuilderFactory`) the constructor parameter dangling. The result was
uncompilable code.

Three changes:

1. Before deleting a `JobBuilderFactory`/`StepBuilderFactory` field, both visitors
   now scan the class for type-aware references to the field outside the methods
   they rewrite (constructors and methods containing `.get(...)`). If references
   exist (getters, setters, helper methods), the field is preserved and constructor
   parameter removal is also skipped so the field, parameter, and body assignment
   stay wired up.
2. When a `JobBuilderFactory`/`StepBuilderFactory` parameter is dropped from a
   method, any matching `this.<field> = <removedParam>;` body assignment is
   removed in lockstep rather than being left referencing the removed parameter.
3. `MigrateStepBuilderFactory.AddJobRepositoryVisitor.visitMethodDeclaration`
   now also rewrites constructors that take a `StepBuilderFactory` parameter,
   mirroring the existing constructor handling in `MigrateJobBuilderFactory`.

Closes #788
Closes #789
@steve-aom-elliott steve-aom-elliott added bug Something isn't working spring-batch labels Jun 11, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jun 11, 2026
@steve-aom-elliott steve-aom-elliott moved this from In Progress to Ready to Review in OpenRewrite Jun 12, 2026
@timtebeek timtebeek merged commit 6c0c957 into main Jun 16, 2026
1 check passed
@timtebeek timtebeek deleted the spring-batch-jbf-sbf-guard branch June 16, 2026 18:22
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

2 participants