Skip to content

Commit 3cc9436

Browse files
committed
Fix shaded formatter prefs
Update the shaded jar so that the key values in `formatter.prefs` are also processed. Prior to this commit the keys all started with `org.eclipse` and were unfortunately not picked up due to the fact that the shade plugin changed the value of the `JavaCore.PLUGIN_ID` constant in the bytecode. Closes gh-161
1 parent da80f7e commit 3cc9436

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed

spring-javaformat/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<module>spring-javaformat-formatter-eclipse-runtime</module>
3737
<module>spring-javaformat-formatter-eclipse</module>
3838
<module>spring-javaformat-formatter</module>
39+
<module>spring-javaformat-formatter-shader</module>
3940
<module>spring-javaformat-formatter-shaded</module>
4041
</modules>
4142
</project>

spring-javaformat/spring-javaformat-formatter-shaded/pom.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@
4848
<plugin>
4949
<groupId>org.apache.maven.plugins</groupId>
5050
<artifactId>maven-shade-plugin</artifactId>
51+
<dependencies>
52+
<dependency>
53+
<groupId>${project.groupId}</groupId>
54+
<artifactId>spring-javaformat-formatter-shader</artifactId>
55+
<version>${project.version}</version>
56+
</dependency>
57+
</dependencies>
5158
<configuration>
5259
<createSourcesJar>true</createSourcesJar>
5360
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
@@ -71,6 +78,9 @@
7178
<shadedPattern>io.spring.javaformat.org.osgi</shadedPattern>
7279
</relocation>
7380
</relocations>
81+
<transformers>
82+
<transformer implementation="io.spring.javaformat.formatter.shader.PrefsResourceTransformer" />
83+
</transformers>
7484
</configuration>
7585
<executions>
7686
<execution>
@@ -84,7 +94,6 @@
8494
</plugins>
8595
</build>
8696
<dependencies>
87-
<!-- Provided -->
8897
<dependency>
8998
<groupId>io.spring.javaformat</groupId>
9099
<artifactId>spring-javaformat-formatter</artifactId>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>io.spring.javaformat</groupId>
8+
<artifactId>spring-javaformat</artifactId>
9+
<version>0.0.18-SNAPSHOT</version>
10+
</parent>
11+
<artifactId>spring-javaformat-formatter-shader</artifactId>
12+
<name>Spring JavaFormat Formatter Shaded</name>
13+
<properties>
14+
<main.basedir>${basedir}/../..</main.basedir>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.apache.maven.plugins</groupId>
19+
<artifactId>maven-shade-plugin</artifactId>
20+
<version>3.1.0</version>
21+
<type>maven-plugin</type>
22+
</dependency>
23+
</dependencies>
24+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2017-2020 the original author or authors.
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+
* https://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+
17+
package io.spring.javaformat.formatter.shader;
18+
19+
import java.io.BufferedReader;
20+
import java.io.BufferedWriter;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.io.InputStreamReader;
24+
import java.io.OutputStreamWriter;
25+
import java.util.ArrayList;
26+
import java.util.LinkedHashMap;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.jar.JarEntry;
30+
import java.util.jar.JarOutputStream;
31+
32+
import org.apache.maven.plugins.shade.relocation.Relocator;
33+
import org.apache.maven.plugins.shade.resource.ResourceTransformer;
34+
35+
/**
36+
* {@link ResourceTransformer} to fix formatter preference files.
37+
*
38+
* @author Phillip Webb
39+
*/
40+
public class PrefsResourceTransformer implements ResourceTransformer {
41+
42+
private final Map<String, List<String>> transformedResources = new LinkedHashMap<>();
43+
44+
@Override
45+
public boolean canTransformResource(String resource) {
46+
return resource.endsWith("formatter.prefs");
47+
}
48+
49+
@Override
50+
public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException {
51+
List<String> trandformedLines = new ArrayList<>();
52+
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
53+
String line = reader.readLine();
54+
while (line != null) {
55+
trandformedLines.add(transformLine(line, relocators));
56+
line = reader.readLine();
57+
}
58+
this.transformedResources.put(resource, trandformedLines);
59+
}
60+
61+
private String transformLine(String line, List<Relocator> relocators) {
62+
int splitIndex = line.lastIndexOf('=');
63+
if (splitIndex == -1) {
64+
return line;
65+
}
66+
String key = line.substring(0, splitIndex);
67+
String value = line.substring(splitIndex + 1);
68+
return relocateKey(key, relocators) + "=" + value;
69+
}
70+
71+
private String relocateKey(String key, List<Relocator> relocators) {
72+
for (Relocator relocator : relocators) {
73+
if (relocator.canRelocateClass(key)) {
74+
return relocator.relocateClass(key);
75+
}
76+
}
77+
return key;
78+
}
79+
80+
@Override
81+
public boolean hasTransformedResource() {
82+
return true;
83+
}
84+
85+
@Override
86+
public void modifyOutputStream(JarOutputStream os) throws IOException {
87+
for (Map.Entry<String, List<String>> entry : this.transformedResources.entrySet()) {
88+
write(os, entry.getKey(), entry.getValue());
89+
}
90+
}
91+
92+
private void write(JarOutputStream os, String resource, List<String> lines) throws IOException {
93+
os.putNextEntry(new JarEntry(resource));
94+
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
95+
for (String line : lines) {
96+
writer.write(line);
97+
writer.write("\n");
98+
}
99+
writer.flush();
100+
}
101+
102+
}

0 commit comments

Comments
 (0)