Skip to content

Deprecated StepBuilderFactory/JobBuilderFactory types left alone when it's not used in same Java file #788

@dcsekar

Description

@dcsekar

While using the "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_4", we expected that it will remove StepBuilderFactory and JobBuilderFactory or change it to StepBuilder/JobBuilder. When the deprecated StepBuilderFactory/JobBuilderFactory instances are defined and if it's not used in the same Java file, then it's not getting removed.
But when the same java file has some function using that instance say "Job myJob(Step step) { return this.jobBuilderFactory.get ... }" , then it's getting changed to "Job myJob(Step step, JobRepository jobRepository)" and these instances are removed.

What problem are you trying to solve?

After the run of UpgradeSpringBoot_3_4 the StepBuilderFactory and JobBuilderFactory instances should be removed.

What version of OpenRewrite are you using?

org.openrewrite.recipe:rewrite-spring latest version

How are you running OpenRewrite?

Using Maven multi-module project.

What is the smallest, simplest way to reproduce the problem?

import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

public class StepConfig {
    protected StepBuilderFactory stepBuilderFactory;

    protected JobBuilderFactory jobBuilderFactory;
}

What did you expect to see?

Basically the content is not changed as expected. Either it can remove the stepBuilderFactory/jobBuilderFactory and change type to StepBuilder/JobBuilder

What did you see instead?

import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

public class StepConfig {
    protected StepBuilderFactory stepBuilderFactory;

    protected JobBuilderFactory jobBuilderFactory;
}
@Issue("https://github.com/openrewrite/rewrite-spring/issues/788")
@Test
void replaceBuilderFactoryTest() {
    rewriteRun(spec -> spec.recipeFromResources("org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_4"),
      java(
        """
          package com.example.batch;

          import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
          import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.context.annotation.Configuration;

          public class StepConfig {
              protected StepBuilderFactory stepBuilderFactory;

              protected JobBuilderFactory jobBuilderFactory;
          }
          """, spec -> spec.path("StepConfig.java")
      ),
      java(
        """
          package com.example.batch;

          import org.springframework.batch.core.Job;
          import org.springframework.batch.core.Step;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.context.annotation.Bean;
          import org.springframework.context.annotation.Configuration;

          @Configuration
          public class StepConfigExt extends StepConfig {

              @Bean
              Job myJob(Step step) {
                  return this.jobBuilderFactory.get("myJob")
                      .start(step)
                      .build();
              }
          }
          """
        , """
          package com.example.batch;

          import org.springframework.batch.core.Job;
          import org.springframework.batch.core.Step;
          import org.springframework.batch.core.job.builder.JobBuilder;
          import org.springframework.batch.core.repository.JobRepository;
          import org.springframework.context.annotation.Bean;
          import org.springframework.context.annotation.Configuration;

          @Configuration
          public class StepConfigExt extends StepConfig {

              @Bean
              Job myJob(Step step, JobRepository jobRepository) {
                  return new JobBuilder("myJob", jobRepository)
                      .start(step)
                      .build();
              }
          }
          """, spec -> spec.path("StepConfigEtx.java")
      )
    );
}

Are you interested in contributing a fix to OpenRewrite?

No

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions