|
| 1 | +/* |
| 2 | + * Copyright 2021 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.kotlin; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.Serializable; |
| 20 | +import java.lang.reflect.Constructor; |
| 21 | +import java.lang.reflect.InvocationTargetException; |
| 22 | +import java.lang.reflect.Method; |
| 23 | +import java.lang.reflect.Proxy; |
| 24 | +import java.util.*; |
| 25 | + |
| 26 | +import javax.annotation.Nullable; |
| 27 | + |
| 28 | +import com.diffplug.spotless.*; |
| 29 | + |
| 30 | +/** Wraps up [diktat](https://github.com/cqfn/diKTat) as a FormatterStep. */ |
| 31 | +public class DiktatStep { |
| 32 | + |
| 33 | + // prevent direct instantiation |
| 34 | + private DiktatStep() {} |
| 35 | + |
| 36 | + private static final String DEFAULT_VERSION = "0.4.0"; |
| 37 | + static final String NAME = "diktat"; |
| 38 | + static final String PACKAGE_DIKTAT = "org.cqfn.diktat"; |
| 39 | + static final String PACKAGE_KTLINT = "com.pinterest.ktlint"; |
| 40 | + static final String MAVEN_COORDINATE = PACKAGE_DIKTAT + ":diktat-rules:"; |
| 41 | + |
| 42 | + public static String defaultVersionDiktat() { |
| 43 | + return DEFAULT_VERSION; |
| 44 | + } |
| 45 | + |
| 46 | + public static FormatterStep create(Provisioner provisioner) { |
| 47 | + return create(defaultVersionDiktat(), provisioner); |
| 48 | + } |
| 49 | + |
| 50 | + public static FormatterStep create(String versionDiktat, Provisioner provisioner) { |
| 51 | + return create(versionDiktat, provisioner, Collections.emptyMap(), null); |
| 52 | + } |
| 53 | + |
| 54 | + public static FormatterStep create(String versionDiktat, Provisioner provisioner, @Nullable FileSignature config) { |
| 55 | + return create(versionDiktat, provisioner, Collections.emptyMap(), config); |
| 56 | + } |
| 57 | + |
| 58 | + public static FormatterStep create(String versionDiktat, Provisioner provisioner, Map<String, String> userData, @Nullable FileSignature config) { |
| 59 | + return create(versionDiktat, provisioner, false, userData, config); |
| 60 | + } |
| 61 | + |
| 62 | + public static FormatterStep createForScript(String versionDiktat, Provisioner provisioner, @Nullable FileSignature config) { |
| 63 | + return createForScript(versionDiktat, provisioner, Collections.emptyMap(), config); |
| 64 | + } |
| 65 | + |
| 66 | + public static FormatterStep createForScript(String versionDiktat, Provisioner provisioner, Map<String, String> userData, @Nullable FileSignature config) { |
| 67 | + return create(versionDiktat, provisioner, true, userData, config); |
| 68 | + } |
| 69 | + |
| 70 | + public static FormatterStep create(String versionDiktat, Provisioner provisioner, boolean isScript, Map<String, String> userData, @Nullable FileSignature config) { |
| 71 | + Objects.requireNonNull(versionDiktat, "versionDiktat"); |
| 72 | + Objects.requireNonNull(provisioner, "provisioner"); |
| 73 | + return FormatterStep.createLazy(NAME, |
| 74 | + () -> new DiktatStep.State(versionDiktat, provisioner, isScript, userData, config), |
| 75 | + DiktatStep.State::createFormat); |
| 76 | + } |
| 77 | + |
| 78 | + static final class State implements Serializable { |
| 79 | + |
| 80 | + private static final long serialVersionUID = 1L; |
| 81 | + |
| 82 | + /** Are the files being linted Kotlin script files. */ |
| 83 | + private final boolean isScript; |
| 84 | + private final @Nullable FileSignature config; |
| 85 | + private final String pkg; |
| 86 | + private final String pkgKtlint; |
| 87 | + final JarState jar; |
| 88 | + private final TreeMap<String, String> userData; |
| 89 | + |
| 90 | + State(String versionDiktat, Provisioner provisioner, boolean isScript, Map<String, String> userData, @Nullable FileSignature config) throws IOException { |
| 91 | + |
| 92 | + HashSet<String> pkgSet = new HashSet<>(); |
| 93 | + pkgSet.add(MAVEN_COORDINATE + versionDiktat); |
| 94 | + |
| 95 | + this.userData = new TreeMap<>(userData); |
| 96 | + this.pkg = PACKAGE_DIKTAT; |
| 97 | + this.pkgKtlint = PACKAGE_KTLINT; |
| 98 | + this.jar = JarState.from(pkgSet, provisioner); |
| 99 | + this.isScript = isScript; |
| 100 | + this.config = config; |
| 101 | + } |
| 102 | + |
| 103 | + FormatterFunc createFormat() throws Exception { |
| 104 | + |
| 105 | + ClassLoader classLoader = jar.getClassLoader(); |
| 106 | + |
| 107 | + // first, we get the diktat rules |
| 108 | + if (config != null) { |
| 109 | + System.setProperty("diktat.config.path", config.getOnlyFile().getAbsolutePath()); |
| 110 | + } |
| 111 | + |
| 112 | + Class<?> ruleSetProviderClass = classLoader.loadClass(pkg + ".ruleset.rules.DiktatRuleSetProvider"); |
| 113 | + Object diktatRuleSet = ruleSetProviderClass.getMethod("get").invoke(ruleSetProviderClass.newInstance()); |
| 114 | + Iterable<?> ruleSets = Collections.singletonList(diktatRuleSet); |
| 115 | + |
| 116 | + // next, we create an error callback which throws an assertion error when the format is bad |
| 117 | + Class<?> function2Interface = classLoader.loadClass("kotlin.jvm.functions.Function2"); |
| 118 | + Class<?> lintErrorClass = classLoader.loadClass(pkgKtlint + ".core.LintError"); |
| 119 | + Method detailGetter = lintErrorClass.getMethod("getDetail"); |
| 120 | + Method lineGetter = lintErrorClass.getMethod("getLine"); |
| 121 | + Method colGetter = lintErrorClass.getMethod("getCol"); |
| 122 | + |
| 123 | + // grab the KtLint singleton |
| 124 | + Class<?> ktlintClass = classLoader.loadClass(pkgKtlint + ".core.KtLint"); |
| 125 | + Object ktlint = ktlintClass.getDeclaredField("INSTANCE").get(null); |
| 126 | + |
| 127 | + Class<?> paramsClass = classLoader.loadClass(pkgKtlint + ".core.KtLint$Params"); |
| 128 | + // and its constructor |
| 129 | + Constructor<?> constructor = paramsClass.getConstructor( |
| 130 | + /* fileName, nullable */ String.class, |
| 131 | + /* text */ String.class, |
| 132 | + /* ruleSets */ Iterable.class, |
| 133 | + /* userData */ Map.class, |
| 134 | + /* callback */ function2Interface, |
| 135 | + /* script */ boolean.class, |
| 136 | + /* editorConfigPath, nullable */ String.class, |
| 137 | + /* debug */ boolean.class); |
| 138 | + Method formatterMethod = ktlintClass.getMethod("format", paramsClass); |
| 139 | + FormatterFunc.NeedsFile formatterFunc = (input, file) -> { |
| 140 | + ArrayList<Object> errors = new ArrayList<>(); |
| 141 | + |
| 142 | + Object formatterCallback = Proxy.newProxyInstance(classLoader, new Class[]{function2Interface}, |
| 143 | + (proxy, method, args) -> { |
| 144 | + Object lintError = args[0]; //ktlint.core.LintError |
| 145 | + boolean corrected = (Boolean) args[1]; |
| 146 | + if (!corrected) { |
| 147 | + errors.add(lintError); |
| 148 | + } |
| 149 | + return null; |
| 150 | + }); |
| 151 | + |
| 152 | + userData.put("file_path", file.getAbsolutePath()); |
| 153 | + try { |
| 154 | + Object params = constructor.newInstance( |
| 155 | + /* fileName, nullable */ file.getName(), |
| 156 | + /* text */ input, |
| 157 | + /* ruleSets */ ruleSets, |
| 158 | + /* userData */ userData, |
| 159 | + /* callback */ formatterCallback, |
| 160 | + /* script */ isScript, |
| 161 | + /* editorConfigPath, nullable */ null, |
| 162 | + /* debug */ false); |
| 163 | + String result = (String) formatterMethod.invoke(ktlint, params); |
| 164 | + if (!errors.isEmpty()) { |
| 165 | + StringBuilder error = new StringBuilder(""); |
| 166 | + error.append("There are ").append(errors.size()).append(" unfixed errors:"); |
| 167 | + for (Object er : errors) { |
| 168 | + String detail = (String) detailGetter.invoke(er); |
| 169 | + int line = (Integer) lineGetter.invoke(er); |
| 170 | + int col = (Integer) colGetter.invoke(er); |
| 171 | + |
| 172 | + error.append(System.lineSeparator()).append("Error on line: ").append(line).append(", column: ").append(col).append(" cannot be fixed automatically") |
| 173 | + .append(System.lineSeparator()).append(detail); |
| 174 | + } |
| 175 | + throw new AssertionError(error); |
| 176 | + } |
| 177 | + return result; |
| 178 | + } catch (InvocationTargetException e) { |
| 179 | + throw ThrowingEx.unwrapCause(e); |
| 180 | + } |
| 181 | + }; |
| 182 | + |
| 183 | + return formatterFunc; |
| 184 | + } |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | +} |
0 commit comments