18
18
import java .io .IOException ;
19
19
import java .io .Serializable ;
20
20
import java .lang .reflect .Constructor ;
21
- import java .lang .reflect .InvocationTargetException ;
22
- import java .lang .reflect .Method ;
23
- import java .lang .reflect .Proxy ;
24
21
import java .util .*;
25
22
26
23
import javax .annotation .Nullable ;
@@ -33,10 +30,9 @@ public class DiktatStep {
33
30
// prevent direct instantiation
34
31
private DiktatStep () {}
35
32
36
- private static final String DEFAULT_VERSION = "1.0.1 " ;
33
+ private static final String DEFAULT_VERSION = "1.1.0 " ;
37
34
static final String NAME = "diktat" ;
38
35
static final String PACKAGE_DIKTAT = "org.cqfn.diktat" ;
39
- static final String PACKAGE_KTLINT = "com.pinterest.ktlint" ;
40
36
static final String MAVEN_COORDINATE = PACKAGE_DIKTAT + ":diktat-rules:" ;
41
37
42
38
public static String defaultVersionDiktat () {
@@ -82,8 +78,6 @@ static final class State implements Serializable {
82
78
/** Are the files being linted Kotlin script files. */
83
79
private final boolean isScript ;
84
80
private final @ Nullable FileSignature config ;
85
- private final String pkg ;
86
- private final String pkgKtlint ;
87
81
final JarState jar ;
88
82
private final TreeMap <String , String > userData ;
89
83
@@ -93,96 +87,19 @@ static final class State implements Serializable {
93
87
pkgSet .add (MAVEN_COORDINATE + versionDiktat );
94
88
95
89
this .userData = new TreeMap <>(userData );
96
- this .pkg = PACKAGE_DIKTAT ;
97
- this .pkgKtlint = PACKAGE_KTLINT ;
98
90
this .jar = JarState .from (pkgSet , provisioner );
99
91
this .isScript = isScript ;
100
92
this .config = config ;
101
93
}
102
94
103
95
FormatterFunc createFormat () throws Exception {
104
-
105
- ClassLoader classLoader = jar .getClassLoader ();
106
-
107
- // first, we get the diktat rules
108
96
if (config != null ) {
109
97
System .setProperty ("diktat.config.path" , config .getOnlyFile ().getAbsolutePath ());
110
98
}
111
99
112
- Class <?> ruleSetProviderClass = classLoader .loadClass (pkg + ".ruleset.rules.DiktatRuleSetProvider" );
113
- Object diktatRuleSet = ruleSetProviderClass .getMethod ("get" ).invoke (ruleSetProviderClass .newInstance ());
114
- Iterable <?> ruleSets = Collections .singletonList (diktatRuleSet );
115
-
116
- // next, we create an error callback which throws an assertion error when the format is bad
117
- Class <?> function2Interface = classLoader .loadClass ("kotlin.jvm.functions.Function2" );
118
- Class <?> lintErrorClass = classLoader .loadClass (pkgKtlint + ".core.LintError" );
119
- Method detailGetter = lintErrorClass .getMethod ("getDetail" );
120
- Method lineGetter = lintErrorClass .getMethod ("getLine" );
121
- Method colGetter = lintErrorClass .getMethod ("getCol" );
122
-
123
- // grab the KtLint singleton
124
- Class <?> ktlintClass = classLoader .loadClass (pkgKtlint + ".core.KtLint" );
125
- Object ktlint = ktlintClass .getDeclaredField ("INSTANCE" ).get (null );
126
-
127
- Class <?> paramsClass = classLoader .loadClass (pkgKtlint + ".core.KtLint$Params" );
128
- // and its constructor
129
- Constructor <?> constructor = paramsClass .getConstructor (
130
- /* fileName, nullable */ String .class ,
131
- /* text */ String .class ,
132
- /* ruleSets */ Iterable .class ,
133
- /* userData */ Map .class ,
134
- /* callback */ function2Interface ,
135
- /* script */ boolean .class ,
136
- /* editorConfigPath, nullable */ String .class ,
137
- /* debug */ boolean .class );
138
- Method formatterMethod = ktlintClass .getMethod ("format" , paramsClass );
139
- FormatterFunc .NeedsFile formatterFunc = (input , file ) -> {
140
- ArrayList <Object > errors = new ArrayList <>();
141
-
142
- Object formatterCallback = Proxy .newProxyInstance (classLoader , new Class []{function2Interface },
143
- (proxy , method , args ) -> {
144
- Object lintError = args [0 ]; //ktlint.core.LintError
145
- boolean corrected = (Boolean ) args [1 ];
146
- if (!corrected ) {
147
- errors .add (lintError );
148
- }
149
- return null ;
150
- });
151
-
152
- userData .put ("file_path" , file .getAbsolutePath ());
153
- try {
154
- Object params = constructor .newInstance (
155
- /* fileName, nullable */ file .getName (),
156
- /* text */ input ,
157
- /* ruleSets */ ruleSets ,
158
- /* userData */ userData ,
159
- /* callback */ formatterCallback ,
160
- /* script */ isScript ,
161
- /* editorConfigPath, nullable */ null ,
162
- /* debug */ false );
163
- String result = (String ) formatterMethod .invoke (ktlint , params );
164
- if (!errors .isEmpty ()) {
165
- StringBuilder error = new StringBuilder ("" );
166
- error .append ("There are " ).append (errors .size ()).append (" unfixed errors:" );
167
- for (Object er : errors ) {
168
- String detail = (String ) detailGetter .invoke (er );
169
- int line = (Integer ) lineGetter .invoke (er );
170
- int col = (Integer ) colGetter .invoke (er );
171
-
172
- error .append (System .lineSeparator ()).append ("Error on line: " ).append (line ).append (", column: " ).append (col ).append (" cannot be fixed automatically" )
173
- .append (System .lineSeparator ()).append (detail );
174
- }
175
- throw new AssertionError (error );
176
- }
177
- return result ;
178
- } catch (InvocationTargetException e ) {
179
- throw ThrowingEx .unwrapCause (e );
180
- }
181
- };
182
-
183
- return formatterFunc ;
100
+ Class <?> formatterFunc = jar .getClassLoader ().loadClass ("com.diffplug.spotless.glue.diktat.DiktatFormatterFunc" );
101
+ Constructor <?> constructor = formatterFunc .getConstructor (boolean .class , Map .class );
102
+ return (FormatterFunc .NeedsFile ) constructor .newInstance (isScript , userData );
184
103
}
185
-
186
104
}
187
-
188
105
}
0 commit comments