Skip to content

Commit

Permalink
Replaced importOrder array argument by varargs. Fixes #125.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Vennemeyer committed Jul 26, 2017
1 parent 48e3f9f commit 912eb5d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
10 changes: 10 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.nio.file.Files;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -37,12 +38,21 @@ public final class ImportOrderStep {
// prevent direct instantiation
private ImportOrderStep() {}

/** Method interface has been changed to
* {@link ImportOrderStep#importOrder(String...)}.*/
@Deprecated
public static FormatterStep createFromOrder(List<String> importOrder) {
// defensive copying and null checking
importOrder = requireElementsNonNull(new ArrayList<>(importOrder));
return createFromOrderImpl(importOrder);
}

public static FormatterStep createFromOrder(String... importOrder) {
// defensive copying and null checking
List<String> importOrderList = requireElementsNonNull(Arrays.asList(importOrder));
return createFromOrderImpl(importOrderList);
}

public static FormatterStep createFromFile(File importsFile) {
Objects.requireNonNull(importsFile);
return createFromOrderImpl(getImportOrder(importsFile));
Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Version 3.5.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-plugin-gradle/))

* Changed importOrder interface from array to varargs ([#125](https://github.com/diffplug/spotless/issues/125))

### Version 3.4.1 - July 11th 2017 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.4.1/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.4.1))

* Default eclipse version for `EclipseFormatterStep` bumped to `4.6.3` from `4.6.1`. ([#116](https://github.com/diffplug/spotless/issues/116))
Expand Down
4 changes: 2 additions & 2 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ spotless {
licenseHeaderFile 'spotless.license.java' // License header file
// Obviously, you can't specify both licenseHeader and licenseHeaderFile at the same time
importOrder ['java', 'javax', 'org', 'com', 'com.diffplug', ''] // An array of package names
importOrderFile 'spotless.importorder' // An import ordering file, exported from Eclipse
importOrder 'java', 'javax', 'org', 'com', 'com.diffplug', '' // A sequence of package names
importOrderFile 'spotless.importorder' // An import ordering file, exported from Eclipse
// As before, you can't specify both importOrder and importOrderFile at the same time
// You probably want an empty string at the end - all of the imports you didn't specify
// explicitly will go there.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,17 @@ public void licenseHeaderFile(Object licenseHeaderFile) {
licenseHeaderFile(licenseHeaderFile, JavaExtension.LICENSE_HEADER_DELIMITER);
}

/** Method interface has been changed to
* {@link GroovyExtension#importOrder(String...)}.*/
@Deprecated
public void importOrder(List<String> importOrder) {
addStep(ImportOrderStep.createFromOrder(importOrder));
}

public void importOrder(String... importOrder) {
addStep(ImportOrderStep.createFromOrder(importOrder));
}

public void importOrderFile(Object importOrderFile) {
Objects.requireNonNull(importOrderFile);
addStep(ImportOrderStep.createFromFile(getProject().file(importOrderFile)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,17 @@ public void licenseHeaderFile(Object licenseHeaderFile) {
licenseHeaderFile(licenseHeaderFile, LICENSE_HEADER_DELIMITER);
}

/** Method interface has been changed to
* {@link JavaExtension#importOrder(String...)}.*/
@Deprecated
public void importOrder(List<String> importOrder) {
addStep(ImportOrderStep.createFromOrder(importOrder));
}

public void importOrder(String... importOrder) {
addStep(ImportOrderStep.createFromOrder(importOrder));
}

public void importOrderFile(Object importOrderFile) {
Objects.requireNonNull(importOrderFile);
addStep(ImportOrderStep.createFromFile(getProject().file(importOrderFile)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
*/
package com.diffplug.spotless.java;

import java.util.Arrays;

import org.junit.Test;

import com.diffplug.common.collect.ImmutableList;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.NonSerializableList;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.SerializableEqualityTester;

public class ImportOrderStepTest extends ResourceHarness {
@Test
public void sortImportsFromArray() throws Throwable {
FormatterStep step = ImportOrderStep.createFromOrder(Arrays.asList("java", "javax", "org", "\\#com"));
FormatterStep step = ImportOrderStep.createFromOrder("java", "javax", "org", "\\#com");
assertOnResources(step, "java/importsorter/JavaCodeUnsortedImports.test", "java/importsorter/JavaCodeSortedImports.test");
}

Expand Down Expand Up @@ -64,26 +60,26 @@ public void misplacedImports() throws Throwable {

@Test
public void doesntThrowIfImportOrderIsntSerializable() {
ImportOrderStep.createFromOrder(NonSerializableList.of("java", "javax", "org", "\\#com"));
ImportOrderStep.createFromOrder("java", "javax", "org", "\\#com");
}

@Test
public void equality() throws Exception {
new SerializableEqualityTester() {
ImmutableList<String> imports = ImmutableList.of();
String[] imports = {};

@Override
protected void setupTest(API api) {
// same version == same
api.areDifferentThan();
// change the version, and it's different
imports = ImmutableList.of("a");
imports = new String[]{"a"};
api.areDifferentThan();
// change the version, and it's different
imports = ImmutableList.of("b");
imports = new String[]{"b"};
api.areDifferentThan();
// change the version, and it's different
imports = ImmutableList.of("a", "b");
imports = new String[]{"a", "b"};
api.areDifferentThan();
}

Expand Down

0 comments on commit 912eb5d

Please sign in to comment.