From b7958a92c1d86bd48710ca5170095e047adfcbca Mon Sep 17 00:00:00 2001 From: Markus Heberling Date: Sat, 25 Sep 2021 21:00:31 +0200 Subject: [PATCH] load SortPom from JarState --- lib-extra/build.gradle | 2 - lib/build.gradle | 20 ++++ .../spotless/pom/DelegatingClassLoader.java | 86 ++++++++++++++++ .../spotless/pom/SortPomFormatterFunc.java | 76 ++++++++++++++ .../diffplug/spotless}/pom/SortPomStep.java | 99 +++++++++---------- plugin-maven/build.gradle | 1 + .../diffplug/spotless/maven/pom/SortPom.java | 4 +- .../spotless/maven/pom/SortPomTest.java | 4 +- 8 files changed, 234 insertions(+), 58 deletions(-) create mode 100644 lib/src/sortPom/java/com/diffplug/spotless/pom/DelegatingClassLoader.java create mode 100644 lib/src/sortPom/java/com/diffplug/spotless/pom/SortPomFormatterFunc.java rename {lib-extra/src/main/java/com/diffplug/spotless/extra => lib/src/sortPom/java/com/diffplug/spotless}/pom/SortPomStep.java (53%) diff --git a/lib-extra/build.gradle b/lib-extra/build.gradle index bd3bded2b4..1f6fdd8962 100644 --- a/lib-extra/build.gradle +++ b/lib-extra/build.gradle @@ -16,8 +16,6 @@ dependencies { implementation "com.googlecode.concurrent-trees:concurrent-trees:2.6.1" // used for xml parsing in EclipseFormatter implementation "org.codehaus.groovy:groovy-xml:3.0.9" - // used for pom sorting - implementation 'com.github.ekryd.sortpom:sortpom-sorter:3.0.0' // testing testImplementation project(':testlib') diff --git a/lib/build.gradle b/lib/build.gradle index d8685a5acd..88745a42ac 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -6,15 +6,35 @@ version = rootProject.spotlessChangelog.versionNext apply from: rootProject.file('gradle/java-setup.gradle') apply from: rootProject.file('gradle/java-publish.gradle') +sourceSets { + sortPom { + compileClasspath += sourceSets.main.output + runtimeClasspath += sourceSets.main.output + + java { + } + } +} + dependencies { // zero runtime reqs is a hard requirements for spotless-lib // if you need a dep, put it in lib-extra testImplementation "org.junit.jupiter:junit-jupiter:${VER_JUNIT}" testImplementation "org.assertj:assertj-core:${VER_ASSERTJ}" testImplementation "com.diffplug.durian:durian-testlib:${VER_DURIAN}" + + // used for pom sorting + sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.0.0' } // we'll hold the core lib to a high standard spotbugs { reportLevel = 'low' } // low|medium|high (low = sensitive to even minor mistakes) test { useJUnitPlatform() } + +jar { + // Add directories with generated sources (might be several - one for every supported language). + from sourceSets.sortPom.output.classesDirs + // Add output directories of the sourceset's resources. + from sourceSets.sortPom.output.resourcesDir +} diff --git a/lib/src/sortPom/java/com/diffplug/spotless/pom/DelegatingClassLoader.java b/lib/src/sortPom/java/com/diffplug/spotless/pom/DelegatingClassLoader.java new file mode 100644 index 0000000000..416fce01ed --- /dev/null +++ b/lib/src/sortPom/java/com/diffplug/spotless/pom/DelegatingClassLoader.java @@ -0,0 +1,86 @@ +/* + * Copyright 2021 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.pom; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.ByteBuffer; +import java.util.Enumeration; +import java.util.Vector; + +public class DelegatingClassLoader extends ClassLoader { + + private final ClassLoader[] delegateClassLoaders; + + public DelegatingClassLoader(ClassLoader... delegateClassLoaders) { + super(null); + this.delegateClassLoaders = delegateClassLoaders; + } + + protected Class findClass(String name) throws ClassNotFoundException { + String path = name.replace('.', '/') + ".class"; + URL url = findResource(path); + if (url == null) { + throw new ClassNotFoundException(name); + } + try { + ByteBuffer byteCode = loadResource(url); + return defineClass(name, byteCode, null); + } catch (IOException e) { + throw new ClassNotFoundException(name, e); + } + } + + private ByteBuffer loadResource(URL url) throws IOException { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + + int nRead; + byte[] data = new byte[1024]; + + InputStream inputStream = url.openStream(); + while ((nRead = inputStream.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead); + } + + buffer.flush(); + + return ByteBuffer.wrap(buffer.toByteArray()); + } + + protected URL findResource(String name) { + for (ClassLoader delegate : delegateClassLoaders) { + URL resource = delegate.getResource(name); + if (resource != null) { + return resource; + } + } + return null; + } + + protected Enumeration findResources(String name) throws IOException { + Vector vector = new Vector<>(); + for (ClassLoader delegate : delegateClassLoaders) { + Enumeration enumeration = delegate.getResources(name); + while (enumeration.hasMoreElements()) { + vector.add(enumeration.nextElement()); + } + } + return vector.elements(); + } + +} diff --git a/lib/src/sortPom/java/com/diffplug/spotless/pom/SortPomFormatterFunc.java b/lib/src/sortPom/java/com/diffplug/spotless/pom/SortPomFormatterFunc.java new file mode 100644 index 0000000000..e056f4ed36 --- /dev/null +++ b/lib/src/sortPom/java/com/diffplug/spotless/pom/SortPomFormatterFunc.java @@ -0,0 +1,76 @@ +/* + * Copyright 2021 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.pom; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.util.logging.Logger; + +import org.apache.commons.io.IOUtils; + +import com.diffplug.spotless.FormatterFunc; + +import sortpom.SortPomImpl; +import sortpom.logger.SortPomLogger; +import sortpom.parameter.PluginParameters; + +class SortPomFormatterFunc implements FormatterFunc { + private static final Logger logger = Logger.getLogger(SortPomStep.class.getName()); + private final SortPomStep.InternalState state; + + public SortPomFormatterFunc(SortPomStep.InternalState state) { + this.state = state; + } + + @Override + public String apply(String input) throws Exception { + // SortPom expects a file to sort, so we write the inpout into a temporary file + File pom = File.createTempFile("pom", ".xml"); + pom.deleteOnExit(); + try (FileWriter fw = new FileWriter(pom)) { + fw.write(input); + } + SortPomImpl sortPom = new SortPomImpl(); + sortPom.setup(new SortPomLogger() { + @Override + public void warn(String content) { + logger.warning(content); + } + + @Override + public void info(String content) { + logger.info(content); + } + + @Override + public void error(String content) { + logger.severe(content); + } + }, PluginParameters.builder() + .setPomFile(pom) + .setFileOutput(false, null, null, false) + .setEncoding(state.encoding) + .setFormatting(state.lineSeparator, state.expandEmptyElements, state.spaceBeforeCloseEmptyElement, state.keepBlankLines) + .setIndent(state.nrOfIndentSpace, state.indentBlankLines, state.indentSchemaLocation) + .setSortOrder(state.sortOrderFile, state.predefinedSortOrder) + .setSortEntities(state.sortDependencies, state.sortDependencyExclusions, state.sortPlugins, state.sortProperties, state.sortModules, state.sortExecutions) + .setTriggers(false) + .build()); + sortPom.sortPom(); + return IOUtils.toString(new FileReader(pom)); + } +} diff --git a/lib-extra/src/main/java/com/diffplug/spotless/extra/pom/SortPomStep.java b/lib/src/sortPom/java/com/diffplug/spotless/pom/SortPomStep.java similarity index 53% rename from lib-extra/src/main/java/com/diffplug/spotless/extra/pom/SortPomStep.java rename to lib/src/sortPom/java/com/diffplug/spotless/pom/SortPomStep.java index d70605acf7..9a5cf081c5 100644 --- a/lib-extra/src/main/java/com/diffplug/spotless/extra/pom/SortPomStep.java +++ b/lib/src/sortPom/java/com/diffplug/spotless/pom/SortPomStep.java @@ -13,36 +13,37 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.diffplug.spotless.extra.pom; - -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; +package com.diffplug.spotless.pom; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.ObjectStreamClass; import java.io.Serializable; -import java.util.logging.Logger; - -import org.apache.commons.io.IOUtils; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import com.diffplug.spotless.FormatterFunc; import com.diffplug.spotless.FormatterStep; - -import sortpom.SortPomImpl; -import sortpom.logger.SortPomLogger; -import sortpom.parameter.PluginParameters; +import com.diffplug.spotless.JarState; +import com.diffplug.spotless.Provisioner; public class SortPomStep { public static final String NAME = "sortPom"; - private static final Logger logger = Logger.getLogger(SortPomStep.class.getName()); private SortPomStep() {} - public static FormatterStep create(String encoding, String lineSeparator, boolean expandEmptyElements, boolean spaceBeforeCloseEmptyElement, boolean keepBlankLines, int nrOfIndentSpace, boolean indentBlankLines, boolean indentSchemaLocation, String predefinedSortOrder, String sortOrderFile, String sortDependencies, String sortDependencyExclusions, String sortPlugins, boolean sortProperties, boolean sortModules, boolean sortExecutions) { - return FormatterStep.createLazy(NAME, () -> new State(encoding, lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement, keepBlankLines, nrOfIndentSpace, indentBlankLines, indentSchemaLocation, predefinedSortOrder, sortOrderFile, sortDependencies, sortDependencyExclusions, sortPlugins, sortProperties, sortModules, sortExecutions), State::createFormat); + public static FormatterStep create(String encoding, String lineSeparator, boolean expandEmptyElements, boolean spaceBeforeCloseEmptyElement, boolean keepBlankLines, int nrOfIndentSpace, boolean indentBlankLines, boolean indentSchemaLocation, String predefinedSortOrder, String sortOrderFile, String sortDependencies, String sortDependencyExclusions, String sortPlugins, boolean sortProperties, boolean sortModules, boolean sortExecutions, Provisioner provisioner) { + return FormatterStep.createLazy(NAME, () -> new State(encoding, lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement, keepBlankLines, nrOfIndentSpace, indentBlankLines, indentSchemaLocation, predefinedSortOrder, sortOrderFile, sortDependencies, sortDependencyExclusions, sortPlugins, sortProperties, sortModules, sortExecutions, provisioner), State::createFormat); } - static final class State implements Serializable { + static final class InternalState implements Serializable { private static final long serialVersionUID = 1L; + final String encoding; final String lineSeparator; @@ -75,7 +76,7 @@ static final class State implements Serializable { final boolean sortExecutions; - State(String encoding, String lineSeparator, boolean expandEmptyElements, boolean spaceBeforeCloseEmptyElement, boolean keepBlankLines, int nrOfIndentSpace, boolean indentBlankLines, boolean indentSchemaLocation, String predefinedSortOrder, String sortOrderFile, String sortDependencies, String sortDependencyExclusions, String sortPlugins, boolean sortProperties, boolean sortModules, boolean sortExecutions) { + InternalState(String encoding, String lineSeparator, boolean expandEmptyElements, boolean spaceBeforeCloseEmptyElement, boolean keepBlankLines, int nrOfIndentSpace, boolean indentBlankLines, boolean indentSchemaLocation, String predefinedSortOrder, String sortOrderFile, String sortDependencies, String sortDependencyExclusions, String sortPlugins, boolean sortProperties, boolean sortModules, boolean sortExecutions) { this.encoding = encoding; this.lineSeparator = lineSeparator; this.expandEmptyElements = expandEmptyElements; @@ -93,44 +94,38 @@ static final class State implements Serializable { this.sortModules = sortModules; this.sortExecutions = sortExecutions; } + } - FormatterFunc createFormat() { - return input -> { - // SortPom expects a file to sort, so we write the inpout into a temporary file - File pom = File.createTempFile("pom", ".xml"); - pom.deleteOnExit(); - try (FileWriter fw = new FileWriter(pom)) { - fw.write(input); + static final class State implements Serializable { + private static final long serialVersionUID = 1L; + final JarState jarState; + + final InternalState internalState; + + State(String encoding, String lineSeparator, boolean expandEmptyElements, boolean spaceBeforeCloseEmptyElement, boolean keepBlankLines, int nrOfIndentSpace, boolean indentBlankLines, boolean indentSchemaLocation, String predefinedSortOrder, String sortOrderFile, String sortDependencies, String sortDependencyExclusions, String sortPlugins, boolean sortProperties, boolean sortModules, boolean sortExecutions, Provisioner provisioner) throws IOException { + this.jarState = JarState.from("com.github.ekryd.sortpom:sortpom-sorter:3.0.0", provisioner); + this.internalState = new InternalState(encoding, lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement, keepBlankLines, nrOfIndentSpace, indentBlankLines, indentSchemaLocation, predefinedSortOrder, sortOrderFile, sortDependencies, sortDependencyExclusions, sortPlugins, sortProperties, sortModules, sortExecutions); + } + + FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, IOException { + ClassLoader classLoader = new DelegatingClassLoader(this.getClass().getClassLoader(), jarState.getClassLoader()); + Constructor constructor = classLoader.loadClass(SortPomFormatterFunc.class.getName()).getConstructor(classLoader.loadClass(InternalState.class.getName())); + constructor.setAccessible(true); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(out); + oos.writeObject(internalState); + ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())) { + @Override + protected Class resolveClass(ObjectStreamClass desc) throws ClassNotFoundException { + return classLoader.loadClass(desc.getName()); } - SortPomImpl sortPom = new SortPomImpl(); - sortPom.setup(new SortPomLogger() { - @Override - public void warn(String content) { - logger.warning(content); - } - - @Override - public void info(String content) { - logger.info(content); - } - - @Override - public void error(String content) { - logger.severe(content); - } - }, PluginParameters.builder() - .setPomFile(pom) - .setFileOutput(false, null, null, false) - .setEncoding(encoding) - .setFormatting(lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement, keepBlankLines) - .setIndent(nrOfIndentSpace, indentBlankLines, indentSchemaLocation) - .setSortOrder(sortOrderFile, predefinedSortOrder) - .setSortEntities(sortDependencies, sortDependencyExclusions, sortPlugins, sortProperties, sortModules, sortExecutions) - .setTriggers(false) - .build()); - sortPom.sortPom(); - return IOUtils.toString(new FileReader(pom)); }; + Object state = ois.readObject(); + Object formatterFunc = constructor.newInstance(state); + Method apply = formatterFunc.getClass().getMethod("apply", String.class); + apply.setAccessible(true); + return input -> (String) apply.invoke(formatterFunc, input); } + } } diff --git a/plugin-maven/build.gradle b/plugin-maven/build.gradle index f5257e065c..44b7d7222d 100644 --- a/plugin-maven/build.gradle +++ b/plugin-maven/build.gradle @@ -63,6 +63,7 @@ String libVersion = version.endsWith('-SNAPSHOT') ? dependencies { if (version.endsWith('-SNAPSHOT') || (rootProject.spotlessChangelog.versionNext == rootProject.spotlessChangelog.versionLast)) { implementation project(':lib') + implementation project(':lib').sourceSets.sortPom.output implementation project(':lib-extra') } else { implementation "com.diffplug.spotless:spotless-lib:${libVersion}" diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java index 3fbe06f3dd..ddd3fb6fbc 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java @@ -18,9 +18,9 @@ import org.apache.maven.plugins.annotations.Parameter; import com.diffplug.spotless.FormatterStep; -import com.diffplug.spotless.extra.pom.SortPomStep; import com.diffplug.spotless.maven.FormatterStepConfig; import com.diffplug.spotless.maven.FormatterStepFactory; +import com.diffplug.spotless.pom.SortPomStep; public class SortPom implements FormatterStepFactory { @Parameter @@ -73,6 +73,6 @@ public class SortPom implements FormatterStepFactory { @Override public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { - return SortPomStep.create(encoding, lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement, keepBlankLines, nrOfIndentSpace, indentBlankLines, indentSchemaLocation, predefinedSortOrder, sortOrderFile, sortDependencies, sortDependencyExclusions, sortPlugins, sortProperties, sortModules, sortExecutions); + return SortPomStep.create(encoding, lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement, keepBlankLines, nrOfIndentSpace, indentBlankLines, indentSchemaLocation, predefinedSortOrder, sortOrderFile, sortDependencies, sortDependencyExclusions, sortPlugins, sortProperties, sortModules, sortExecutions, stepConfig.getProvisioner()); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/pom/SortPomTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/pom/SortPomTest.java index 8df667ce44..4b26127cc0 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/pom/SortPomTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/pom/SortPomTest.java @@ -15,10 +15,10 @@ */ package com.diffplug.spotless.maven.pom; -import com.diffplug.spotless.maven.MavenIntegrationHarness; - import org.junit.jupiter.api.Test; +import com.diffplug.spotless.maven.MavenIntegrationHarness; + public class SortPomTest extends MavenIntegrationHarness { @Test public void testSortPomWithDefaultConfig() throws Exception {