-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
spring-javaformat/spring-javaformat-formatter-shader/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.spring.javaformat</groupId> | ||
<artifactId>spring-javaformat</artifactId> | ||
<version>0.0.18-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>spring-javaformat-formatter-shader</artifactId> | ||
<name>Spring JavaFormat Formatter Shaded</name> | ||
<properties> | ||
<main.basedir>${basedir}/../..</main.basedir> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>3.1.0</version> | ||
<type>maven-plugin</type> | ||
</dependency> | ||
</dependencies> | ||
</project> |
102 changes: 102 additions & 0 deletions
102
...-shader/src/main/java/io/spring/javaformat/formatter/shader/PrefsResourceTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2017-2020 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 io.spring.javaformat.formatter.shader; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStreamWriter; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarOutputStream; | ||
|
||
import org.apache.maven.plugins.shade.relocation.Relocator; | ||
import org.apache.maven.plugins.shade.resource.ResourceTransformer; | ||
|
||
/** | ||
* {@link ResourceTransformer} to fix formatter preference files. | ||
* | ||
* @author Phillip Webb | ||
*/ | ||
public class PrefsResourceTransformer implements ResourceTransformer { | ||
|
||
private final Map<String, List<String>> transformedResources = new LinkedHashMap<>(); | ||
|
||
@Override | ||
public boolean canTransformResource(String resource) { | ||
return resource.endsWith("formatter.prefs"); | ||
} | ||
|
||
@Override | ||
public void processResource(String resource, InputStream is, List<Relocator> relocators) throws IOException { | ||
List<String> trandformedLines = new ArrayList<>(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); | ||
String line = reader.readLine(); | ||
while (line != null) { | ||
trandformedLines.add(transformLine(line, relocators)); | ||
line = reader.readLine(); | ||
} | ||
this.transformedResources.put(resource, trandformedLines); | ||
} | ||
|
||
private String transformLine(String line, List<Relocator> relocators) { | ||
int splitIndex = line.lastIndexOf('='); | ||
if (splitIndex == -1) { | ||
return line; | ||
} | ||
String key = line.substring(0, splitIndex); | ||
String value = line.substring(splitIndex + 1); | ||
return relocateKey(key, relocators) + "=" + value; | ||
} | ||
|
||
private String relocateKey(String key, List<Relocator> relocators) { | ||
for (Relocator relocator : relocators) { | ||
if (relocator.canRelocateClass(key)) { | ||
return relocator.relocateClass(key); | ||
} | ||
} | ||
return key; | ||
} | ||
|
||
@Override | ||
public boolean hasTransformedResource() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void modifyOutputStream(JarOutputStream os) throws IOException { | ||
for (Map.Entry<String, List<String>> entry : this.transformedResources.entrySet()) { | ||
write(os, entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
|
||
private void write(JarOutputStream os, String resource, List<String> lines) throws IOException { | ||
os.putNextEntry(new JarEntry(resource)); | ||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os)); | ||
for (String line : lines) { | ||
writer.write(line); | ||
writer.write("\n"); | ||
} | ||
writer.flush(); | ||
} | ||
|
||
} |