xtextxtend

How to clear cache of create methods in Xtend


I am using create methods (Xtend doc) to perform model transformation of languages in Xtext. I need to clear internal cache of create methods, how can I achieve that?


Solution

  • The proper way to get a clean cache would be to create a new instance of the type that contains the create method.

    When doing model transformations, the classes containing create methods often are singletons though. If that's the case here too, I would suggest to use a provider for these classes which returns new instances when appropriate. Or maybe even scoped dependency injection (e.g. a custom Guice scope).


    That said, it is possible to clear a create method's cache. It seems a bit like a hack though, because you need to access generated properties that you aren't really supposed to use directly.

    If you have a create method called createElement then Xtend will generate a private HashMap property with the name _createCache_createElement. This property is accessible from Xtend code. So to clear the cache you can call:

    _createCache_createElement.clear()
    

    Be careful if you have multiple create methods with the same name: It will generate properties like _createCache_createElement_1, _createCache_createElement_2 and so on. You'd have to check which map belongs to which create methods. I guess that if the method signature changes, the numbers may change too. So I would avoid accessing the maps in this case or make the method names distinct. If you are using Eclipse, you can right click in the editor and then "Open generated file" to inspect what names Xtend has generated.