Skip to content

Commit 4b04f33

Browse files
committed
Support IDEA 2020.1
Closes gh-180
1 parent e8039de commit 4b04f33

File tree

2 files changed

+41
-6
lines changed
  • spring-javaformat-intellij

2 files changed

+41
-6
lines changed

spring-javaformat-intellij/spring-javaformat-intellij-plugin/src/main/java/io/spring/format/formatter/intellij/SpringFormatComponent.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.spring.format.formatter.intellij;
1818

19+
import java.lang.reflect.Method;
1920
import java.util.concurrent.locks.Lock;
2021
import java.util.concurrent.locks.ReentrantLock;
2122

@@ -26,10 +27,11 @@
2627
import com.intellij.openapi.application.ApplicationManager;
2728
import com.intellij.openapi.components.ProjectComponent;
2829
import com.intellij.openapi.diagnostic.Logger;
30+
import com.intellij.openapi.extensions.PluginDescriptor;
2931
import com.intellij.openapi.extensions.PluginId;
3032
import com.intellij.openapi.project.Project;
3133
import com.intellij.psi.codeStyle.CodeStyleManager;
32-
import com.intellij.serviceContainer.PlatformComponentManagerImpl;
34+
import com.intellij.serviceContainer.ComponentManagerImpl;
3335
import org.picocontainer.MutablePicoContainer;
3436

3537
import 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
}

spring-javaformat-intellij/spring-javaformat-intellij-runtime/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
<name>Spring JavaFormat IntelliJ Runtime</name>
1414
<properties>
1515
<main.basedir>${basedir}/../..</main.basedir>
16-
<intellij.binary>https://download.jetbrains.com/idea/ideaIC-2019.3.1.tar.gz</intellij.binary>
17-
<intellij.source>https://github.com/JetBrains/intellij-community/archive/idea/193.5662.53.zip</intellij.source>
18-
<intellij.root>idea-IC-193.5662.53</intellij.root>
16+
<intellij.binary>https://download.jetbrains.com/idea/ideaIC-2020.1.tar.gz</intellij.binary>
17+
<intellij.source>https://github.com/JetBrains/intellij-community/archive/idea/201.6668.121.zip</intellij.source>
18+
<intellij.root>idea-IC-201.6668.121</intellij.root>
1919
</properties>
2020
<build>
2121
<plugins>

0 commit comments

Comments
 (0)