1818import java .io .IOException ;
1919import java .io .Serializable ;
2020import java .lang .reflect .Constructor ;
21- import java .lang .reflect .InvocationTargetException ;
22- import java .lang .reflect .Method ;
23- import java .lang .reflect .Proxy ;
2421import java .util .*;
2522
2623import javax .annotation .Nullable ;
@@ -33,10 +30,9 @@ public class DiktatStep {
3330 // prevent direct instantiation
3431 private DiktatStep () {}
3532
36- private static final String DEFAULT_VERSION = "1.0.1 " ;
33+ private static final String DEFAULT_VERSION = "1.1.0 " ;
3734 static final String NAME = "diktat" ;
3835 static final String PACKAGE_DIKTAT = "org.cqfn.diktat" ;
39- static final String PACKAGE_KTLINT = "com.pinterest.ktlint" ;
4036 static final String MAVEN_COORDINATE = PACKAGE_DIKTAT + ":diktat-rules:" ;
4137
4238 public static String defaultVersionDiktat () {
@@ -82,8 +78,6 @@ static final class State implements Serializable {
8278 /** Are the files being linted Kotlin script files. */
8379 private final boolean isScript ;
8480 private final @ Nullable FileSignature config ;
85- private final String pkg ;
86- private final String pkgKtlint ;
8781 final JarState jar ;
8882 private final TreeMap <String , String > userData ;
8983
@@ -93,96 +87,19 @@ static final class State implements Serializable {
9387 pkgSet .add (MAVEN_COORDINATE + versionDiktat );
9488
9589 this .userData = new TreeMap <>(userData );
96- this .pkg = PACKAGE_DIKTAT ;
97- this .pkgKtlint = PACKAGE_KTLINT ;
9890 this .jar = JarState .from (pkgSet , provisioner );
9991 this .isScript = isScript ;
10092 this .config = config ;
10193 }
10294
10395 FormatterFunc createFormat () throws Exception {
104-
105- ClassLoader classLoader = jar .getClassLoader ();
106-
107- // first, we get the diktat rules
10896 if (config != null ) {
10997 System .setProperty ("diktat.config.path" , config .getOnlyFile ().getAbsolutePath ());
11098 }
11199
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 );
184103 }
185-
186104 }
187-
188105}
0 commit comments