Skip to content

Commit

Permalink
Fix shaded formatter prefs
Browse files Browse the repository at this point in the history
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
philwebb committed Jan 10, 2020
1 parent da80f7e commit 3cc9436
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 1 deletion.
1 change: 1 addition & 0 deletions spring-javaformat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<module>spring-javaformat-formatter-eclipse-runtime</module>
<module>spring-javaformat-formatter-eclipse</module>
<module>spring-javaformat-formatter</module>
<module>spring-javaformat-formatter-shader</module>
<module>spring-javaformat-formatter-shaded</module>
</modules>
</project>
11 changes: 10 additions & 1 deletion spring-javaformat/spring-javaformat-formatter-shaded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-javaformat-formatter-shader</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<configuration>
<createSourcesJar>true</createSourcesJar>
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
Expand All @@ -71,6 +78,9 @@
<shadedPattern>io.spring.javaformat.org.osgi</shadedPattern>
</relocation>
</relocations>
<transformers>
<transformer implementation="io.spring.javaformat.formatter.shader.PrefsResourceTransformer" />
</transformers>
</configuration>
<executions>
<execution>
Expand All @@ -84,7 +94,6 @@
</plugins>
</build>
<dependencies>
<!-- Provided -->
<dependency>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-formatter</artifactId>
Expand Down
24 changes: 24 additions & 0 deletions spring-javaformat/spring-javaformat-formatter-shader/pom.xml
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>
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();
}

}

0 comments on commit 3cc9436

Please sign in to comment.