Skip to content

Commit fa203e4

Browse files
committed
Remove reflection impl to rely on glue impl
1 parent 13d9d3c commit fa203e4

File tree

6 files changed

+15
-167
lines changed

6 files changed

+15
-167
lines changed

lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ dependencies {
5757
palantirJavaFormatCompileOnly 'com.palantir.javaformat:palantir-java-format:1.1.0' // this version needs to stay compilable against Java 8 for CI Job testNpm
5858

5959
// used jackson-based formatters
60-
jacksonCompileOnly 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.13.4'
60+
jacksonCompileOnly 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.14.1'
6161

6262
String VER_KTFMT = '0.42'
6363
ktfmtCompileOnly "com.facebook:ktfmt:$VER_KTFMT"

lib/src/jackson/java/com/diffplug/spotless/glue/yaml/YamlJacksonFormatterFunc.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ public class YamlJacksonFormatterFunc implements FormatterFunc {
3131
private List<String> enabledFeatures;
3232
private List<String> disabledFeatures;
3333

34-
// private static final Logger logger = LoggerFactory.getLogger(YamlJacksonFormatterFunc.class);
35-
3634
public YamlJacksonFormatterFunc(List<String> enabledFeatures, List<String> disabledFeatures) {
3735
this.enabledFeatures = enabledFeatures;
3836
this.disabledFeatures = disabledFeatures;

lib/src/main/java/com/diffplug/spotless/yaml/YamlJacksonStep.java

Lines changed: 12 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
import java.io.IOException;
1919
import java.io.Serializable;
20+
import java.lang.reflect.Constructor;
2021
import java.lang.reflect.InvocationTargetException;
21-
import java.lang.reflect.Method;
2222
import java.util.Arrays;
2323
import java.util.List;
2424
import java.util.Objects;
@@ -32,9 +32,12 @@
3232
* Simple YAML formatter which reformats the file according to Jackson YAMLFactory.
3333
*/
3434
// https://stackoverflow.com/questions/14515994/convert-json-string-to-pretty-print-json-output-using-jackson
35-
public final class YamlJacksonStep {
35+
public class YamlJacksonStep {
3636
static final String MAVEN_COORDINATE = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:";
37-
static final String DEFAULT_VERSION = "2.13.4";
37+
// https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml
38+
static final String DEFAULT_VERSION = "2.14.1";
39+
40+
private YamlJacksonStep() {}
3841

3942
public static String defaultVersion() {
4043
return DEFAULT_VERSION;
@@ -69,89 +72,14 @@ private State(List<String> enabledFeatures,
6972
this.enabledFeatures = enabledFeatures;
7073
this.disabledFeatures = disabledFeatures;
7174

72-
this.jarState = JarState.from(MAVEN_COORDINATE + jacksonVersion, provisioner);
75+
this.jarState = JarState.from(YamlJacksonStep.MAVEN_COORDINATE + jacksonVersion, provisioner);
7376
}
7477

75-
FormatterFunc toFormatter() {
76-
Class<?> jsonFactoryClass;
77-
Class<?> yamlFactoryClass;
78-
Class<?> objectMapperClass;
79-
80-
Class<?> serializationFeatureClass;
81-
Method enableFeature;
82-
Method disableFeature;
83-
84-
Method stringToNode;
85-
Method nodeToString;
86-
try {
87-
ClassLoader classLoader = jarState.getClassLoader();
88-
jsonFactoryClass = classLoader.loadClass("com.fasterxml.jackson.core.JsonFactory");
89-
yamlFactoryClass = classLoader.loadClass("com.fasterxml.jackson.dataformat.yaml.YAMLFactory");
90-
91-
objectMapperClass = classLoader.loadClass("com.fasterxml.jackson.databind.ObjectMapper");
92-
93-
// Configure the ObjectMapper
94-
// https://github.com/FasterXML/jackson-databind#commonly-used-features
95-
{
96-
serializationFeatureClass = classLoader.loadClass("com.fasterxml.jackson.databind.SerializationFeature");
97-
enableFeature = objectMapperClass.getMethod("enable", serializationFeatureClass);
98-
disableFeature = objectMapperClass.getMethod("disable", serializationFeatureClass);
99-
}
100-
101-
// https://stackoverflow.com/questions/25222327/deserialize-pojos-from-multiple-yaml-documents-in-a-single-file-in-jackson
102-
// List<ObjectNode> docs = mapper
103-
// .readValues<ObjectNode>(yamlParser, new TypeReference<ObjectNode> {})
104-
// .readAll();
105-
106-
Class<?> jsonNodeClass = classLoader.loadClass("com.fasterxml.jackson.databind.JsonNode");
107-
108-
// This will transit with a JsonNode
109-
// A JsonNode may keep the comments from the input node
110-
stringToNode = objectMapperClass.getMethod("readTree", String.class);
111-
// Not 'toPrettyString' as one could require no INDENT_OUTPUT
112-
nodeToString = jsonNodeClass.getMethod("toPrettyString");
113-
} catch (ClassNotFoundException | NoSuchMethodException e) {
114-
throw new IllegalStateException("There was a problem preparing org.json dependencies", e);
115-
}
116-
117-
return s -> {
118-
if (s.isEmpty()) {
119-
return s;
120-
}
121-
122-
Object yamlFactory = yamlFactoryClass.getConstructor().newInstance();
123-
Object objectMapper = objectMapperClass.getConstructor(jsonFactoryClass).newInstance(yamlFactory);
124-
125-
for (String feature : enabledFeatures) {
126-
// https://stackoverflow.com/questions/3735927/java-instantiating-an-enum-using-reflection
127-
Object indentOutput = Enum.valueOf(serializationFeatureClass.asSubclass(Enum.class), feature);
128-
129-
enableFeature.invoke(objectMapper, indentOutput);
130-
}
131-
132-
for (String feature : disabledFeatures) {
133-
// https://stackoverflow.com/questions/3735927/java-instantiating-an-enum-using-reflection
134-
Object indentOutput = Enum.valueOf(serializationFeatureClass.asSubclass(Enum.class), feature);
135-
136-
disableFeature.invoke(objectMapper, indentOutput);
137-
}
138-
139-
return format(objectMapper, stringToNode, nodeToString, s);
140-
};
78+
FormatterFunc toFormatter() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
79+
InstantiationException, IllegalAccessException {
80+
Class<?> formatterFunc = jarState.getClassLoader().loadClass("com.diffplug.spotless.glue.yaml.YamlJacksonFormatterFunc");
81+
Constructor<?> constructor = formatterFunc.getConstructor(List.class, List.class);
82+
return (FormatterFunc) constructor.newInstance(enabledFeatures, disabledFeatures);
14183
}
142-
143-
private String format(Object objectMapper, Method stringToNode, Method nodeToString, String s)
144-
throws IllegalAccessException, IllegalArgumentException {
145-
try {
146-
Object node = stringToNode.invoke(objectMapper, s);
147-
return (String) nodeToString.invoke(node);
148-
} catch (InvocationTargetException ex) {
149-
throw new AssertionError("Unable to format YAML", ex.getCause());
150-
}
151-
}
152-
}
153-
154-
private YamlJacksonStep() {
155-
// cannot be directly instantiated
15684
}
15785
}

lib/src/main/java/com/diffplug/spotless/yaml/YamlJacksonV2Step.java

Lines changed: 0 additions & 77 deletions
This file was deleted.

plugin-maven/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ Uses Jackson and YAMLFactory to pretty print objects:
923923

924924
```xml
925925
<jackson>
926-
<version>2.13.4</version> <!-- optional: The version of 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml' to be used -->
926+
<version>2.14.1</version> <!-- optional: The version of 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml' to be used -->
927927
<enabledFeatures> <!-- optional: Customize the set of enabled features -->
928928
<enabledFeature>INDENT_OUTPUT<enabledFeature/>
929929
</enabledFeatures>

plugin-maven/src/main/java/com/diffplug/spotless/maven/yaml/Jackson.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.diffplug.spotless.maven.FormatterStepConfig;
2525
import com.diffplug.spotless.maven.FormatterStepFactory;
2626
import com.diffplug.spotless.yaml.YamlJacksonStep;
27-
import com.diffplug.spotless.yaml.YamlJacksonV2Step;
2827

2928
public class Jackson implements FormatterStepFactory {
3029

@@ -41,7 +40,7 @@ public class Jackson implements FormatterStepFactory {
4140
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
4241
List<String> enabledFeaturesAsList = Arrays.asList(enabledFeatures);
4342
List<String> disabledFeaturesAsList = Arrays.asList(disabledFeatures);
44-
return YamlJacksonV2Step
43+
return YamlJacksonStep
4544
.create(enabledFeaturesAsList, disabledFeaturesAsList, version, stepConfig.getProvisioner());
4645
}
4746
}

0 commit comments

Comments
 (0)