openrewrite / openrewrite/rewrite
Split method signature line into multiple lines based on the length
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3.7k
- Forks
- 570
- Avg merge
- 13h 12m
- Merged PRs (30d)
- 261
Description
What problem are you trying to solve?
Having https://github.com/spring-projects/spring-petclinic with spring-javaformat-maven-plugin validating the Java code
There is a splitLine feature that will split the line if it is over 120 characters: https://github.com/spring-io/spring-javaformat/blob/7db7c9765fbad3e1c0b8f021dc16533f9d5c10f2/spring-javaformat/spring-javaformat-formatter/src/main/resources/io/spring/javaformat/formatter/eclipse/formatter.prefs#L324
Applying org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_4 on https://github.com/spring-projects/spring-petclinic
causes the next change
@PostMapping("/owners/{ownerId}/edit")
public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result,
- @PathVariable("ownerId") int ownerId) {
+ @PathVariable int ownerId) {
if (result.hasErrors()) {
return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
}
Because the new method signature is less than 120 characters, the validation plugin provokes an error. The @PathVariable int ownerId parameter should be in the same line of the method signature because it will become less than 120 characters.
This can be found in other validators. Example: eclipse jdt
What precondition(s) should be checked before applying this recipe?
The length of the line of a MethodDeclaration and the styleConfig to applied (120 by default)
Describe the situation before applying the recipe
class Test {
public static void example(Object o1, String thisisabigparameter1, String thisisasecondbigparameter, String iDontcareThisName, String iWantToExceedTheLenght, String formattingThing) {
System.out.println("foo bar");
}
}
Describe the situation after applying the recipe
class Test {
public static void example(Object o1, String thisisabigparameter1, String thisisasecondbigparameter,
String iDontcareThisName, String iWantToExceedTheLenght, String formattingThing) {
System.out.println("foo bar");
}
}
After the 4th parameter reaches 120 characters -> the split is done before the 4th parameter.
Have you considered any alternatives or workarounds?
I tried to run IntelliJ style with AutoFormat ->, but it didn't work
I can also run my own plugin to format the code, but I would like to get things working directly from OpenRewrite
Any additional context
I think this could be extrapolated to other code sentences (example: class declaration with lots of interfaces over 120 characters)
Are you interested in contributing this recipe to OpenRewrite?
I am happy to contribute.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the formatter or validation entry point that handles Java method declarations and its related tests; the issue provides the spring-javaformat preference and an Eclipse JDT formatter example for comparison. Done means a method signature is split only when it exceeds the configured 120-character limit, while a shortened signature keeps parameters on the same line, with tests covering both cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100