Dat Chu
Dat Chu

Reputation: 11140

Add a specific code path in my plugin to support multiple IntelliJ version

IntelliJ 15 introduces a new method in SimpleJavaParameters class called setUseClasspathJar.

I want my plugin to set call this method if the user is running IntelliJ 15. If the user runs IntelliJ 14.1, the method is not even available (it won't compile).

How do I write my plugin so it will do different things depending on the version when there is a signature change like this?

Upvotes: 0

Views: 55

Answers (1)

Bas Leijdekkers
Bas Leijdekkers

Reputation: 26522

You could only compile on IntelliJ IDEA 15 and guard the call with an if statement. For example:

final BuildNumber build = ApplicationInfo.getInstance().getBuild();
if (build.getBaselineVersion() >= 143) {
    // call setUseClasspathJar() here
}

The build number ranges for different IntelliJ Platform-based products are available here.

Another option would be to use reflection to call the method, if it is available. This is a lot more verbose, but com.intellij.util.ReflectionUtil is available to make it a little easier:

final Method method = 
    ReflectionUtil.getDeclaredMethod(SimpleJavaParameters.class, 
                                     "setUseClasspathJar", boolean.class);
if (method != null) {
  try {
    method.invoke(parameters, true);
  }
  catch (IllegalAccessException e1) {
    throw new RuntimeException(e1);
  }
  catch (InvocationTargetException e1) {
    throw new RuntimeException(e1);
  }
}

Upvotes: 1

Related Questions