Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Howto: Register Annotation Providers

< To: Tigerstripe_APIs

Introduction

With an Annotation Framework you can annotate any object which can be identified with the URI (annotated objects). By default, framework provide two types of the annotated objects. There are resource (files, folders, etc.) and java elements (packages, classes, methods, etc.)

To contribute new annotated objects class you need to provide two operations:

  • Get URI by annotated object with an adapter factory.
  • Get annotated object by URI with an annotation provider.

We looks how annotated objects can be contributed by the example of resource annotated objects.

Get URI by annotated object

At this step we need to adapt org.eclipse.core.resources.IResource (in our example) to the org.eclipse.tigerstripe.annotation.core.IAnnotable with an adapter factory. The IAnnotable interface uses to mark annotable objects. With an adapter factory this interface can not be implemented directly by annotated object. Adapter Factory should be registered using Eclipse Core:

 <extension point="org.eclipse.core.runtime.adapters">
    <factory
          adaptableType="org.eclipse.core.resources.IResource"
          class="org.eclipse.tigerstripe.annotation.resource.ResourceAdapterFactory">
          <adapter type="org.eclipse.tigerstripe.annotation.core.IAnnotable"/>
    </factory>
 </extension>

ResourceAdapterFactory get URI by IResource and return new Annotable object with the following URI.

 public class ResourceAdapterFactory implements IAdapterFactory {
   public Object getAdapter(Object adaptableObject, Class adapterType) {
       if (IAnnotable.class.equals(adapterType)) {
           if (adaptableObject instanceof IResource) {
               IResource resource = (IResource)adaptableObject;
               URI uri = ResourceURIConverter.toURI(resource);
               if (uri != null)
                   return new Annotable(uri);
           }
       }
       return null;
   }
   public Class<?>[] getAdapterList() {
       return new Class[] { IAnnotable.class };
   }
 }

URI can be created from the IResource path and with the specific "resource" scheme:

 return URI.createHierarchicalURI("resource", null, null, resource.getFullPath().segments(), null, null);

Get annotated object by URI

To get annotated object by URI we need to use annotation provider. It can be contributed using "org.eclipse.tigerstripe.annotation.core.annotationProvider" extension point:

 <extension
        point="org.eclipse.tigerstripe.annotation.core.annotationProvider">
     <provider
         id="org.eclipse.tigerstripe.annotation.resource.provider"
         class="org.eclipse.tigerstripe.annotation.resource.ResourceAnnotationProvider"/>
 </extension>

This extension point provide a resource provider class which implements org.eclipse.tigerstripe.annotation.core.IAnnotationProvider interface:

 public class ResourceAnnotationProvider implements IAnnotationProvider {
     public Object getObject(URI uri) {
         return ResourceURIConverter.toResource(uri);
     }
 }

To get IResource from the URI we can used following code:

 if ("resource".equals(uri.scheme())) {
     IPath path = new Path(uri.path());
     return ResourcesPlugin.getWorkspace().getRoot().findMember(path);
 }
 return null;

Back to the top