-
Notifications
You must be signed in to change notification settings - Fork 486
Introduce cleanthat refactorer #1560
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
Merged
nedtwigg
merged 14 commits into
diffplug:main
from
blacelle:IntroduceCleanthatRefactorer
Feb 10, 2023
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cf434f2
Start with Cleanthat Java refactorer
blacelle 7ea0f3a
Add custom JDK version
blacelle 20ea6e1
Merge remote-tracking branch 'origin/main' into IntroduceCleanthatRef…
blacelle 23c97a4
Clean implementation based on CleanThat 2.0
blacelle e9efb13
Add documentation, tests, and hit a wall as mutators are not detected
blacelle 69e8524
Move to 2.1 to fix issue with class detection
blacelle bcab5b4
Improve documentation and changelog ref
blacelle a5c8b4d
Fix classLoader issue
blacelle 14122d0
Add Gradle compatibility
blacelle cfd07b0
Improve Gradle integration
blacelle 374acb6
Fix unitTests
blacelle 8fbbb71
Remove useless dependency
blacelle 640a1c2
Remove irrelevant test file
blacelle fdee03c
Fix extention of unitTest
blacelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
lib/src/cleanthat/java/com/diffplug/spotless/glue/java/JavaCleanthatRefactorerFunc.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2023 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.glue.java; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
|
||
import eu.solven.cleanthat.config.pojo.CleanthatEngineProperties; | ||
import eu.solven.cleanthat.engine.java.IJdkVersionConstants; | ||
import eu.solven.cleanthat.engine.java.refactorer.JavaRefactorer; | ||
import eu.solven.cleanthat.engine.java.refactorer.JavaRefactorerProperties; | ||
import eu.solven.cleanthat.formatter.LineEnding; | ||
|
||
public class JavaCleanthatRefactorerFunc implements FormatterFunc { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(JavaCleanthatRefactorerFunc.class); | ||
|
||
private String jdkVersion; | ||
private List<String> included; | ||
private List<String> excluded; | ||
|
||
public JavaCleanthatRefactorerFunc(String jdkVersion, List<String> included, List<String> excluded) { | ||
this.jdkVersion = jdkVersion == null ? IJdkVersionConstants.JDK_8 : jdkVersion; | ||
this.included = included == null ? Collections.emptyList() : included; | ||
this.excluded = excluded == null ? Collections.emptyList() : excluded; | ||
} | ||
|
||
public JavaCleanthatRefactorerFunc() { | ||
this(IJdkVersionConstants.JDK_8, Arrays.asList(JavaRefactorerProperties.WILDCARD), Arrays.asList()); | ||
} | ||
|
||
@Override | ||
public String apply(String input) throws Exception { | ||
CleanthatEngineProperties engineProperties = CleanthatEngineProperties.builder().engineVersion(jdkVersion).build(); | ||
|
||
JavaRefactorerProperties refactorerProperties = new JavaRefactorerProperties(); | ||
|
||
refactorerProperties.setIncluded(included); | ||
refactorerProperties.setExcluded(excluded); | ||
|
||
JavaRefactorer refactorer = new JavaRefactorer(engineProperties, refactorerProperties); | ||
|
||
LOGGER.debug("Processing sourceJdk={} included={} excluded={}", jdkVersion, included, excluded); | ||
LOGGER.debug("Available mutators: {}", JavaRefactorer.getAllIncluded()); | ||
|
||
// Spotless calls steps always with LF eol. | ||
return refactorer.doFormat(input, LineEnding.LF); | ||
} | ||
|
||
} |
158 changes: 158 additions & 0 deletions
158
lib/src/main/java/com/diffplug/spotless/java/CleanthatJavaStep.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright 2023 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.java; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.JarState; | ||
import com.diffplug.spotless.Jvm; | ||
import com.diffplug.spotless.Provisioner; | ||
|
||
/** | ||
* Enables CleanThat as a SpotLess step. | ||
* | ||
* @author Benoit Lacelle | ||
*/ | ||
// https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md#how-to-add-a-new-formatterstep | ||
public final class CleanthatJavaStep { | ||
|
||
private static final String NAME = "cleanthat"; | ||
private static final String MAVEN_COORDINATE = "io.github.solven-eu.cleanthat:java"; | ||
|
||
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(11, "2.1"); | ||
|
||
// prevent direct instantiation | ||
private CleanthatJavaStep() {} | ||
|
||
/** Creates a step which apply default CleanThat mutators. */ | ||
public static FormatterStep create(Provisioner provisioner) { | ||
return create(defaultVersion(), provisioner); | ||
} | ||
|
||
/** Creates a step which apply default CleanThat mutators. */ | ||
public static FormatterStep create(String version, Provisioner provisioner) { | ||
return create(MAVEN_COORDINATE, version, defaultJdkVersion(), defaultExcludedMutators(), defaultMutators(), provisioner); | ||
} | ||
|
||
public static String defaultJdkVersion() { | ||
// see IJdkVersionConstants.JDK_7 | ||
// https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#source | ||
// 1.7 is the default for 'maven-compiler-plugin' since 3.9.0 | ||
return "1.7"; | ||
} | ||
|
||
public static List<String> defaultExcludedMutators() { | ||
return List.of(); | ||
} | ||
|
||
/** | ||
* By default, we include all available rules | ||
* @return | ||
*/ | ||
public static List<String> defaultMutators() { | ||
// see JavaRefactorerProperties.WILDCARD | ||
return List.of("*"); | ||
} | ||
|
||
/** Creates a step which apply selected CleanThat mutators. */ | ||
public static FormatterStep create(String groupArtifact, | ||
String version, | ||
String sourceJdkVersion, | ||
List<String> excluded, | ||
List<String> included, | ||
Provisioner provisioner) { | ||
Objects.requireNonNull(groupArtifact, "groupArtifact"); | ||
if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) { | ||
throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'. it was: " + groupArtifact); | ||
} | ||
Objects.requireNonNull(version, "version"); | ||
Objects.requireNonNull(provisioner, "provisioner"); | ||
return FormatterStep.createLazy(NAME, | ||
() -> new JavaRefactorerState(NAME, groupArtifact, version, sourceJdkVersion, excluded, included, provisioner), | ||
JavaRefactorerState::createFormat); | ||
} | ||
|
||
/** Get default formatter version */ | ||
public static String defaultVersion() { | ||
return JVM_SUPPORT.getRecommendedFormatterVersion(); | ||
} | ||
|
||
public static String defaultGroupArtifact() { | ||
return MAVEN_COORDINATE; | ||
} | ||
|
||
static final class JavaRefactorerState implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
final JarState jarState; | ||
final String stepName; | ||
final String version; | ||
|
||
final String sourceJdkVersion; | ||
final List<String> included; | ||
final List<String> excluded; | ||
|
||
JavaRefactorerState(String stepName, String version, Provisioner provisioner) throws IOException { | ||
this(stepName, MAVEN_COORDINATE, version, defaultJdkVersion(), defaultExcludedMutators(), defaultMutators(), provisioner); | ||
} | ||
|
||
JavaRefactorerState(String stepName, | ||
String groupArtifact, | ||
String version, | ||
String sourceJdkVersion, | ||
List<String> included, | ||
List<String> excluded, | ||
Provisioner provisioner) throws IOException { | ||
JVM_SUPPORT.assertFormatterSupported(version); | ||
ModuleHelper.doOpenInternalPackagesIfRequired(); | ||
this.jarState = JarState.from(groupArtifact + ":" + version, provisioner); | ||
this.stepName = stepName; | ||
this.version = version; | ||
|
||
this.sourceJdkVersion = sourceJdkVersion; | ||
this.included = included; | ||
this.excluded = excluded; | ||
} | ||
|
||
@SuppressWarnings("PMD.UseProperClassLoader") | ||
FormatterFunc createFormat() { | ||
ClassLoader classLoader = jarState.getClassLoader(); | ||
|
||
Object formatter; | ||
Method formatterMethod; | ||
try { | ||
Class<?> formatterClazz = classLoader.loadClass("com.diffplug.spotless.glue.java.JavaCleanthatRefactorerFunc"); | ||
Constructor<?> formatterConstructor = formatterClazz.getConstructor(String.class, List.class, List.class); | ||
|
||
formatter = formatterConstructor.newInstance(sourceJdkVersion, included, excluded); | ||
formatterMethod = formatterClazz.getMethod("apply", String.class); | ||
} catch (ReflectiveOperationException e) { | ||
throw new IllegalStateException("Issue executing the formatter", e); | ||
} | ||
return JVM_SUPPORT.suggestLaterVersionOnError(version, input -> { | ||
return (String) formatterMethod.invoke(formatter, input); | ||
}); | ||
} | ||
|
||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
lib/src/test/java/com/diffplug/spotless/glue/java/JavaCleanthatRefactorerFuncTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2023 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.glue.java; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import eu.solven.cleanthat.engine.java.refactorer.JavaRefactorer; | ||
|
||
public class JavaCleanthatRefactorerFuncTest { | ||
@Test | ||
public void testMutatorsDetection() { | ||
Assertions.assertThat(JavaRefactorer.getAllIncluded()).isNotEmpty(); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...c/testCleanthat/java/com/diffplug/spotless/glue/java/JavaCleanthatRefactorerFuncTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2023 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.glue.ktlint.compat; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
public class KtLintCompat0Dot48Dot0AdapterTest { | ||
@Test | ||
public void testDefaults(@TempDir Path path) throws IOException { | ||
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter(); | ||
String text = loadAndWriteText(path, "empty_class_body.kt"); | ||
final Path filePath = Paths.get(path.toString(), "empty_class_body.kt"); | ||
|
||
Map<String, String> userData = new HashMap<>(); | ||
|
||
Map<String, Object> editorConfigOverrideMap = new HashMap<>(); | ||
|
||
String formatted = ktLintCompat0Dot48Dot0Adapter.format(text, filePath, false, false, null, userData, editorConfigOverrideMap); | ||
assertEquals("class empty_class_body\n", formatted); | ||
} | ||
|
||
@Test | ||
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException { | ||
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter(); | ||
String text = loadAndWriteText(path, "fails_no_semicolons.kt"); | ||
final Path filePath = Paths.get(path.toString(), "fails_no_semicolons.kt"); | ||
|
||
Map<String, String> userData = new HashMap<>(); | ||
|
||
Map<String, Object> editorConfigOverrideMap = new HashMap<>(); | ||
editorConfigOverrideMap.put("indent_style", "tab"); | ||
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled"); | ||
// ktlint_filename is an invalid rule in ktlint 0.48.0 | ||
editorConfigOverrideMap.put("ktlint_filename", "disabled"); | ||
|
||
String formatted = ktLintCompat0Dot48Dot0Adapter.format(text, filePath, false, false, null, userData, editorConfigOverrideMap); | ||
assertEquals("class fails_no_semicolons {\n\tval i = 0;\n}\n", formatted); | ||
} | ||
|
||
private static String loadAndWriteText(Path path, String name) throws IOException { | ||
try (InputStream is = KtLintCompat0Dot48Dot0AdapterTest.class.getResourceAsStream("/" + name)) { | ||
Files.copy(is, path.resolve(name)); | ||
} | ||
return new String(Files.readAllBytes(path.resolve(name)), StandardCharsets.UTF_8); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.