Skip to content

Adding ImportLayoutStyle to OrderImports recipe removes all import statements... #5032

Open
@ajoshi66

Description

Thank you for creating this framework and the platform.

Problem Context:

This one is straight from the "getting started" documentation (https://docs.openrewrite.org/running-recipes/getting-started). I cloned the project "spring-petclinic-migration" and setup the plugin (version 6.1.1.) as documented and configured the plugin for OrderImports recipe.

The recipe worked fine. The order of imports were changed from the original. However, I found that the class org.springframework.samples.petclinic.owner.Owner got its javax.persistence classes got collapsed as javax.persistence.*. As I prefer the imports to include individual classes, I wanted to change the style. Based on another documentation (https://docs.openrewrite.org/concepts-and-explanations/styles), when I added the rewrite.yml and configured maven plugin to include the style. At this time, the rewrite:run removed all imports from all classes.

Creating this as new issue, as I could not find a similar one reported. Apologize, if this is already reported/resolved.

What version of OpenRewrite are you using?

I am using maven plugin version 6.1.1. I do have Java JDK 21 installed; but maven is configured to use version JDK 11.

How are you running OpenRewrite?

From the command line. Not using any IDE. I am using WinMerge to view the differences against original.

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

  1. Cloned the project spring pet clinic from the Github.
  2. Configured the maven plugin as documented.
  3. Running the recipe with Order Imports works fine
  4. Added the rewrite.yml with ImportLayoutStyle and configured the maven plugin with the style.
  5. Running the recipe removed all imports.

Here is the maven plugin configuration:

<plugin>
	<groupId>org.openrewrite.maven</groupId>
	<artifactId>rewrite-maven-plugin</artifactId>
	<version>6.1.1</version>
	<configuration>
		<activeRecipes>
			<recipe>org.openrewrite.java.format.AutoFormat</recipe>
			<recipe>org.openrewrite.java.OrderImports</recipe>
		</activeRecipes>
		<activeStyles>
			<style>com.yourorg.YesTabsNoStarImports</style>
		</activeStyles>
	</configuration>
</plugin>

And here is the rewrite.yml

---
type: specs.openrewrite.org/v1beta/style
name: com.yourorg.YesTabsNoStarImports
styleConfigs:
  - org.openrewrite.java.style.TabsAndIndentsStyle:
      useTabCharacter: true
  - org.openrewrite.java.style.ImportLayoutStyle:
      classCountToUseStarImport: 9999

What did you expect to see?

The java class with the imports as below. But the entire section of imports were removed. From all classes.

package org.springframework.samples.petclinic.owner;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Digits;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.core.style.ToStringCreator;
import org.springframework.samples.petclinic.model.Person;

/**
 * Simple JavaBean domain object representing an owner.
 *
 * @author Ken Krebs
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @author Michael Isvy
 */
@Entity
@Table(name = "owners")
public class Owner extends Person {
   ...
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdocumentationImprovements or additions to documentation

    Type

    Projects

    • Status

      Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions