1
1
/*
2
- * Copyright 2016-2020 DiffPlug
2
+ * Copyright 2016-2021 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package com .diffplug .spotless .extra .wtp ;
17
17
18
+ import java .io .File ;
18
19
import java .lang .reflect .InvocationTargetException ;
19
20
import java .lang .reflect .Method ;
20
21
import java .util .Properties ;
21
22
22
23
import com .diffplug .spotless .FormatterFunc ;
24
+ import com .diffplug .spotless .Jvm ;
23
25
import com .diffplug .spotless .Provisioner ;
24
26
import com .diffplug .spotless .ThrowingEx ;
25
27
import com .diffplug .spotless .extra .EclipseBasedStepBuilder ;
@@ -36,7 +38,7 @@ public enum EclipseWtpFormatterStep {
36
38
37
39
private static final String NAME = "eclipse wtp formatters" ;
38
40
private static final String FORMATTER_PACKAGE = "com.diffplug.spotless.extra.eclipse.wtp." ;
39
- private static final String DEFAULT_VERSION = "4.18.0" ;
41
+ private static final Jvm . Support < String > JVM_SUPPORT = Jvm .< String > support ( NAME ). add ( 8 , "4.18.0" ). add ( 11 , "4.20.0" ) ;
40
42
private static final String FORMATTER_METHOD = "format" ;
41
43
42
44
private final String implementationClassName ;
@@ -52,10 +54,11 @@ public EclipseBasedStepBuilder createBuilder(Provisioner provisioner) {
52
54
}
53
55
54
56
public static String defaultVersion () {
55
- return DEFAULT_VERSION ;
57
+ return JVM_SUPPORT . getRecommendedFormatterVersion () ;
56
58
}
57
59
58
60
private static FormatterFunc applyWithoutFile (String className , EclipseBasedStepBuilder .State state ) throws Exception {
61
+ JVM_SUPPORT .assertFormatterSupported (state .getSemanticVersion ());
59
62
Class <?> formatterClazz = state .loadClass (FORMATTER_PACKAGE + className );
60
63
Object formatter = formatterClazz .getConstructor (Properties .class ).newInstance (state .getPreferences ());
61
64
Method method = formatterClazz .getMethod (FORMATTER_METHOD , String .class );
@@ -70,18 +73,22 @@ private static FormatterFunc applyWithoutFile(String className, EclipseBasedStep
70
73
};
71
74
}
72
75
73
- private static FormatterFunc .NeedsFile applyWithFile (String className , EclipseBasedStepBuilder .State state ) throws Exception {
76
+ private static FormatterFunc applyWithFile (String className , EclipseBasedStepBuilder .State state ) throws Exception {
77
+ JVM_SUPPORT .assertFormatterSupported (state .getSemanticVersion ());
74
78
Class <?> formatterClazz = state .loadClass (FORMATTER_PACKAGE + className );
75
79
Object formatter = formatterClazz .getConstructor (Properties .class ).newInstance (state .getPreferences ());
76
80
Method method = formatterClazz .getMethod (FORMATTER_METHOD , String .class , String .class );
77
- return (unixString , file ) -> {
78
- try {
79
- return (String ) method .invoke (formatter , unixString , file .getAbsolutePath ());
80
- } catch (InvocationTargetException exceptionWrapper ) {
81
- Throwable throwable = exceptionWrapper .getTargetException ();
82
- Exception exception = (throwable instanceof Exception ) ? (Exception ) throwable : null ;
83
- throw (null == exception ) ? exceptionWrapper : exception ;
81
+ return JVM_SUPPORT .suggestLaterVersionOnError (state .getSemanticVersion (), new FormatterFunc .NeedsFile () {
82
+ @ Override
83
+ public String applyWithFile (String unix , File file ) throws Exception {
84
+ try {
85
+ return (String ) method .invoke (formatter , unix , file .getAbsolutePath ());
86
+ } catch (InvocationTargetException exceptionWrapper ) {
87
+ Throwable throwable = exceptionWrapper .getTargetException ();
88
+ Exception exception = (throwable instanceof Exception ) ? (Exception ) throwable : null ;
89
+ throw (null == exception ) ? exceptionWrapper : exception ;
90
+ }
84
91
}
85
- };
92
+ }) ;
86
93
}
87
94
}
0 commit comments