Skip to content

Commit

Permalink
load SortPom from JarState
Browse files Browse the repository at this point in the history
  • Loading branch information
tisoft committed Sep 26, 2021
1 parent d4a1f87 commit b7958a9
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 58 deletions.
2 changes: 0 additions & 2 deletions lib-extra/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
20 changes: 20 additions & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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<URL> findResources(String name) throws IOException {
Vector<URL> vector = new Vector<>();
for (ClassLoader delegate : delegateClassLoaders) {
Enumeration<URL> enumeration = delegate.getResources(name);
while (enumeration.hasMoreElements()) {
vector.add(enumeration.nextElement());
}
}
return vector.elements();
}

}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

}
}
1 change: 1 addition & 0 deletions plugin-maven/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b7958a9

Please sign in to comment.