1
1
/*
2
- * Copyright 2016-2021 DiffPlug
2
+ * Copyright 2016-2022 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.
18
18
import java .io .File ;
19
19
import java .io .IOException ;
20
20
import java .io .Serializable ;
21
- import java .lang .reflect .Method ;
22
- import java .nio .charset .StandardCharsets ;
23
- import java .nio .file .Files ;
21
+ import java .lang .reflect .Constructor ;
24
22
import java .util .Collections ;
25
- import java .util .Objects ;
26
- import java .util .regex .Matcher ;
27
- import java .util .regex .Pattern ;
28
23
29
24
import javax .annotation .Nullable ;
30
25
@@ -39,23 +34,17 @@ public class ScalaFmtStep {
39
34
// prevent direct instantiation
40
35
private ScalaFmtStep () {}
41
36
42
- private static final Pattern VERSION_PRE_2_0 = Pattern .compile ("[10]\\ .(\\ d+)\\ .\\ d+" );
43
- private static final Pattern VERSION_PRE_3_0 = Pattern .compile ("2\\ .(\\ d+)\\ .\\ d+" );
44
- private static final String DEFAULT_VERSION = "3.0.8" ;
37
+ private static final String DEFAULT_VERSION = "3.5.9" ;
45
38
static final String NAME = "scalafmt" ;
46
- static final String MAVEN_COORDINATE_PRE_2_0 = "com.geirsson:scalafmt-core_2.11:" ;
47
- static final String MAVEN_COORDINATE_PRE_3_0 = "org.scalameta:scalafmt-core_2.11:" ;
48
39
static final String MAVEN_COORDINATE = "org.scalameta:scalafmt-core_2.13:" ;
49
40
50
41
public static FormatterStep create (Provisioner provisioner ) {
51
42
return create (defaultVersion (), provisioner , null );
52
43
}
53
44
54
45
public static FormatterStep create (String version , Provisioner provisioner , @ Nullable File configFile ) {
55
- Objects .requireNonNull (version , "version" );
56
- Objects .requireNonNull (provisioner , "provisioner" );
57
46
return FormatterStep .createLazy (NAME ,
58
- () -> new State (version , provisioner , configFile ),
47
+ () -> new State (JarState . from ( MAVEN_COORDINATE + version , provisioner ) , configFile ),
59
48
State ::createFormat );
60
49
}
61
50
@@ -69,78 +58,16 @@ static final class State implements Serializable {
69
58
final JarState jarState ;
70
59
final FileSignature configSignature ;
71
60
72
- State (String version , Provisioner provisioner , @ Nullable File configFile ) throws IOException {
73
- String mavenCoordinate ;
74
- Matcher versionMatcher ;
75
- if ((versionMatcher = VERSION_PRE_2_0 .matcher (version )).matches ()) {
76
- mavenCoordinate = MAVEN_COORDINATE_PRE_2_0 ;
77
- } else if ((versionMatcher = VERSION_PRE_3_0 .matcher (version )).matches ()) {
78
- mavenCoordinate = MAVEN_COORDINATE_PRE_3_0 ;
79
- } else {
80
- mavenCoordinate = MAVEN_COORDINATE ;
81
- }
82
-
83
- this .jarState = JarState .from (mavenCoordinate + version , provisioner );
61
+ State (JarState jarState , @ Nullable File configFile ) throws IOException {
62
+ this .jarState = jarState ;
84
63
this .configSignature = FileSignature .signAsList (configFile == null ? Collections .emptySet () : Collections .singleton (configFile ));
85
64
}
86
65
87
66
FormatterFunc createFormat () throws Exception {
88
- ClassLoader classLoader = jarState .getClassLoader ();
89
-
90
- // scalafmt returns instances of formatted, we get result by calling get()
91
- Class <?> formatted = classLoader .loadClass ("org.scalafmt.Formatted" );
92
- Method formattedGet = formatted .getMethod ("get" );
93
-
94
- // this is how we actually do a format
95
- Class <?> scalafmt = classLoader .loadClass ("org.scalafmt.Scalafmt" );
96
- Class <?> scalaSet = classLoader .loadClass ("scala.collection.immutable.Set" );
97
-
98
- Object defaultScalaFmtConfig = scalafmt .getMethod ("format$default$2" ).invoke (null );
99
- Object emptyRange = scalafmt .getMethod ("format$default$3" ).invoke (null );
100
- Method formatMethod = scalafmt .getMethod ("format" , String .class , defaultScalaFmtConfig .getClass (), scalaSet );
101
-
102
- // now we just need to parse the config, if any
103
- Object config ;
104
- if (configSignature .files ().isEmpty ()) {
105
- config = defaultScalaFmtConfig ;
106
- } else {
107
- File file = configSignature .getOnlyFile ();
108
-
109
- Class <?> optionCls = classLoader .loadClass ("scala.Option" );
110
- Class <?> configCls = classLoader .loadClass ("org.scalafmt.config.Config" );
111
- Class <?> scalafmtCls = classLoader .loadClass ("org.scalafmt.Scalafmt" );
112
-
113
- Object configured ;
114
-
115
- try {
116
- // scalafmt >= 1.6.0
117
- Method parseHoconConfig = scalafmtCls .getMethod ("parseHoconConfig" , String .class );
118
-
119
- String configStr = new String (Files .readAllBytes (file .toPath ()), StandardCharsets .UTF_8 );
120
-
121
- configured = parseHoconConfig .invoke (null , configStr );
122
- } catch (NoSuchMethodException e ) {
123
- // scalafmt >= v0.7.0-RC1 && scalafmt < 1.6.0
124
- Method fromHocon = configCls .getMethod ("fromHoconString" , String .class , optionCls );
125
- Object fromHoconEmptyPath = configCls .getMethod ("fromHoconString$default$2" ).invoke (null );
126
-
127
- String configStr = new String (Files .readAllBytes (file .toPath ()), StandardCharsets .UTF_8 );
128
-
129
- configured = fromHocon .invoke (null , configStr , fromHoconEmptyPath );
130
- }
131
-
132
- config = invokeNoArg (configured , "get" );
133
- }
134
- return input -> {
135
- Object resultInsideFormatted = formatMethod .invoke (null , input , config , emptyRange );
136
- return (String ) formattedGet .invoke (resultInsideFormatted );
137
- };
67
+ final ClassLoader classLoader = jarState .getClassLoader ();
68
+ final Class <?> formatterFunc = classLoader .loadClass ("com.diffplug.spotless.glue.scalafmt.ScalafmtFormatterFunc" );
69
+ final Constructor <?> constructor = formatterFunc .getConstructor (FileSignature .class );
70
+ return (FormatterFunc ) constructor .newInstance (this .configSignature );
138
71
}
139
72
}
140
-
141
- private static Object invokeNoArg (Object obj , String toInvoke ) throws Exception {
142
- Class <?> clazz = obj .getClass ();
143
- Method method = clazz .getMethod (toInvoke );
144
- return method .invoke (obj );
145
- }
146
73
}
0 commit comments