Polygene™
Introduction
Tutorials
Javadoc
Samples
Core
Libraries
Extensions
Tools
Glossary 

Use @Invocation

The @Invocation annotation is relatively unknown but can be rather powerful to use, especially when creating libraries that needs to be flexible of what the user needs to do.

@Invocation is a different InjectionScope, which is a concept to tell Polygene runtime where to look for the instances to be injected. Other, more well-known, InjectionScope annotations are @This, @Structure and @Service.

The @Invocation injection scope can provide the following types, all related to the on-going method invocation, which is especially useful in Generic Concerns or Generic Mixins;

java.lang.reflect.Method

This injection will simply provide the java.lang.reflect.Method of the on-going call. For generic fragments that will be the same as the second argument in the java.lang.reflect.InvocationHandler.invoke() method. Sometimes it is useful to obtain this for typed fragment as well, to reduce names in Strings.

java.lang.reflect.AnnotatedElement

This Reflection API class encapsulates the annotation aspect of any element that can be annotated. Polygene implements this interface for the Composite. That means that annotations for both the method as well as the composite is provided through this injection.

Custom Annotations

It is often useful to introduce one’s own annotations, especially for libraries, and use these annotations to direct the runtime to do different things. Many of the "built-in" features in Polygene is actually done by this mechanism and not directly implemented in the Core Runtime.

First create an annotation of your own liking, it must have java.lang.annotation.Retention set to RUNTIME

@Retention( RUNTIME )
@interface Foo
{
    String value();
}

After that it is possible to have this annotation placed on composite type methods,

public interface MyComposite
    extends TransientComposite
{
    @Foo( "1" )
    void doStuff();

and then the annotation can simply be injected into your Concerns or Mixins, like this;

public abstract static class MyConcern
    extends ConcernOf<MyComposite>
    implements MyComposite
{
    @Invocation
    Foo foo;

java.util.Iterable<Method>

This injection will provide all the declared methods of the current composite. This is particularly useful for mixins or concerns that builds information about the composite they belong to.