What to put in the index?

As explained earlier in this chapter, everything that can be given a name will have a corresponding entry in the index; moreover, by default, each element of the index can be referred through its fully qualified name. However, only the references that use the qualified name syntax can refer to these elements using the index. In SmallJava, only classes can be referred with qualified names.

The index is also used by Xtext to keep track of dependencies among files and to determine when to rebuild other files when a file changes.

Therefore, it makes no sense to index those elements that cannot be referred from other files. In our DSL, this means that it does not make sense to index variables, since they can only be accessed from a method in the containing class. Instead, we leave the methods and their parameters in the index, because we want the other files using a method of an external class to be notified if the method changes name or parameters. Although the presence of entries in the index for local variables does not harm, still it occupies some memory space uselessly. Moreover, the indexing procedure could be optimized by removing the overhead of indexing useless elements.

To tweak the strategy for building the index we provide a custom implementation of DefaultResourceDescriptionsStrategy and redefine the method createEObjectDescriptions. This method is automatically called by Xtext when the index is built or updated when resources change. This method is expected to return false if the children of the passed EObject must not be processed. In our case, we simply return false when the object is an SJBlock:

@Singleton
class SmallJavaResourceDescriptionsStrategy extends DefaultResourceDescriptionStrategy {

  override createEObjectDescriptions(EObject e,
                  IAcceptor<IEObjectDescription> acceptor) {
    if (e instanceof SJBlock)
      return false
    
else
      return super.createEObjectDescriptions(e, acceptor)
  }
}

Note that this class must be annotated with @Singleton, indicating that only one instance per injector will be used for all injections for this class.

Of course, we bind our implementation in the runtime module:

  def Class<? extends IDefaultResourceDescriptionStrategy>
              bindIDefaultResourceDescriptionStrategy() {
    return SmallJavaResourceDescriptionsStrategy;
  }...

This way, we improve the default indexing behavior.

Tip

If you now run the test method testExportedEObjectDescriptions shown in the section Exported objects, you will see that it fails. You will need to modify it according to the descriptions found in the index after our customization.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.102.225