Skip to content

Commit 44d2948

Browse files
authored
Apply eclipse-jdt-4.8.1(2nd attempt) (#962)
2 parents f22977d + 7f4711c commit 44d2948

File tree

5 files changed

+57
-14
lines changed

5 files changed

+57
-14
lines changed

_ext/eclipse-jdt/src/test/java/com/diffplug/spotless/extra/eclipse/java/EclipseJdtFormatterStepImplTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ static void beforeAll() throws IOException {
4646

4747
@BeforeEach
4848
void beforeEach() throws IOException {
49-
File sttingsFile = createTestFile("java/eclipse/formatter.xml");
50-
config = FormatterProperties.from(sttingsFile).getProperties();
49+
File settingsFile = createTestFile("java/eclipse/formatter.xml");
50+
config = FormatterProperties.from(settingsFile).getProperties();
5151
}
5252

5353
private final static String ILLEGAL_CHAR = Character.toString((char) 254);
@@ -97,10 +97,8 @@ void htmlPreTag() throws Throwable {
9797

9898
@Test
9999
void moduleInfo() throws Throwable {
100-
config.clear();
101-
config.setProperty(
102-
DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_MODULE_STATEMENTS,
103-
DefaultCodeFormatterConstants.createAlignmentValue(true, DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE, DefaultCodeFormatterConstants.INDENT_BY_ONE));
100+
File settingsFile = createTestFile("java/eclipse/ModuleInfo.prefs");
101+
config = FormatterProperties.from(settingsFile).getProperties();
104102
String formatted = getTestResource("java/eclipse/ModuleInfoFormatted.test");
105103
String unformatted = getTestResource("java/eclipse/ModuleInfoUnformatted.test");
106104
assertEquals(formatted, format(unformatted, "whatever/module-info.java"), "Jvm9 module info not formatted.");

lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStep.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless.extra.java;
1717

18+
import java.io.File;
1819
import java.lang.reflect.Method;
1920
import java.util.Properties;
2021

@@ -49,8 +50,7 @@ private static FormatterFunc apply(State state) throws Exception {
4950
JVM_SUPPORT.assertFormatterSupported(state.getSemanticVersion());
5051
Class<?> formatterClazz = getClass(state);
5152
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences());
52-
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
53-
FormatterFunc formatterFunc = getFormatterFunc(formatter, method);
53+
FormatterFunc formatterFunc = getFormatterFunc(formatter, formatterClazz);
5454
return JVM_SUPPORT.suggestLaterVersionOnError(state.getSemanticVersion(), formatterFunc);
5555
}
5656

@@ -61,10 +61,13 @@ private static Class<?> getClass(State state) {
6161
return state.loadClass(FORMATTER_CLASS_OLD);
6262
}
6363

64-
private static FormatterFunc getFormatterFunc(Object formatter, Method method) {
65-
if (1 == method.getParameterCount()) {
64+
private static FormatterFunc getFormatterFunc(Object formatter, Class<?> formatterClazz) throws NoSuchMethodException, SecurityException {
65+
try {
66+
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class, File.class);
67+
return (FormatterFunc.NeedsFile) (input, file) -> (String) method.invoke(formatter, input, file);
68+
} catch (NoSuchMethodException e) {
69+
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
6670
return input -> (String) method.invoke(formatter, input);
6771
}
68-
return (FormatterFunc.NeedsFile) (input, file) -> (String) method.invoke(formatter, input, file);
6972
}
7073
}

lib-extra/src/test/java/com/diffplug/spotless/extra/eclipse/EclipseResourceHarness.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,30 @@
4343
*/
4444
public class EclipseResourceHarness extends ResourceHarness {
4545
private final EclipseBasedStepBuilder stepBuilder;
46+
private final String fileName;
4647
private final String input;
4748
private final String expected;
4849

4950
/**
5051
* Create harness to be used for several versions of the formatter step
5152
* @param builder Eclipse Formatter step builder
5253
* @param unformatted Simple unformatted input
53-
* @param formatted
54+
* @param formatted Expected formatted output
5455
*/
5556
public EclipseResourceHarness(EclipseBasedStepBuilder builder, String unformatted, String formatted) {
57+
this(builder, "someSourceFile", unformatted, formatted);
58+
}
59+
60+
/**
61+
* Create harness to be used for several versions of the formatter step
62+
* @param builder Eclipse Formatter step builder
63+
* @param sourceFileName File name of the source file
64+
* @param unformatted Simple unformatted input
65+
* @param formatted Expected formatted output
66+
*/
67+
public EclipseResourceHarness(EclipseBasedStepBuilder builder, String sourceFileName, String unformatted, String formatted) {
5668
stepBuilder = builder;
69+
fileName = sourceFileName;
5770
input = unformatted;
5871
expected = formatted;
5972
}
@@ -77,7 +90,7 @@ protected String assertFormatted(String formatterVersion, File... settingsFiles)
7790
* @return Formatted string
7891
*/
7992
protected String format(String formatterVersion, File... settingsFiles) throws Exception {
80-
File inputFile = setFile("someInputFile").toContent(input);
93+
File inputFile = setFile(fileName).toContent(input);
8194
stepBuilder.setVersion(formatterVersion);
8295
stepBuilder.setPreferences(Arrays.asList(settingsFiles));
8396
FormatterStep step = stepBuilder.build();

lib-extra/src/test/java/com/diffplug/spotless/extra/java/EclipseJdtFormatterStepTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@
1515
*/
1616
package com.diffplug.spotless.extra.java;
1717

18+
import static org.junit.jupiter.api.condition.JRE.JAVA_11;
19+
20+
import java.io.File;
1821
import java.util.stream.Stream;
1922

23+
import org.junit.jupiter.api.Nested;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.condition.EnabledForJreRange;
2026
import org.junit.jupiter.params.ParameterizedTest;
2127
import org.junit.jupiter.params.provider.MethodSource;
2228

2329
import com.diffplug.spotless.Jvm;
2430
import com.diffplug.spotless.TestProvisioner;
31+
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
2532
import com.diffplug.spotless.extra.eclipse.EclipseResourceHarness;
2633

2734
class EclipseJdtFormatterStepTest extends EclipseResourceHarness {
@@ -30,8 +37,12 @@ class EclipseJdtFormatterStepTest extends EclipseResourceHarness {
3037
private final static String INPUT = "package p; class C{}";
3138
private final static String EXPECTED = "package p;\nclass C {\n}";
3239

40+
private static EclipseBasedStepBuilder createBuilder() {
41+
return EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral());
42+
}
43+
3344
public EclipseJdtFormatterStepTest() {
34-
super(EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral()), INPUT, EXPECTED);
45+
super(createBuilder(), INPUT, EXPECTED);
3546
}
3647

3748
@ParameterizedTest
@@ -43,4 +54,19 @@ void formatWithVersion(String version) throws Exception {
4354
private static Stream<String> formatWithVersion() {
4455
return Stream.of(NON_SEMANTIC_ECLIPSE_VERSION, JVM_SUPPORT.getRecommendedFormatterVersion(), EclipseJdtFormatterStep.defaultVersion());
4556
}
57+
58+
/** New format interface requires source file information to distinguish module-info from compilation unit */
59+
@Nested
60+
@EnabledForJreRange(min = JAVA_11)
61+
class NewFormatInterface extends EclipseResourceHarness {
62+
public NewFormatInterface() throws Exception {
63+
super(createBuilder(), "module-info.java", getTestResource("java/eclipse/ModuleInfoUnformatted.test"), getTestResource("java/eclipse/ModuleInfoFormatted.test"));
64+
}
65+
66+
@Test
67+
void formatModuleInfo() throws Exception {
68+
File settingsFile = createTestFile("java/eclipse/ModuleInfo.prefs");
69+
assertFormatted(JVM_SUPPORT.getRecommendedFormatterVersion(), settingsFile);
70+
}
71+
}
4672
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
eclipse.preferences.version=1
2+
#DefaultCodeFormatterConstants.createAlignmentValue(true, DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE, DefaultCodeFormatterConstants.INDENT_BY_ONE)
3+
org.eclipse.jdt.core.formatter.alignment_for_module_statements=53

0 commit comments

Comments
 (0)