|
| 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.glue.java; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +import com.diffplug.spotless.FormatterFunc; |
| 27 | + |
| 28 | +import eu.solven.cleanthat.config.pojo.CleanthatEngineProperties; |
| 29 | +import eu.solven.cleanthat.config.pojo.SourceCodeProperties; |
| 30 | +import eu.solven.cleanthat.engine.java.IJdkVersionConstants; |
| 31 | +import eu.solven.cleanthat.engine.java.refactorer.JavaRefactorer; |
| 32 | +import eu.solven.cleanthat.engine.java.refactorer.JavaRefactorerProperties; |
| 33 | +import eu.solven.cleanthat.formatter.LineEnding; |
| 34 | + |
| 35 | +/** |
| 36 | + * The glue for CleanThat: it is build over the version in build.gradle, but at runtime it will be executed over |
| 37 | + * the version loaded in JarState, which is by default defined in com.diffplug.spotless.java.CleanthatJavaStep#JVM_SUPPORT |
| 38 | + */ |
| 39 | +public class JavaCleanthatRefactorerFunc implements FormatterFunc { |
| 40 | + private static final Logger LOGGER = LoggerFactory.getLogger(JavaCleanthatRefactorerFunc.class); |
| 41 | + |
| 42 | + private String jdkVersion; |
| 43 | + private List<String> included; |
| 44 | + private List<String> excluded; |
| 45 | + |
| 46 | + public JavaCleanthatRefactorerFunc(String jdkVersion, List<String> included, List<String> excluded) { |
| 47 | + this.jdkVersion = jdkVersion == null ? IJdkVersionConstants.JDK_8 : jdkVersion; |
| 48 | + this.included = included == null ? Collections.emptyList() : included; |
| 49 | + this.excluded = excluded == null ? Collections.emptyList() : excluded; |
| 50 | + } |
| 51 | + |
| 52 | + public JavaCleanthatRefactorerFunc() { |
| 53 | + this(IJdkVersionConstants.JDK_8, Arrays.asList(JavaRefactorerProperties.WILDCARD), Arrays.asList()); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String apply(String input) throws Exception { |
| 58 | + // https://stackoverflow.com/questions/1771679/difference-between-threads-context-class-loader-and-normal-classloader |
| 59 | + ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); |
| 60 | + try { |
| 61 | + // Ensure CleanThat main Thread has its custom classLoader while executing its refactoring |
| 62 | + Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); |
| 63 | + return doApply(input); |
| 64 | + } finally { |
| 65 | + // Restore the originalClassLoader |
| 66 | + Thread.currentThread().setContextClassLoader(originalClassLoader); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private String doApply(String input) throws InterruptedException, IOException { |
| 71 | + // call some API that uses reflection without taking ClassLoader param |
| 72 | + CleanthatEngineProperties engineProperties = CleanthatEngineProperties.builder().engineVersion(jdkVersion).build(); |
| 73 | + |
| 74 | + // Spotless will push us LF content |
| 75 | + engineProperties.setSourceCode(SourceCodeProperties.builder().lineEnding(LineEnding.LF).build()); |
| 76 | + |
| 77 | + JavaRefactorerProperties refactorerProperties = new JavaRefactorerProperties(); |
| 78 | + |
| 79 | + refactorerProperties.setIncluded(included); |
| 80 | + refactorerProperties.setExcluded(excluded); |
| 81 | + |
| 82 | + JavaRefactorer refactorer = new JavaRefactorer(engineProperties, refactorerProperties); |
| 83 | + |
| 84 | + LOGGER.debug("Processing sourceJdk={} included={} excluded={}", jdkVersion, included, excluded); |
| 85 | + LOGGER.debug("Available mutators: {}", JavaRefactorer.getAllIncluded()); |
| 86 | + |
| 87 | + // Spotless calls steps always with LF eol. |
| 88 | + return refactorer.doFormat(input, LineEnding.LF); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments