Skip to content

fix #1076: Add support for palantir-java-format #1083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jan 12, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove unused, fix OPTIONS_STYLE casing
  • Loading branch information
carterkozak committed Jan 11, 2022
commit 6aff52d7f8ea25d05772ec8cd76688946a16f1e9
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ private PalantirJavaFormatStep() {}
private static final String OPTIONS_BUILDER_CLASS = "com.palantir.javaformat.java.JavaFormatterOptions$Builder";
private static final String OPTIONS_BUILDER_STYLE_METHOD = "style";
private static final String OPTIONS_BUILDER_BUILD_METHOD = "build";
private static final String OPTIONS_Style = "com.palantir.javaformat.java.JavaFormatterOptions$Style";
private static final String OPTIONS_MAX_LINE_LENGTH_METHOD = "maxLineLength";
private static final String OPTIONS_STYLE = "com.palantir.javaformat.java.JavaFormatterOptions$Style";

private static final String REMOVE_UNUSED_CLASS = "com.palantir.javaformat.java.RemoveUnusedImports";
private static final String REMOVE_UNUSED_METHOD = "removeUnusedImports";
Expand Down Expand Up @@ -101,14 +100,6 @@ static final class State implements Serializable {
final String version;
final String style;

State(String stepName, String version, Provisioner provisioner) throws Exception {
this(stepName, version, DEFAULT_STYLE, provisioner);
}

State(String stepName, String version, String style, Provisioner provisioner) throws Exception {
this(stepName, MAVEN_COORDINATE, version, style, provisioner);
}

State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner) throws Exception {
JVM_SUPPORT.assertFormatterSupported(version);
this.jarState = JarState.from(groupArtifact + ":" + version, provisioner);
Expand All @@ -127,7 +118,7 @@ FormatterFunc createFormat() throws Exception {
Method optionsBuilderMethod = optionsClass.getMethod(OPTIONS_BUILDER_METHOD);
Object optionsBuilder = optionsBuilderMethod.invoke(null);

Class<?> optionsStyleClass = classLoader.loadClass(OPTIONS_Style);
Class<?> optionsStyleClass = classLoader.loadClass(OPTIONS_STYLE);
Object styleConstant = Enum.valueOf((Class<Enum>) optionsStyleClass, style);
Method optionsBuilderStyleMethod = optionsBuilderClass.getMethod(OPTIONS_BUILDER_STYLE_METHOD, optionsStyleClass);
optionsBuilderStyleMethod.invoke(optionsBuilder, styleConstant);
Expand All @@ -147,17 +138,10 @@ FormatterFunc createFormat() throws Exception {
return JVM_SUPPORT.suggestLaterVersionOnError(version, (input -> {
String formatted = (String) formatterMethod.invoke(formatter, input);
String removedUnused = removeUnused.apply(formatted);
String sortedImports = (String) importOrdererMethod.invoke(null, removedUnused);
return sortedImports;
return (String) importOrdererMethod.invoke(null, removedUnused);
}));
}

FormatterFunc createRemoveUnusedImportsOnly() throws Exception {
ClassLoader classLoader = jarState.getClassLoader();
Function<String, String> removeUnused = constructRemoveUnusedFunction(classLoader);
return JVM_SUPPORT.suggestLaterVersionOnError(version, removeUnused::apply);
}

private static Function<String, String> constructRemoveUnusedFunction(ClassLoader classLoader)
throws NoSuchMethodException, ClassNotFoundException {
Class<?> removeUnusedClass = classLoader.loadClass(REMOVE_UNUSED_CLASS);
Expand Down