Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;

import com.diffplug.common.base.Errors;
Expand Down Expand Up @@ -151,6 +152,12 @@ public Properties getPreferences() {
return preferences.getProperties();
}

/** Returns first coordinate from sorted set that starts with a given prefix.*/
public Optional<String> getMavenCoordinate(String prefix) {
return jarState.getMavenCoordinates().stream()
.filter(coordinate -> coordinate.startsWith(prefix)).findFirst();
}

/** Load class based on the given configuration of JAR provider and Maven coordinates. */
public Class<?> loadClass(String name) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ public final class EclipseJdtFormatterStep {
private EclipseJdtFormatterStep() {}

private static final String NAME = "eclipse jdt formatter";
private static final String FORMATTER_CLASS = "com.diffplug.gradle.spotless.java.eclipse.EclipseFormatterStepImpl";
private static final String DEFAULT_VERSION = "4.7.2";
private static final String FORMATTER_CLASS_OLD = "com.diffplug.gradle.spotless.java.eclipse.EclipseFormatterStepImpl";
private static final String FORMATTER_CLASS = "com.diffplug.spotless.extra.eclipse.java.EclipseJdtFormatterStepImpl";
private static final String MAVEN_GROUP_ARTIFACT = "com.diffplug.spotless:spotless-eclipse-jdt";
private static final String DEFAULT_VERSION = "4.8.0";
private static final String FORMATTER_METHOD = "format";

public static String defaultVersion() {
Expand All @@ -43,9 +45,15 @@ public static EclipseBasedStepBuilder createBuilder(Provisioner provisioner) {
}

private static FormatterFunc apply(State state) throws Exception {
Class<?> formatterClazz = state.loadClass(FORMATTER_CLASS);
Class<?> formatterClazz = getClass(state);
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences());
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
return input -> (String) method.invoke(formatter, input);
}

private static Class<?> getClass(State state) {
if (state.getMavenCoordinate(MAVEN_GROUP_ARTIFACT).isPresent())
return state.loadClass(FORMATTER_CLASS);
return state.loadClass(FORMATTER_CLASS_OLD);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ifs without brackets are dangerous. I would change to this:

if ( ) {
   return
} else {
   return
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Spotless formatter based on JDT version 4.8.0 (see https://projects.eclipse.org/projects/eclipse.jdt)
com.diffplug.spotless:spotless-eclipse-jdt:4.8.0
com.diffplug.spotless:spotless-eclipse-base:3.0.0
com.google.code.findbugs:annotations:3.0.0
com.google.code.findbugs:jsr305:3.0.0
org.eclipse.jdt:org.eclipse.jdt.core:3.14.0
org.eclipse.platform:org.eclipse.core.commands:3.9.100
org.eclipse.platform:org.eclipse.core.contenttype:3.7.0
org.eclipse.platform:org.eclipse.core.jobs:3.10.0
org.eclipse.platform:org.eclipse.core.resources:3.13.0
org.eclipse.platform:org.eclipse.core.runtime:3.14.0
org.eclipse.platform:org.eclipse.equinox.app:1.3.500
org.eclipse.platform:org.eclipse.equinox.common:3.10.0
org.eclipse.platform:org.eclipse.equinox.preferences:3.7.100
org.eclipse.platform:org.eclipse.equinox.registry:3.8.0
org.eclipse.platform:org.eclipse.osgi:3.13.0
org.eclipse.platform:org.eclipse.text:3.6.300
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class EclipseJdtFormatterStepTest extends EclipseCommonTests {

@Override
protected String[] getSupportedVersions() {
return new String[]{"4.6.1", "4.6.3", "4.7.0", "4.7.1", "4.7.2"};
return new String[]{"4.6.1", "4.6.3", "4.7.0", "4.7.1", "4.7.2", "4.8.0"};
}

@Override
Expand Down
8 changes: 6 additions & 2 deletions lib/src/main/java/com/diffplug/spotless/JarState.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
public final class JarState implements Serializable {
private static final long serialVersionUID = 1L;

@SuppressWarnings("unused")
private final Set<String> mavenCoordinates;
private final TreeSet<String> mavenCoordinates;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I almost missed this one. There are corner-cases where classpath order matters. Unless we have a really compelling reason, I think we should keep this as Set and not TreeSet

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanted to emphasize that I expect a deterministic result for getMavenCoordinates for the same input. I do not insist. What's your final decision?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then feel free to press the merge button, and I'll test the master integration build on some of my projects

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, THX.

@SuppressWarnings("unused")
private final FileSignature fileSignature;

Expand Down Expand Up @@ -92,4 +91,9 @@ URL[] jarUrls() {
public ClassLoader getClassLoader() {
return SpotlessCache.instance().classloader(this);
}

/** Returns unmodifiable view on sorted Maven coordinates */
public Set<String> getMavenCoordinates() {
return Collections.unmodifiableSet(mavenCoordinates);
}
}