13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package com .diffplug .spotless .extra .pom ;
17
-
18
- import java .io .File ;
19
- import java .io .FileReader ;
20
- import java .io .FileWriter ;
16
+ package com .diffplug .spotless .pom ;
17
+
18
+ import java .io .ByteArrayInputStream ;
19
+ import java .io .ByteArrayOutputStream ;
20
+ import java .io .IOException ;
21
+ import java .io .ObjectInputStream ;
22
+ import java .io .ObjectOutputStream ;
23
+ import java .io .ObjectStreamClass ;
21
24
import java .io .Serializable ;
22
- import java .util . logging . Logger ;
23
-
24
- import org . apache . commons . io . IOUtils ;
25
+ import java .lang . reflect . Constructor ;
26
+ import java . lang . reflect . InvocationTargetException ;
27
+ import java . lang . reflect . Method ;
25
28
26
29
import com .diffplug .spotless .FormatterFunc ;
27
30
import com .diffplug .spotless .FormatterStep ;
28
-
29
- import sortpom .SortPomImpl ;
30
- import sortpom .logger .SortPomLogger ;
31
- import sortpom .parameter .PluginParameters ;
31
+ import com .diffplug .spotless .JarState ;
32
+ import com .diffplug .spotless .Provisioner ;
32
33
33
34
public class SortPomStep {
34
35
35
36
public static final String NAME = "sortPom" ;
36
- private static final Logger logger = Logger .getLogger (SortPomStep .class .getName ());
37
37
38
38
private SortPomStep () {}
39
39
40
- 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 ) {
41
- return FormatterStep .createLazy (NAME , () -> new State (encoding , lineSeparator , expandEmptyElements , spaceBeforeCloseEmptyElement , keepBlankLines , nrOfIndentSpace , indentBlankLines , indentSchemaLocation , predefinedSortOrder , sortOrderFile , sortDependencies , sortDependencyExclusions , sortPlugins , sortProperties , sortModules , sortExecutions ), State ::createFormat );
40
+ 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 ) {
41
+ 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 );
42
42
}
43
43
44
- static final class State implements Serializable {
44
+ static final class InternalState implements Serializable {
45
45
private static final long serialVersionUID = 1L ;
46
+
46
47
final String encoding ;
47
48
48
49
final String lineSeparator ;
@@ -75,7 +76,7 @@ static final class State implements Serializable {
75
76
76
77
final boolean sortExecutions ;
77
78
78
- 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 ) {
79
+ 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 ) {
79
80
this .encoding = encoding ;
80
81
this .lineSeparator = lineSeparator ;
81
82
this .expandEmptyElements = expandEmptyElements ;
@@ -93,44 +94,38 @@ static final class State implements Serializable {
93
94
this .sortModules = sortModules ;
94
95
this .sortExecutions = sortExecutions ;
95
96
}
97
+ }
96
98
97
- FormatterFunc createFormat () {
98
- return input -> {
99
- // SortPom expects a file to sort, so we write the inpout into a temporary file
100
- File pom = File .createTempFile ("pom" , ".xml" );
101
- pom .deleteOnExit ();
102
- try (FileWriter fw = new FileWriter (pom )) {
103
- fw .write (input );
99
+ static final class State implements Serializable {
100
+ private static final long serialVersionUID = 1L ;
101
+ final JarState jarState ;
102
+
103
+ final InternalState internalState ;
104
+
105
+ 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 {
106
+ this .jarState = JarState .from ("com.github.ekryd.sortpom:sortpom-sorter:3.0.0" , provisioner );
107
+ this .internalState = new InternalState (encoding , lineSeparator , expandEmptyElements , spaceBeforeCloseEmptyElement , keepBlankLines , nrOfIndentSpace , indentBlankLines , indentSchemaLocation , predefinedSortOrder , sortOrderFile , sortDependencies , sortDependencyExclusions , sortPlugins , sortProperties , sortModules , sortExecutions );
108
+ }
109
+
110
+ FormatterFunc createFormat () throws ClassNotFoundException , NoSuchMethodException , InvocationTargetException , InstantiationException , IllegalAccessException , IOException {
111
+ ClassLoader classLoader = new DelegatingClassLoader (this .getClass ().getClassLoader (), jarState .getClassLoader ());
112
+ Constructor <?> constructor = classLoader .loadClass (SortPomFormatterFunc .class .getName ()).getConstructor (classLoader .loadClass (InternalState .class .getName ()));
113
+ constructor .setAccessible (true );
114
+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
115
+ ObjectOutputStream oos = new ObjectOutputStream (out );
116
+ oos .writeObject (internalState );
117
+ ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (out .toByteArray ())) {
118
+ @ Override
119
+ protected Class <?> resolveClass (ObjectStreamClass desc ) throws ClassNotFoundException {
120
+ return classLoader .loadClass (desc .getName ());
104
121
}
105
- SortPomImpl sortPom = new SortPomImpl ();
106
- sortPom .setup (new SortPomLogger () {
107
- @ Override
108
- public void warn (String content ) {
109
- logger .warning (content );
110
- }
111
-
112
- @ Override
113
- public void info (String content ) {
114
- logger .info (content );
115
- }
116
-
117
- @ Override
118
- public void error (String content ) {
119
- logger .severe (content );
120
- }
121
- }, PluginParameters .builder ()
122
- .setPomFile (pom )
123
- .setFileOutput (false , null , null , false )
124
- .setEncoding (encoding )
125
- .setFormatting (lineSeparator , expandEmptyElements , spaceBeforeCloseEmptyElement , keepBlankLines )
126
- .setIndent (nrOfIndentSpace , indentBlankLines , indentSchemaLocation )
127
- .setSortOrder (sortOrderFile , predefinedSortOrder )
128
- .setSortEntities (sortDependencies , sortDependencyExclusions , sortPlugins , sortProperties , sortModules , sortExecutions )
129
- .setTriggers (false )
130
- .build ());
131
- sortPom .sortPom ();
132
- return IOUtils .toString (new FileReader (pom ));
133
122
};
123
+ Object state = ois .readObject ();
124
+ Object formatterFunc = constructor .newInstance (state );
125
+ Method apply = formatterFunc .getClass ().getMethod ("apply" , String .class );
126
+ apply .setAccessible (true );
127
+ return input -> (String ) apply .invoke (formatterFunc , input );
134
128
}
129
+
135
130
}
136
131
}
0 commit comments