0

I would like to clear the Eclipse e4 cache (The .metadata directory) during runtime.

There are lots of posts for clearing the cache by setting the checkbox in the run configurations but I can't find anything on clearing the cache in the code.

I'd prefer to use a method that has already been written (if there is one) in comparison to writing my own.

If I was to do this myself then I'll do it during @PostContextCreate in the life cycle manager.

Is there a method that will do this for me or should I just delete the cache directory?

Update Here is the issue I'm trying to work around.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=430090#add_comment

Michael
  • 3,411
  • 4
  • 25
  • 56
  • The .metadata contains all sorts of different things belonging to many different plug-ins. What exactly do you want to clear? – greg-449 Jun 27 '19 at 12:12
  • The layout for all of the perspectives/views. The equivalent of clicking the `Clear` checkbox in the run configuration but instead at runtime. – Michael Jun 27 '19 at 12:26
  • @Michael There are two _Clear_ checkboxes in an Eclipse application launch configuration: one in the _Main_ tab to clear the workspace (containing the `.metadata` subfolder) and another in the _Configuration_ tab to clear the configuration area (which is not related to the `.metadata` folder). The `.metadata` contains not only caches, but also preferences, locale file history, not shared launch configurations, etc. which might be in use at run time. A perspective can be reset. Is this what you want to do? – howlger Jun 27 '19 at 12:42
  • The saved application model can be cleared with just the `-clearPersistedState` command line option or using `-persistState false` to stop it being saved in the first place. I'm not sure of an API to do that, but `@PostContextCreate` might be too late during the startup anyway to clear this. – greg-449 Jun 27 '19 at 12:42
  • The problem is that we might add a new view to the application. Resetting the perspective wouldn't update this would it? I can't use -clearPersistedState as it is always called when the application starts. I need it to clear the cache whenever we update the application so I need to check if it has been updated during runtime. – Michael Jun 27 '19 at 12:43
  • Currently I have created a `ModelResourceHandler`. When RCP tries to get the cache file I'm checking to see if the application has been updated. If it has been I would like to delete the layout cache. If not then I'd like it to load the old one. – Michael Jun 27 '19 at 12:46
  • Instead of deleting the whole `.metadata` I was meaning I'd delete `.metadata\.plugins\org.eclipse.e4.workbench\workbench.xmi` – Michael Jun 27 '19 at 12:49
  • A new view will be show in an existing perspective when using [`org.eclipse.ui.perspectiveExtensions`](https://help.eclipse.org/2019-06/topic/org.eclipse.platform.doc.isv/guide/workbench_advext_perspectiveExtension.htm?cp=2_0_12_2_1), won't it? – howlger Jun 27 '19 at 12:54
  • I've updated my question with a link to the issue I'm trying to work around. – Michael Jun 27 '19 at 12:58
  • 1
    Deleting the workbench.xmi looks like it would work, I don't think there is any better way currently. E4Application loads the application model immediately after the @PostContextCreate call. – greg-449 Jun 27 '19 at 13:02
  • Yeah looks like the best way to me. If you'd like to answer the question with that I'll accept :) If not let me know and I'll add it. – Michael Jun 27 '19 at 13:14

2 Answers2

0

To clear the cache at runtime I've overridden the ResourceHandler and added this to loadMostRecentModel.

final Method m = getClass().getSuperclass().getDeclaredMethod("getWorkbenchSaveLocation", new Class<?>[] {});
m.setAccessible(true);
final File workbenchSaveLocation = (File) m.invoke(this, (Object[]) null);
workbenchSaveLocation.delete();  

I use reflection as the parent method is private. It would be better to do this instead of writing code to get the file myself as it ensures I always get the correct location.

Michael
  • 3,411
  • 4
  • 25
  • 56
0

First, deleting .metadata folder can damage the user data: preferences, launch configs, who knows what else - it depends on particular plug-in implementation.

Also your updates may contain new bundles and fragments with new services and extension. And the user may rearrange views and do other things persisted with workbench model.

=>

The deletion of workbench model will not resolve all the issues, please consider the following: