rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

Handling java module-info files

Open georgeVasiliu opened this issue 2 years ago • 6 comments

Steps to recreate the scenario:

  • Create a minimal Spring Boot application from initiliazr that uses Java and Gradle Groovy for the build system and only contains the following single class. (ignore resources/app port or other configurations)
 @SpringBootApplication
public class ExampleApplication{
	public static void main(String[] args) {
		ExampleApplication.run(ExampleApplication.class, args);
	}

	@RestController
	static class BootController {
		@GetMapping("/")
		public ResponseEntity<String> askQuestion() {
			return ResponseEntity.ok("Hello! ");
		}
	}
}
  • Configure the openrewrite plugin to activate the "org.openrewrite.java.format.AutoFormat" recipe in order to automate the formatting of the source code
rewrite {
    activeRecipe("org.openrewrite.java.format.AutoFormat")
}
  • Create a module-info.java for the application that contains and exposes everything required by the application
module com.example.application{
	requires spring.boot;
	requires spring.boot.autoconfigure;
	requires spring.web;

	opens com.example.application to spring.core;
	
	exports com.example.application;
}
  • Run the rewriteDryRun task and then you will see this output (slimmed down from the whole output to the relevant part):
Validating active recipes
Parsing sources from project com.example.app;ication
All sources parsed, running active recipes: org.openrewrite.java.format.AutoFormat
These recipes would make results to src/main/java/module-info.java:
    org.openrewrite.java.format.AutoFormat
  • Run the rewriteRun task and you will see that the module-info.java has been emptied and the task output mentions the changes:

image

All sources parsed, running active recipes: org.openrewrite.java.format.AutoFormat
Changes have been made to src/main/java/module-info.java by:
    org.openrewrite.java.format.AutoFormat

Question: Is it possible to ignore the module-info.java file? By using the "exclusion" feature from the plugin, it does not work (I have tried all possible combinations until I realized the phrasing used for the "exclusion" feature (in which it mentioned it ignores NON-Java sources, whiles the module-info does end indeed in .java). I would love either to have the plugin do this automatically (I can look over the source code and maybe fork it and try a fix) or have some sort of parameter or be able to include in the "exclusion" also Java source files. If you need anymore info, please let me know.

Note: The same seems to be true for package-info.java files.

georgeVasiliu avatar Jun 02 '22 06:06 georgeVasiliu

Thanks for the detailed report @georgeVasiliu! I am able to reproduce the bug and will work on a solution.

pway99 avatar Jun 06 '22 22:06 pway99

Hello @georgeVasiliu, As it turns out the latest release 4.25.2 adds support for exclusions, here is an example of excluding the module-info.java file.

<plugin>
	<groupId>org.openrewrite.maven</groupId>
	<artifactId>rewrite-maven-plugin</artifactId>
	<version>4.26.0-SNAPSHOT</version>
	<configuration>
		<activeRecipes>
			<recipe>org.openrewrite.java.format.AutoFormat</recipe>
		</activeRecipes>
		<exclusions>
			<exclusion>**/module-info.java</exclusion>
		</exclusions>
	</configuration>
</plugin>

There is an effort going on to update the Java 11+ Parsers with support module-info source files which will provide the ability to refactor them via rewrite recipes.

Again thanks for the detailed and informative issue and please accept my apology for overlooking your question on the possibility of excluding source files and offering to help.

pway99 avatar Jun 16 '22 18:06 pway99

Hello @pway99,

Thank you very much! I was using the gradle-plugin, not the maven-plugin, but on the latest version the exclusion works for the module-info.java files. Glad to see it's working now, so thank you!

Wish you good luck in your endeavours. You can close the issue in my opinion (I am not sure whether I should do it or not).

georgeVasiliu avatar Jun 17 '22 07:06 georgeVasiliu

Thanks @georgeVasiliu, Glad to hear exclusions are working for you :) How about we keep this one open while #1925 is in progress.

pway99 avatar Jun 17 '22 14:06 pway99