2

I'm developing an Eclipse plugin in which I want to overwrite the functionality of a method contained in a class that is in the Eclipse library.

What I've tried so far is creating an identical (same package and file names) file in my plugin source, and making the changes I want there. Then, I set the build path order such that my source is above the Plug-in Dependencies. Based on my limited understanding, this should mean that when Java looks for that class, it should use mine over the one in the library.

However, this is not working. The behavior that I want to override is not changing, and I don't see the print statements I put in my code either.

How can I "replace" a class in the Eclipse library with one of my own?

Charles
  • 4,372
  • 9
  • 41
  • 80
  • you can extend that class and override all the functions. – Shantanu Jul 14 '16 at 15:45
  • @shatanu Won't that only change the behavior of methods called on instances of that subclass? The behavior of the superclass (the one I want to modify, and the one that is being called in the library) would remain the same. – Charles Jul 14 '16 at 15:48
  • yaa that is true but as you stated "I want to overwrite the functionality of a method contained in a class that is in the Eclipse library" you can achieve that. Otherwise if the library is open source then you can edit it as you want. – Shantanu Jul 14 '16 at 15:52
  • @shantanu I have tried cloning the entire eclipse.platform.text codebase, but I'm not exactly sure how to integrate it within my plugin project. I tried exporting as JAR, but one issue I ran into was that it seems like the version that I downloaded requires Java 8, while I am using Java 7 for the plugin. – Charles Jul 14 '16 at 15:57
  • you can always update your java version – Shantanu Jul 14 '16 at 15:59
  • @shantanu The requirements of the project are that it be developed using Java 7. – Charles Jul 14 '16 at 15:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117346/discussion-between-shantanu-and-charles). – Shantanu Jul 14 '16 at 16:01
  • While you might succeed in changing the behavior in your plugin you will not be able to change the code used by any other plugin which will always use the standard Eclipse code. – greg-449 Jul 14 '16 at 16:29
  • Why do you want to do that? – Aleksandr M Jul 14 '16 at 17:09
  • @AleksandrM There is an issue in the library that I want to resolve. – Charles Jul 14 '16 at 17:29
  • 1
    Eclipse is open source. How about providing patch for this issue. – Aleksandr M Jul 14 '16 at 19:01
  • 1
    @AleksandrM I have submitted a bug report and a suggested fix, but we want this feature in our code as soon as possible. I am not sure how long it will take for proper code review and the feature to be integrated into a release. – Charles Jul 14 '16 at 19:49
  • Yes, of course. My comment was just a comment. :) – Aleksandr M Jul 15 '16 at 09:11

2 Answers2

0

I did it once (not proud of it :-)) in the following way:

  1. Import the plugin you wish to hack by Import->Plugin Development->Plug-ins and Fragments (Import as Projects with source folders).
  2. Set the project to build automatically, edit the file and find its resulting class file.
  3. Open the jar of the plugin (the one containing its class files), inject your class file instead of the original one.
  4. If jar file is signed remove all signature information from MANIFEST.MF (and maybe other files).

I admit it's ugly but it's the best way I've found.

Arye Shemesh
  • 582
  • 7
  • 20
0

I ended up using the JVM JavaAgent to achieve this, by overriding the class loader and loading in my own class to replace the one in the library.

This was a useful tutorial for me: https://stackoverflow.com/a/11898653/634324

Community
  • 1
  • 1
Charles
  • 4,372
  • 9
  • 41
  • 80