16
16
17
17
package io .spring .format .formatter .intellij ;
18
18
19
+ import java .lang .reflect .Method ;
19
20
import java .util .concurrent .locks .Lock ;
20
21
import java .util .concurrent .locks .ReentrantLock ;
21
22
26
27
import com .intellij .openapi .application .ApplicationManager ;
27
28
import com .intellij .openapi .components .ProjectComponent ;
28
29
import com .intellij .openapi .diagnostic .Logger ;
30
+ import com .intellij .openapi .extensions .PluginDescriptor ;
29
31
import com .intellij .openapi .extensions .PluginId ;
30
32
import com .intellij .openapi .project .Project ;
31
33
import com .intellij .psi .codeStyle .CodeStyleManager ;
32
- import com .intellij .serviceContainer .PlatformComponentManagerImpl ;
34
+ import com .intellij .serviceContainer .ComponentManagerImpl ;
33
35
import org .picocontainer .MutablePicoContainer ;
34
36
35
37
import io .spring .format .formatter .intellij .codestyle .SpringCodeStyleManager ;
@@ -112,9 +114,14 @@ private void update(State state) {
112
114
113
115
private void registerCodeStyleManager (CodeStyleManager manager ) {
114
116
if (ApplicationInfo .getInstance ().getBuild ().getBaselineVersion () >= 193 ) {
115
- PlatformComponentManagerImpl platformComponentManager = (PlatformComponentManagerImpl ) this .project ;
116
117
IdeaPluginDescriptor plugin = PluginManagerCore .getPlugin (PluginId .getId ("spring-javaformat" ));
117
- platformComponentManager .registerServiceInstance (CodeStyleManager .class , manager , plugin );
118
+ try {
119
+ ((ComponentManagerImpl ) this .project ).registerServiceInstance (CodeStyleManager .class , manager , plugin );
120
+ }
121
+ catch (NoSuchMethodError ex ) {
122
+ Method method = findRegisterServiceInstanceMethod (this .project .getClass ());
123
+ invokeRegisterServiceInstanceMethod (manager , plugin , method );
124
+ }
118
125
}
119
126
else {
120
127
MutablePicoContainer container = (MutablePicoContainer ) this .project .getPicoContainer ();
@@ -123,4 +130,32 @@ private void registerCodeStyleManager(CodeStyleManager manager) {
123
130
}
124
131
}
125
132
133
+ private Method findRegisterServiceInstanceMethod (Class <?> projectClass ) {
134
+ if (projectClass != null ) {
135
+ Method [] methods = projectClass .getDeclaredMethods ();
136
+ for (Method method : methods ) {
137
+ if (method .getName ().equals ("registerServiceInstance" ) && method .getParameterCount () == 3 ) {
138
+ if (PluginDescriptor .class .isAssignableFrom (method .getParameterTypes ()[2 ])) {
139
+ return method ;
140
+ }
141
+ }
142
+ }
143
+ return findRegisterServiceInstanceMethod (projectClass .getSuperclass ());
144
+ }
145
+ return null ;
146
+ }
147
+
148
+ private void invokeRegisterServiceInstanceMethod (CodeStyleManager manager , IdeaPluginDescriptor plugin ,
149
+ Method method ) {
150
+ if (method == null ) {
151
+ throw new IllegalStateException ("Unsupported IntelliJ version" );
152
+ }
153
+ method .setAccessible (true );
154
+ try {
155
+ method .invoke (this .project , manager , plugin );
156
+ }
157
+ catch (Exception ex ) {
158
+ throw new IllegalStateException (ex );
159
+ }
160
+ }
126
161
}
0 commit comments