Skip to content

Commit

Permalink
Support google-java-format's skip-javadoc-formatting option
Browse files Browse the repository at this point in the history
Add configuration option to switch the option of the googleJavaFormat step.
Default to format javadoc for backwards compatibility.

Fixes diffplug#1287
  • Loading branch information
mokamoto12 committed Aug 30, 2023
1 parent efe6734 commit b70b6ee
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ public class GoogleJavaFormatFormatterFunc implements FormatterFunc {

private final boolean reorderImports;

public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings, boolean reorderImports) {
public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings, boolean reorderImports, boolean formatJavadoc) {
this.version = Objects.requireNonNull(version);
this.formatterStyle = Style.valueOf(Objects.requireNonNull(style));
this.reflowStrings = reflowStrings;
this.reorderImports = reorderImports;

this.formatter = new Formatter(JavaFormatterOptions.builder()
.style(formatterStyle)
.build());
JavaFormatterOptions.Builder builder = JavaFormatterOptions.builder().style(formatterStyle);
if (!formatJavadoc) {
builder = builder.formatJavadoc(false);
}
this.formatter = new Formatter(builder.build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private GoogleJavaFormatStep() {}
private static final String DEFAULT_STYLE = "GOOGLE";
private static final boolean DEFAULT_REFLOW_LONG_STRINGS = false;
private static final boolean DEFAULT_REORDER_IMPORTS = false;
private static final boolean DEFAULT_FORMAT_JAVADOC = true;
static final String NAME = "google-java-format";
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";

Expand All @@ -57,11 +58,11 @@ public static FormatterStep create(String version, String style, Provisioner pro
}

public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) {
return create(groupArtifact, version, style, provisioner, reflowLongStrings, false);
return create(groupArtifact, version, style, provisioner, reflowLongStrings, false, DEFAULT_FORMAT_JAVADOC);
}

/** Creates a step which formats everything - groupArtifact, code, import order, and unused imports - and optionally reflows long strings. */
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) {
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports, boolean formatJavadoc) {
Objects.requireNonNull(groupArtifact, "groupArtifact");
if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) {
throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'");
Expand All @@ -70,7 +71,7 @@ public static FormatterStep create(String groupArtifact, String version, String
Objects.requireNonNull(style, "style");
Objects.requireNonNull(provisioner, "provisioner");
return FormatterStep.createLazy(NAME,
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings, reorderImports),
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings, reorderImports, formatJavadoc),
State::createFormat);
}

Expand Down Expand Up @@ -101,6 +102,10 @@ public static boolean defaultReorderImports() {
return DEFAULT_REORDER_IMPORTS;
}

public static boolean defaultFormatJavadoc() {
return DEFAULT_FORMAT_JAVADOC;
}

static final class State implements Serializable {
private static final long serialVersionUID = 1L;

Expand All @@ -111,6 +116,7 @@ static final class State implements Serializable {
final String style;
final boolean reflowLongStrings;
final boolean reorderImports;
final boolean formatJavadoc;

State(String stepName, String version, Provisioner provisioner) throws Exception {
this(stepName, version, DEFAULT_STYLE, provisioner);
Expand All @@ -121,10 +127,10 @@ static final class State implements Serializable {
}

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

State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) throws Exception {
State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports, boolean formatJavadoc) throws Exception {
JVM_SUPPORT.assertFormatterSupported(version);
ModuleHelper.doOpenInternalPackagesIfRequired();
this.jarState = JarState.from(groupArtifact + ":" + version, provisioner);
Expand All @@ -133,13 +139,14 @@ static final class State implements Serializable {
this.style = style;
this.reflowLongStrings = reflowLongStrings;
this.reorderImports = reorderImports;
this.formatJavadoc = formatJavadoc;
}

FormatterFunc createFormat() throws Exception {
final ClassLoader classLoader = jarState.getClassLoader();
Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.java.GoogleJavaFormatFormatterFunc");
Constructor<?> constructor = formatterFunc.getConstructor(String.class, String.class, boolean.class, boolean.class);
FormatterFunc googleJavaFormatFormatterFunc = (FormatterFunc) constructor.newInstance(version, style, reflowLongStrings, reorderImports);
Constructor<?> constructor = formatterFunc.getConstructor(String.class, String.class, boolean.class, boolean.class, boolean.class);
FormatterFunc googleJavaFormatFormatterFunc = (FormatterFunc) constructor.newInstance(version, style, reflowLongStrings, reorderImports, formatJavadoc);

return JVM_SUPPORT.suggestLaterVersionOnError(version, googleJavaFormatFormatterFunc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public class GoogleJavaFormatConfig {
String style;
boolean reflowLongStrings;
boolean reorderImports;
boolean formatJavadoc = true;

GoogleJavaFormatConfig(String version) {
this.version = Objects.requireNonNull(version);
Expand Down Expand Up @@ -213,14 +214,25 @@ public GoogleJavaFormatConfig reorderImports(boolean reorderImports) {
return this;
}

public GoogleJavaFormatConfig skipJavadocFormatting() {
return formatJavadoc(false);
}

public GoogleJavaFormatConfig formatJavadoc(boolean formatJavadoc) {
this.formatJavadoc = formatJavadoc;
replaceStep(createStep());
return this;
}

private FormatterStep createStep() {
return GoogleJavaFormatStep.create(
groupArtifact,
version,
style,
provisioner(),
reflowLongStrings,
reorderImports);
reorderImports,
formatJavadoc);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,30 @@ void integration() throws IOException {
"googleJavaFormat()");
checkRunsThenUpToDate();
}

@Test
void integrationWithSkipJavadocFormatting() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"repositories { mavenCentral() }",
"",
"spotless {",
" java {",
" target file('test.java')",
" googleJavaFormat('1.12.0').skipJavadocFormatting()",
" }",
"}");

setFile("test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test");
gradleRunner().withArguments("spotlessApply").build();
assertFile("test.java").sameAsResource("java/googlejavaformat/JavaCodeFormattedSkipJavadocFormatting.test");

checkRunsThenUpToDate();
replace("build.gradle",
"googleJavaFormat('1.12.0')",
"googleJavaFormat()");
checkRunsThenUpToDate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ public class GoogleJavaFormat implements FormatterStepFactory {
@Parameter
private Boolean reorderImports;

@Parameter
private Boolean formatJavadoc;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
String groupArtifact = this.groupArtifact != null ? this.groupArtifact : GoogleJavaFormatStep.defaultGroupArtifact();
String version = this.version != null ? this.version : GoogleJavaFormatStep.defaultVersion();
String style = this.style != null ? this.style : GoogleJavaFormatStep.defaultStyle();
boolean reflowLongStrings = this.reflowLongStrings != null ? this.reflowLongStrings : GoogleJavaFormatStep.defaultReflowLongStrings();
boolean reorderImports = this.reorderImports != null ? this.reorderImports : GoogleJavaFormatStep.defaultReorderImports();
return GoogleJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner(), reflowLongStrings, reorderImports);
boolean formatJavadoc = this.formatJavadoc != null ? this.formatJavadoc : GoogleJavaFormatStep.defaultFormatJavadoc();
return GoogleJavaFormatStep.create(groupArtifact, version, style, config.getProvisioner(), reflowLongStrings, reorderImports, formatJavadoc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ void specificVersionReflowLongStrings() throws Exception {
runTest("java/googlejavaformat/JavaCodeFormattedReflowLongStrings.test");
}

@Test
void specificVersionSkipJavadocFormatting() throws Exception {
writePomWithJavaSteps(
"<googleJavaFormat>",
" <version>1.12.0</version>",
" <formatJavadoc>false</formatJavadoc>",
"</googleJavaFormat>");

runTest("java/googlejavaformat/JavaCodeFormattedSkipJavadocFormatting.test");
}

private void runTest(String targetResource) throws Exception {
String path = "src/main/java/test.java";
setFile(path).toResource("java/googlejavaformat/JavaCodeUnformatted.test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mylib.UsedA;
import mylib.UsedB;

public class Java {
/** Some javadoc. */
public static void main(String[] args) {
System.out.println(
"A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mylib.UsedA;
import mylib.UsedB;

public class Java {
/** Some javadoc. */
public static void main(String[] args) {
System.out.println(
"A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mylib.UsedA;
import mylib.UsedB;

public class Java {
/** Some javadoc. */
public static void main(String[] args) {
System.out.println(
"A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mylib.UsedA;
import mylib.UsedB;

public class Java {
/** Some javadoc. */
public static void main(String[] args) {
System.out.println(
"A very very very very very very very very very very very very very very very very very"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import mylib.UsedA;
import mylib.UsedB;

public class Java {
/**
* Some javadoc.
*/
public static void main(String[] args) {
System.out.println(
"A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length.");
UsedB.someMethod();
UsedA.someMethod();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import mylib.UsedB;
import mylib.UsedA;

public class Java {
/**
* Some javadoc.
*/
public static void main(String[] args) {
System.out.println("A very very very very very very very very very very very very very very very very very very very very very long string that goes beyond the 100-character line length.");
UsedB.someMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ void behaviorWithReflowLongStrings() throws Exception {
}
}

@Test
void behaviorWithSkipFormatJavadoc() throws Exception {
try (StepHarness step = StepHarness.forStep(GoogleJavaFormatStep.create(GoogleJavaFormatStep.defaultGroupArtifact(), GoogleJavaFormatStep.defaultVersion(), GoogleJavaFormatStep.defaultStyle(), TestProvisioner.mavenCentral(), GoogleJavaFormatStep.defaultReflowLongStrings(), GoogleJavaFormatStep.defaultReorderImports(), false))) {
step.testResource("java/googlejavaformat/JavaCodeUnformatted.test", "java/googlejavaformat/JavaCodeFormattedSkipJavadocFormatting.test")
.testResource("java/googlejavaformat/JavaCodeWithLicenseUnformatted.test", "java/googlejavaformat/JavaCodeWithLicenseFormatted.test")
.testResource("java/googlejavaformat/JavaCodeWithLicensePackageUnformatted.test", "java/googlejavaformat/JavaCodeWithLicensePackageFormatted.test")
.testResource("java/googlejavaformat/JavaCodeWithPackageUnformatted.test", "java/googlejavaformat/JavaCodeWithPackageFormatted.test");
}
}

@Test
void behaviorWithCustomGroupArtifact() throws Exception {
FormatterStep step = GoogleJavaFormatStep.create(GoogleJavaFormatStep.defaultGroupArtifact(), "1.10.0", GoogleJavaFormatStep.defaultStyle(), TestProvisioner.mavenCentral(), false);
Expand Down

0 comments on commit b70b6ee

Please sign in to comment.