openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.15k stars 321 forks source link

AddImport unecessarily formats white space #1548

Closed pway99 closed 2 years ago

pway99 commented 2 years ago

Problem

unecessary white-space formatting

Example diff

 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
  * To migrate your tests follow <a href="https://wiki.openmrs.org/display/docs/How+to+migrate+to+JUnit+5">How to migrate to JUnit 5</a>.
  * The JUnit 5 version of the class is {@link org.openmrs.test.jupiter.BaseContextSensitiveTest}.<p>
  */
-@ContextConfiguration(locations = { "classpath:applicationContext-service.xml", "classpath*:openmrs-servlet.xml",
-        "classpath*:moduleApplicationContext.xml", "classpath*:TestingApplicationContext.xml" })
-@TestExecutionListeners( { TransactionalTestExecutionListener.class, SkipBaseSetupAnnotationExecutionListener.class,
-        StartModuleExecutionListener.class })
+@ContextConfiguration(locations = {"classpath:applicationContext-service.xml", "classpath*:openmrs-servlet.xml",
+                               "classpath*:moduleApplicationContext.xml", "classpath*:TestingApplicationContext.xml"})
+@TestExecutionListeners({TransactionalTestExecutionListener.class, SkipBaseSetupAnnotationExecutionListener.class,
+                               StartModuleExecutionListener.class})
 @Transactional
 @Rollback
 @Deprecated
        runtimeProperties.setProperty(ModuleConstants.IGNORE_CORE_MODULES_PROPERTY, "true");

        try {
-           File tempappdir = File.createTempFile("appdir-for-unit-tests-", "");
-           tempappdir.delete(); // so we can make it into a directory
-           tempappdir.mkdir(); // turn it into a directory
+           File tempappdir = Files.createTempDirectory("appdir-for-unit-tests-" + "").toFile(); // turn it into a directory
            tempappdir.deleteOnExit(); // clean up when we're done with tests

            runtimeProperties.setProperty(OpenmrsConstants.APPLICATION_DATA_DIRECTORY_RUNTIME_PROPERTY, tempappdir

Recipes in example diff:

References:

pway99 commented 2 years ago

Identified this issue as being caused by AddImport when it autoformats the class

https://github.com/openrewrite/rewrite/blob/568320064d39bd716b521ffd0afa95e5699844e5/rewrite-java/src/main/java/org/openrewrite/java/AddImport.java#L148-L149