Method Handlers

Bryn Cooke edited this page Jun 30, 2013 · 2 revisions

Note: The MethodHandler interface supercedes the AnnotationHandler interface from Frames 2.3.1

It is possible to create new annotations, behaviors for those annotations, and then register them with a Module. In order to make use of this feature, two Java classes are required of the developer.

  1. Annotation: An annotation object.
  2. MethodHandler: A class defining the behavior to evaluate when the respective Annotation is used.

The MethodHandler interface is provided below.

public interface MethodHandler<T extends Annotation> {
    public Class<T> getAnnotationType();
    public Object processElement(final Object frame, final Method method, final Object[] arguments, final T annotation, final FramedGraph<?> framedGraph, final Element element);

Any implementation of an MethodHandler must implement the respective methods where getAnnotationType() returns the class of the respective Annotation it represents. The processElement method has arguments that provide information to allow the developer to determine the requisite logic to execute. For example, here are some aspects to reason on:

  • Determine if the method is a get, set, or remove by method.getName().startsWith("get")
  • Make use of ClassUtilities for standard, static reasoning methods.
  • Analyze the types of method’s provided arguments.
  • Analyze the type of the methods return object.
  • All framed elements implement VertexFrame or EdgeFrame even if the framing interface doesn’t extend these. Use these interfaces to get access to the underlying element.

All the Annotation objects provided by Frames make use of handlers. Please feel free to inspect the source code to get ideas for how to implement your own handlers. Finally, once an Annotation and MethodHandler have been created, they can be registered with a Module.

FramedGraph g = new FramedGraphFactory(new AbstractModule() {
  public void doConfigure(FramedGraphConfiguration config) {
    config.addMethodHandler(new MyHandler());