Skip to content

Commit 14ee011

Browse files
committed
format
1 parent b22690a commit 14ee011

File tree

5 files changed

+130
-86
lines changed

5 files changed

+130
-86
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
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+
*/
116
package com.diffplug.spotless.pom;
217

318
import java.io.IOException;
419
import java.io.InputStream;
520
import java.net.URL;
621
import java.nio.Buffer;
722
import java.nio.ByteBuffer;
8-
import java.security.SecureClassLoader;
923
import java.util.Enumeration;
1024
import java.util.Vector;
1125

@@ -16,66 +30,83 @@
1630
*/
1731
public class JoinClassLoader extends ClassLoader {
1832

19-
private ClassLoader[] delegateClassLoaders;
33+
private ClassLoader[] delegateClassLoaders;
2034

21-
public JoinClassLoader (ClassLoader parent, ClassLoader... delegateClassLoaders) {
22-
super(parent);
23-
this.delegateClassLoaders = delegateClassLoaders; }
35+
public JoinClassLoader(ClassLoader parent, ClassLoader... delegateClassLoaders) {
36+
super(parent);
37+
this.delegateClassLoaders = delegateClassLoaders;
38+
}
2439

25-
protected Class<?> findClass (String name) throws ClassNotFoundException {
26-
// It would be easier to call the loadClass() methods of the delegateClassLoaders
27-
// here, but we have to load the class from the byte code ourselves, because we
28-
// need it to be associated with our class loader.
29-
String path = name.replace('.', '/') + ".class";
30-
URL url = findResource(path);
31-
if (url == null) {
32-
throw new ClassNotFoundException(name); }
33-
ByteBuffer byteCode;
34-
try {
35-
byteCode = loadResource(url); }
36-
catch (IOException e) {
37-
throw new ClassNotFoundException(name, e); }
38-
return defineClass(name, byteCode, null); }
40+
protected Class<?> findClass(String name) throws ClassNotFoundException {
41+
// It would be easier to call the loadClass() methods of the delegateClassLoaders
42+
// here, but we have to load the class from the byte code ourselves, because we
43+
// need it to be associated with our class loader.
44+
String path = name.replace('.', '/') + ".class";
45+
URL url = findResource(path);
46+
if (url == null) {
47+
throw new ClassNotFoundException(name);
48+
}
49+
ByteBuffer byteCode;
50+
try {
51+
byteCode = loadResource(url);
52+
} catch (IOException e) {
53+
throw new ClassNotFoundException(name, e);
54+
}
55+
return defineClass(name, byteCode, null);
56+
}
3957

40-
private ByteBuffer loadResource (URL url) throws IOException {
41-
InputStream stream = null;
42-
try {
43-
stream = url.openStream();
44-
int initialBufferCapacity = Math.min(0x40000, stream.available() + 1);
45-
if (initialBufferCapacity <= 2) {
46-
initialBufferCapacity = 0x10000; }
47-
else {
48-
initialBufferCapacity = Math.max(initialBufferCapacity, 0x200); }
49-
ByteBuffer buf = ByteBuffer.allocate(initialBufferCapacity);
50-
while (true) {
51-
if (!buf.hasRemaining()) {
52-
ByteBuffer newBuf = ByteBuffer.allocate(2*buf.capacity());
53-
buf.flip();
54-
newBuf.put(buf);
55-
buf = newBuf; }
56-
int len = stream.read(buf.array(), buf.position(), buf.remaining());
57-
if (len <= 0) {
58-
break; }
59-
((Buffer)buf).position(buf.position()+len); }
60-
((Buffer)buf).flip();
61-
return buf; }
62-
finally {
63-
if (stream != null) {
64-
stream.close(); }}}
58+
private ByteBuffer loadResource(URL url) throws IOException {
59+
InputStream stream = null;
60+
try {
61+
stream = url.openStream();
62+
int initialBufferCapacity = Math.min(0x40000, stream.available() + 1);
63+
if (initialBufferCapacity <= 2) {
64+
initialBufferCapacity = 0x10000;
65+
} else {
66+
initialBufferCapacity = Math.max(initialBufferCapacity, 0x200);
67+
}
68+
ByteBuffer buf = ByteBuffer.allocate(initialBufferCapacity);
69+
while (true) {
70+
if (!buf.hasRemaining()) {
71+
ByteBuffer newBuf = ByteBuffer.allocate(2 * buf.capacity());
72+
buf.flip();
73+
newBuf.put(buf);
74+
buf = newBuf;
75+
}
76+
int len = stream.read(buf.array(), buf.position(), buf.remaining());
77+
if (len <= 0) {
78+
break;
79+
}
80+
((Buffer) buf).position(buf.position() + len);
81+
}
82+
((Buffer) buf).flip();
83+
return buf;
84+
} finally {
85+
if (stream != null) {
86+
stream.close();
87+
}
88+
}
89+
}
6590

66-
protected URL findResource (String name) {
67-
for (ClassLoader delegate : delegateClassLoaders) {
68-
URL resource = delegate.getResource(name);
69-
if (resource != null) {
70-
return resource; }}
71-
return null; }
91+
protected URL findResource(String name) {
92+
for (ClassLoader delegate : delegateClassLoaders) {
93+
URL resource = delegate.getResource(name);
94+
if (resource != null) {
95+
return resource;
96+
}
97+
}
98+
return null;
99+
}
72100

73-
protected Enumeration<URL> findResources (String name) throws IOException {
74-
Vector<URL> vector = new Vector<URL>();
75-
for (ClassLoader delegate : delegateClassLoaders) {
76-
Enumeration<URL> enumeration = delegate.getResources(name);
77-
while (enumeration.hasMoreElements()) {
78-
vector.add(enumeration.nextElement()); }}
79-
return vector.elements(); }
101+
protected Enumeration<URL> findResources(String name) throws IOException {
102+
Vector<URL> vector = new Vector<URL>();
103+
for (ClassLoader delegate : delegateClassLoaders) {
104+
Enumeration<URL> enumeration = delegate.getResources(name);
105+
while (enumeration.hasMoreElements()) {
106+
vector.add(enumeration.nextElement());
107+
}
108+
}
109+
return vector.elements();
110+
}
80111

81112
}

lib/src/sortPom/java/com/diffplug/spotless/pom/SortPomFormatterFunc.java

+30-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
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+
*/
116
package com.diffplug.spotless.pom;
217

3-
import com.diffplug.spotless.FormatterFunc;
18+
import java.io.File;
19+
import java.io.FileReader;
20+
import java.io.FileWriter;
21+
import java.util.logging.Logger;
422

523
import org.apache.commons.io.IOUtils;
624

25+
import com.diffplug.spotless.FormatterFunc;
26+
727
import sortpom.SortPomImpl;
828
import sortpom.logger.SortPomLogger;
929
import sortpom.parameter.PluginParameters;
1030

11-
import java.io.File;
12-
import java.io.FileReader;
13-
import java.io.FileWriter;
14-
import java.util.logging.Logger;
15-
1631
class SortPomFormatterFunc implements FormatterFunc {
1732
private static final Logger logger = Logger.getLogger(SortPomStep.class.getName());
1833
private final SortPomStep.InternalState state;
@@ -46,15 +61,15 @@ public void error(String content) {
4661
logger.severe(content);
4762
}
4863
}, PluginParameters.builder()
49-
.setPomFile(pom)
50-
.setFileOutput(false, null, null, false)
51-
.setEncoding(state.encoding)
52-
.setFormatting(state.lineSeparator, state.expandEmptyElements, state.spaceBeforeCloseEmptyElement, state.keepBlankLines)
53-
.setIndent(state.nrOfIndentSpace, state.indentBlankLines, state.indentSchemaLocation)
54-
.setSortOrder(state.sortOrderFile, state.predefinedSortOrder)
55-
.setSortEntities(state.sortDependencies, state.sortDependencyExclusions, state.sortPlugins, state.sortProperties, state.sortModules, state.sortExecutions)
56-
.setTriggers(false)
57-
.build());
64+
.setPomFile(pom)
65+
.setFileOutput(false, null, null, false)
66+
.setEncoding(state.encoding)
67+
.setFormatting(state.lineSeparator, state.expandEmptyElements, state.spaceBeforeCloseEmptyElement, state.keepBlankLines)
68+
.setIndent(state.nrOfIndentSpace, state.indentBlankLines, state.indentSchemaLocation)
69+
.setSortOrder(state.sortOrderFile, state.predefinedSortOrder)
70+
.setSortEntities(state.sortDependencies, state.sortDependencyExclusions, state.sortPlugins, state.sortProperties, state.sortModules, state.sortExecutions)
71+
.setTriggers(false)
72+
.build());
5873
sortPom.sortPom();
5974
return IOUtils.toString(new FileReader(pom));
6075
}

lib/src/sortPom/java/com/diffplug/spotless/pom/SortPomStep.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,11 @@
2525
import java.lang.reflect.Constructor;
2626
import java.lang.reflect.InvocationTargetException;
2727
import java.lang.reflect.Method;
28-
import java.util.function.Function;
29-
import java.util.logging.Logger;
30-
31-
import com.diffplug.spotless.JarState;
32-
33-
import com.diffplug.spotless.Provisioner;
3428

3529
import com.diffplug.spotless.FormatterFunc;
3630
import com.diffplug.spotless.FormatterStep;
31+
import com.diffplug.spotless.JarState;
32+
import com.diffplug.spotless.Provisioner;
3733

3834
public class SortPomStep {
3935

@@ -44,6 +40,7 @@ private SortPomStep() {}
4440
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) {
4541
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);
4642
}
43+
4744
static final class InternalState implements Serializable {
4845
private static final long serialVersionUID = 1L;
4946

@@ -98,6 +95,7 @@ static final class InternalState implements Serializable {
9895
this.sortExecutions = sortExecutions;
9996
}
10097
}
98+
10199
static final class State implements Serializable {
102100
private static final long serialVersionUID = 1L;
103101
final JarState jarState;
@@ -106,24 +104,24 @@ static final class State implements Serializable {
106104

107105
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 {
108106
this.jarState = JarState.from("com.github.ekryd.sortpom:sortpom-sorter:3.0.0", provisioner);
109-
this.internalState=new InternalState(encoding, lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement, keepBlankLines, nrOfIndentSpace, indentBlankLines, indentSchemaLocation, predefinedSortOrder, sortOrderFile, sortDependencies, sortDependencyExclusions, sortPlugins, sortProperties, sortModules, sortExecutions);
107+
this.internalState = new InternalState(encoding, lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement, keepBlankLines, nrOfIndentSpace, indentBlankLines, indentSchemaLocation, predefinedSortOrder, sortOrderFile, sortDependencies, sortDependencyExclusions, sortPlugins, sortProperties, sortModules, sortExecutions);
110108
}
111109

112110
FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, IOException {
113111
ClassLoader classLoader = new JoinClassLoader(null, this.getClass().getClassLoader(), jarState.getClassLoader());
114112
Constructor<?> constructor = classLoader.loadClass(SortPomFormatterFunc.class.getName()).getConstructor(classLoader.loadClass(InternalState.class.getName()));
115113
constructor.setAccessible(true);
116114
ByteArrayOutputStream out = new ByteArrayOutputStream();
117-
ObjectOutputStream oos=new ObjectOutputStream(out);
115+
ObjectOutputStream oos = new ObjectOutputStream(out);
118116
oos.writeObject(internalState);
119-
ObjectInputStream ois=new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())){
117+
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())) {
120118
@Override
121119
protected Class<?> resolveClass(ObjectStreamClass desc) throws ClassNotFoundException {
122120
return classLoader.loadClass(desc.getName());
123121
}
124122
};
125-
Object state=ois.readObject();
126-
Object formatterFunc=constructor.newInstance(state);
123+
Object state = ois.readObject();
124+
Object formatterFunc = constructor.newInstance(state);
127125
Method apply = formatterFunc.getClass().getMethod("apply", String.class);
128126
apply.setAccessible(true);
129127
return input -> (String) apply.invoke(formatterFunc, input);

plugin-maven/src/main/java/com/diffplug/spotless/maven/pom/SortPom.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import org.apache.maven.plugins.annotations.Parameter;
1919

2020
import com.diffplug.spotless.FormatterStep;
21-
import com.diffplug.spotless.pom.SortPomStep;
2221
import com.diffplug.spotless.maven.FormatterStepConfig;
2322
import com.diffplug.spotless.maven.FormatterStepFactory;
23+
import com.diffplug.spotless.pom.SortPomStep;
2424

2525
public class SortPom implements FormatterStepFactory {
2626
@Parameter

plugin-maven/src/test/java/com/diffplug/spotless/maven/pom/SortPomTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616
package com.diffplug.spotless.maven.pom;
1717

18-
import com.diffplug.spotless.maven.MavenIntegrationHarness;
19-
2018
import org.junit.jupiter.api.Test;
2119

20+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
21+
2222
public class SortPomTest extends MavenIntegrationHarness {
2323
@Test
2424
public void testSortPomWithDefaultConfig() throws Exception {

0 commit comments

Comments
 (0)