|
| 1 | +/* |
| 2 | + * Copyright 2023 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless.java; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.Serializable; |
| 20 | +import java.lang.reflect.Constructor; |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Objects; |
| 24 | + |
| 25 | +import com.diffplug.spotless.FormatterFunc; |
| 26 | +import com.diffplug.spotless.FormatterStep; |
| 27 | +import com.diffplug.spotless.JarState; |
| 28 | +import com.diffplug.spotless.Jvm; |
| 29 | +import com.diffplug.spotless.Provisioner; |
| 30 | + |
| 31 | +/** |
| 32 | + * Enables CleanThat as a SpotLess step. |
| 33 | + * |
| 34 | + * @author Benoit Lacelle |
| 35 | + */ |
| 36 | +// https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md#how-to-add-a-new-formatterstep |
| 37 | +public final class CleanthatJavaStep { |
| 38 | + |
| 39 | + private static final String NAME = "cleanthat"; |
| 40 | + private static final String MAVEN_COORDINATE = "io.github.solven-eu.cleanthat:java"; |
| 41 | + |
| 42 | + // CleanThat changelog is available at https://github.com/solven-eu/cleanthat/blob/master/CHANGES.MD |
| 43 | + private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(11, "2.1"); |
| 44 | + |
| 45 | + // prevent direct instantiation |
| 46 | + private CleanthatJavaStep() {} |
| 47 | + |
| 48 | + /** Creates a step which apply default CleanThat mutators. */ |
| 49 | + public static FormatterStep create(Provisioner provisioner) { |
| 50 | + return create(defaultVersion(), provisioner); |
| 51 | + } |
| 52 | + |
| 53 | + /** Creates a step which apply default CleanThat mutators. */ |
| 54 | + public static FormatterStep create(String version, Provisioner provisioner) { |
| 55 | + return create(MAVEN_COORDINATE, version, defaultSourceJdk(), defaultExcludedMutators(), defaultMutators(), provisioner); |
| 56 | + } |
| 57 | + |
| 58 | + public static String defaultSourceJdk() { |
| 59 | + // see IJdkVersionConstants.JDK_7 |
| 60 | + // https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#source |
| 61 | + // 1.7 is the default for 'maven-compiler-plugin' since 3.9.0 |
| 62 | + return "1.7"; |
| 63 | + } |
| 64 | + |
| 65 | + public static List<String> defaultExcludedMutators() { |
| 66 | + return List.of(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * By default, we include all available rules |
| 71 | + * @return |
| 72 | + */ |
| 73 | + public static List<String> defaultMutators() { |
| 74 | + // see JavaRefactorerProperties.WILDCARD |
| 75 | + return List.of("*"); |
| 76 | + } |
| 77 | + |
| 78 | + /** Creates a step which apply selected CleanThat mutators. */ |
| 79 | + public static FormatterStep create(String groupArtifact, |
| 80 | + String version, |
| 81 | + String sourceJdkVersion, |
| 82 | + List<String> excluded, |
| 83 | + List<String> included, |
| 84 | + Provisioner provisioner) { |
| 85 | + Objects.requireNonNull(groupArtifact, "groupArtifact"); |
| 86 | + if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) { |
| 87 | + throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'. it was: " + groupArtifact); |
| 88 | + } |
| 89 | + Objects.requireNonNull(version, "version"); |
| 90 | + Objects.requireNonNull(provisioner, "provisioner"); |
| 91 | + return FormatterStep.createLazy(NAME, |
| 92 | + () -> new JavaRefactorerState(NAME, groupArtifact, version, sourceJdkVersion, excluded, included, provisioner), |
| 93 | + JavaRefactorerState::createFormat); |
| 94 | + } |
| 95 | + |
| 96 | + /** Get default formatter version */ |
| 97 | + public static String defaultVersion() { |
| 98 | + return JVM_SUPPORT.getRecommendedFormatterVersion(); |
| 99 | + } |
| 100 | + |
| 101 | + public static String defaultGroupArtifact() { |
| 102 | + return MAVEN_COORDINATE; |
| 103 | + } |
| 104 | + |
| 105 | + static final class JavaRefactorerState implements Serializable { |
| 106 | + private static final long serialVersionUID = 1L; |
| 107 | + |
| 108 | + final JarState jarState; |
| 109 | + final String stepName; |
| 110 | + final String version; |
| 111 | + |
| 112 | + final String sourceJdkVersion; |
| 113 | + final List<String> included; |
| 114 | + final List<String> excluded; |
| 115 | + |
| 116 | + JavaRefactorerState(String stepName, String version, Provisioner provisioner) throws IOException { |
| 117 | + this(stepName, MAVEN_COORDINATE, version, defaultSourceJdk(), defaultExcludedMutators(), defaultMutators(), provisioner); |
| 118 | + } |
| 119 | + |
| 120 | + JavaRefactorerState(String stepName, |
| 121 | + String groupArtifact, |
| 122 | + String version, |
| 123 | + String sourceJdkVersion, |
| 124 | + List<String> included, |
| 125 | + List<String> excluded, |
| 126 | + Provisioner provisioner) throws IOException { |
| 127 | + JVM_SUPPORT.assertFormatterSupported(version); |
| 128 | + ModuleHelper.doOpenInternalPackagesIfRequired(); |
| 129 | + this.jarState = JarState.from(groupArtifact + ":" + version, provisioner); |
| 130 | + this.stepName = stepName; |
| 131 | + this.version = version; |
| 132 | + |
| 133 | + this.sourceJdkVersion = sourceJdkVersion; |
| 134 | + this.included = included; |
| 135 | + this.excluded = excluded; |
| 136 | + } |
| 137 | + |
| 138 | + @SuppressWarnings("PMD.UseProperClassLoader") |
| 139 | + FormatterFunc createFormat() { |
| 140 | + ClassLoader classLoader = jarState.getClassLoader(); |
| 141 | + |
| 142 | + Object formatter; |
| 143 | + Method formatterMethod; |
| 144 | + try { |
| 145 | + Class<?> formatterClazz = classLoader.loadClass("com.diffplug.spotless.glue.java.JavaCleanthatRefactorerFunc"); |
| 146 | + Constructor<?> formatterConstructor = formatterClazz.getConstructor(String.class, List.class, List.class); |
| 147 | + |
| 148 | + formatter = formatterConstructor.newInstance(sourceJdkVersion, included, excluded); |
| 149 | + formatterMethod = formatterClazz.getMethod("apply", String.class); |
| 150 | + } catch (ReflectiveOperationException e) { |
| 151 | + throw new IllegalStateException("Issue executing the formatter", e); |
| 152 | + } |
| 153 | + return JVM_SUPPORT.suggestLaterVersionOnError(version, input -> { |
| 154 | + return (String) formatterMethod.invoke(formatter, input); |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | +} |
0 commit comments