Skip to content

Commit f99e8c7

Browse files
authored
fix: use reflection to access MavenId to avoid binary incompatibility with IDEA 261+ (#1279)
MavenHelper.getMavenIdByMaven() previously accessed mavenProject.mavenId directly, which references org.jetbrains.idea.maven.model.MavenId in its bytecode. In IntelliJ IDEA IU-261.22158.121, this class is no longer resolvable from the plugin classloader, causing NoSuchClassError at runtime. This fix uses reflection to call getMavenId() and access groupId/artifactId/ version properties, avoiding any compile-time reference to the MavenId class.
1 parent df083df commit f99e8c7

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

idea-plugin/src/main/kotlin/com/itangcent/idea/utils/MavenHelper.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ object MavenHelper {
3232

3333
/**
3434
* Retrieves the Maven ID of a PsiClass using Maven.
35+
* Uses reflection to access MavenId to avoid binary incompatibility
36+
* with newer IntelliJ versions where MavenId class may be in a different classloader.
3537
*
3638
* @param psiClass the PsiClass for which the Maven ID is to be retrieved.
3739
* @return MavenIdData if found, null otherwise.
@@ -43,11 +45,16 @@ object MavenHelper {
4345

4446
val mavenProjectsManager = MavenProjectsManager.getInstance(project)
4547
val mavenProject = mavenProjectsManager.findProject(module) ?: return null
46-
val mavenId = mavenProject.mavenId
48+
// Use reflection to access mavenId and its properties to avoid direct dependency on
49+
// org.jetbrains.idea.maven.model.MavenId which may not be resolvable in some IDEA versions
50+
val mavenId = mavenProject.javaClass.getMethod("getMavenId").invoke(mavenProject) ?: return null
51+
val groupId = mavenId.javaClass.getMethod("getGroupId").invoke(mavenId) as? String ?: return null
52+
val artifactId = mavenId.javaClass.getMethod("getArtifactId").invoke(mavenId) as? String ?: return null
53+
val version = mavenId.javaClass.getMethod("getVersion").invoke(mavenId) as? String ?: return null
4754
return MavenIdData(
48-
groupId = mavenId.groupId!!,
49-
artifactId = mavenId.artifactId!!,
50-
version = mavenId.version!!
55+
groupId = groupId,
56+
artifactId = artifactId,
57+
version = version
5158
)
5259
}
5360

0 commit comments

Comments
 (0)