1616
1717package io .spring .format .formatter .intellij ;
1818
19+ import java .lang .reflect .Method ;
1920import java .util .concurrent .locks .Lock ;
2021import java .util .concurrent .locks .ReentrantLock ;
2122
2627import com .intellij .openapi .application .ApplicationManager ;
2728import com .intellij .openapi .components .ProjectComponent ;
2829import com .intellij .openapi .diagnostic .Logger ;
30+ import com .intellij .openapi .extensions .PluginDescriptor ;
2931import com .intellij .openapi .extensions .PluginId ;
3032import com .intellij .openapi .project .Project ;
3133import com .intellij .psi .codeStyle .CodeStyleManager ;
32- import com .intellij .serviceContainer .PlatformComponentManagerImpl ;
34+ import com .intellij .serviceContainer .ComponentManagerImpl ;
3335import org .picocontainer .MutablePicoContainer ;
3436
3537import io .spring .format .formatter .intellij .codestyle .SpringCodeStyleManager ;
@@ -112,9 +114,14 @@ private void update(State state) {
112114
113115 private void registerCodeStyleManager (CodeStyleManager manager ) {
114116 if (ApplicationInfo .getInstance ().getBuild ().getBaselineVersion () >= 193 ) {
115- PlatformComponentManagerImpl platformComponentManager = (PlatformComponentManagerImpl ) this .project ;
116117 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+ }
118125 }
119126 else {
120127 MutablePicoContainer container = (MutablePicoContainer ) this .project .getPicoContainer ();
@@ -123,4 +130,32 @@ private void registerCodeStyleManager(CodeStyleManager manager) {
123130 }
124131 }
125132
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+ }
126161}
0 commit comments