-
Notifications
You must be signed in to change notification settings - Fork 460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace static imports with non-static imports #1379
Comments
This is not possible right now, but we'd be happy to merge a PR which added support for this. |
@szpigielm I was going to implement this since I would be intersted in using this as well. I was going to add to the Java extension a function called
The basic case is straight forward. Instead of
Replace with
The more complicated case would be with wildcard imports, in order for that to work we would need access to the compilation classpath which is beyond the scope of most formatters. Any suggestions on how to handle the wildcard static imports? |
In this place spotless/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java Lines 275 to 294 in 8d1467f
You have:
so that should be everything you need. I'm not sure how to make it work with maven, but it's fine to have features which only work for one plugin or another. |
Would this be possible to do in the maven plugin if the dependecy for org.eclipse.jdt.ls.core is included since it has a helper method that does this? https://github.com/eclipse/eclipse.jdt.ls/blob/master/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/StaticAccessUsesClassNameCleanUp.java It atleast works in VSCode when having the qualifyStaticMembers identifier set in the redhat.java VSCode plugin import static org.junit.jupiter.api.Assertions.*;
public class Test {
@Test
public void fake_test() {
assertTrue(true);
assertFalse(true);
}
} to import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Assertions;
public class Test {
@Test
public void fake_test() {
Assertions.assertTrue(true);
Assertions.assertFalse(true);
}
} |
Or as comment from jdbuncan suggested #1532 (comment) OpenRewrite https://docs.openrewrite.org/reference/recipes/java/nostaticimport |
It's definitely possible. The trouble is that currently Spotless provides only source code to the formatters. It does not provide compiled jars or the classpath. Normally people can add a FormatterStep to Spotless and making it work in the build plugins is very little work. In this case, the classpath will have to piped from the build plugin to the FormatterStep, which we have never done before. I know how to do that in the Gradle plugin (shown above), and it can certainly be done in Maven, I just don't know how. Once someone has done it once, we'll be able to standardize it and make it easier to make other classpath-dependent steps in the future. |
I have the classpath access working with Gradle, I just need to implement the FormatterStep logic. Grad school classes started again so I'm a little crunched for time. Like Ned, I don't have any experience with the Maven API but I have a lot of experience with the Gradle API. If there's someone that knows how to get the classpath in Maven, I'd certainly add that to the PR. |
Amazing @Chasson1992, thanks for getting something going! Don't worry about the Maven stuff at all, that should be a separate PR. From our root
|
Hi is it possible to setup spotless to replace static imports with non-static imports in Java?
Example:
Static import:
Replace with
The text was updated successfully, but these errors were encountered: