Depend on plugin jar or copy sources to custom plugin

Answered

Hi,

I'm developing a plugin and I am using the junit-rt.jar plugin from Intellij IDEA. I am using some methods from classes present in this jar but some of these methods are private. My question is: should I use reflection to call those methods or should I copy the source code of these methods into my custom plugin and use them like they were part of my plugin itself?

The problem with reflection, in my opinion, is that the private methods can be changed inside these classes(their name or signature can change). In this case my plugin won't work. On the other hand, if I copy the source code everything will work even if the private methods are changed, BUT in this case:

  1. I don't reuse existing code
  2. Problems can arise from the fact that different versions of Intellij use the same JUnit plugin code(I am not sure about this one??)
  3. if there are some bug fixes, then I will have to manually apply them to my sources

Also, there is a third option, which is the best in my opinion: Copy the whole classes which I want to use into my custom plugin, but DO not modify anything in the source code. Then, change every method to be public or package-private and extend these classes and override only those methods which need to be changed.

Advantages:

  1. Applying bug fixes manually is a breeze because I do not have classes mixed with my own code and plugin code at the same time. So, I can just copy-paste the new source code from the Github repo and hope that it works. If it doesn't work, no worries, I can just change my custom plugin a little bit to make it work

Disadvantage:

  1. Same as second solution.

 

UPDATE: after checking the JUnitStarter code I've found that its source code has changed considerably since 2010. For example:

Code from my installation:

private static int prepareStreamsAndStart(String[] args, String agentName, ArrayList listeners, String name)

Code from 2010:

private static int prepareStreamsAndStart(String[] args, final boolean isJUnit4, ArrayList listeners, SegmentedOutputStream out,
SegmentedOutputStream err)

 

UPDATE2: I've noticed that 4 days ago, the JUnitStarter's package was changed from com.intellij.rt.execution.junit to com.intellij.rt.junit. So, my plugin will break just from this update!!!

4 comments
Comment actions Permalink

Hi,

I don't expect that somebody would use private methods in junit-rt. Please share what your plugin is supposed to do so we could try to make that part somehow extensible. 

Thanks,

Anna

0
Comment actions Permalink

Hi Anna,

sorry for the late reply. My plugin which I was working on is available in the official repository along with its source code: https://plugins.jetbrains.com/plugin/12959-junit4-parallel-runner

So, basically I needed a way to add some custom code to the JUnit4IdeaTestRunner, and to do that I made my own JUnitStarter class which instantiates my own test runner which is a subclass of JUnit4IdeaTestRunner.

 

Thank you,

Csaba

0
Comment actions Permalink

The plugin won't work with 2019.3, sorry. Without renaming, modularised projects won't be able to work and we aim to fix that

My suggestion is to make this functionality (run tests in parallel) as part of junit plugin: create new option in the configuration and pass the parameter to the runner (should be available for junit 4 and disabled for junit 3/5 though). This would make the implementation much easier and more people would be able to try this. A PR is very welcome!

Anna

0
Comment actions Permalink

Hi Anna,

Thank you for your quick response. I've already changed the code. Now it is working with 2019.3(of course I had to build and upload a new version of the plugin specifically targeted at 2019.3+ because I cannot develop a version that is compatible with 2019.3+ and lower versions at the same time) 

 

Thanks,

Csaba

0

Please sign in to leave a comment.