Annotation Type DMethodOrder


@Retention(SOURCE) @Target(METHOD) public @interface DMethodOrder
Search order for arguments to dynamic methods. This annotation is applicable only for methods annotated with the DynamicMethod annotation.

When there are more than one arguments for a dynamic method, multiple methods may match. This annotation determines the search order. The first match will be used. In searching, the implementation starts with most specific class for the object whose method is being executed, and the classes of each argument. If there isn't a match, the class of the last argument in the order list is replaced with its superclass, repeating until either a match is found or a base type (class or interface) is reached. If there is still no match, the next to last argument in the priority list is replaced with its superclass and the last argument in the priority list is set to the class of the last argument. If there is still no match, the next to last argument's current class is replaced with its superclass, repeating as before until a base class is reached. If there is still no match, the third form the last argument is varied, following the same pattern until all combinations of superclasses are tried or until a match is found.

For example, given the classes


 public class Foo1 {
    int a;
 }
 public class Foo2 extends Foo1 {
   int b;
 }

 public class Foo3 extends Foo2 {
   int b;
 }
 

 public class Bar1 {
   int c;
 }
 public class Bar2 extends Bar1 {
   int d;
 {
 public class Bar3 extends Bar2 {
   int d;
 {

 public class FooBar {
    @DynamicMethod("FooBarHelper")
    @DMethodOrder({1,2})
    public class foobar(Foo1 foo, Bar1 bar) {
        FooBarHelper.dispatch(foo, bar);
    }
 }

 @DMethodContext("FooBarHelper", "FooBarHelper1")
 public class FooBar1 extends FooBar {

   @DMethodImpl("FooBarHelper")
   foobarImpl(Foo1 foo, Bar1 bar) {...}

   @DMethodImpl("FooBarHelper")
   foobarImp(Foo2 foo, Bar2 bar) {...}
 }

 
Then the order in which class will be searched will be when arguments of type Foo3 and Bar3 are passed to obj.foobar, where obj is an instance of a subclass of FooBar.

     Foo3  Bar3
     Foo3  Bar2
     Foo3  Bar1
     Foo2  Bar3
     Foo2  Bar2
     Foo2  Bar1
     Foo1  Bar3
     Foo1  Bar2
     Foo1  Bar1
 
with the search stopping at the first match. If there is no match, the search is repeated for the superclass of the object continuing until class FooBar is reached. Arguments that are arrays (including a variable-argument list) or primitive types do not participate in the search and their priority values must be zero or negative. A zero or negative argument can also be used for arguments whose type is a class or interface, in which case the compile-time type of the argument will be used in determining a match. For example,

 public class FooBar {
    @DynamicMethod("FooBarHelper")
    @DMethodOrder({1,0,0,2})
    public class foobar(Foo1 foo,int index, Foo[] array, Bar1 bar) {
        FooBarHelper.dispatch(foo, bar);
    }
 }
 
In this case, the search for arguments foo and bar occur just as in the previous example, while the arguments index and array have their types determined at compile time.
See Also:
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    int[]
    A list of argument priorities, starting from 1, denoting the order in which the arguments' superclasses will be searched until there is a match.
  • Element Details

    • value

      int[] value
      A list of argument priorities, starting from 1, denoting the order in which the arguments' superclasses will be searched until there is a match. If an argument should be ignored, its priority value should be 0 or negative. The argument corresponding to the highest priority value in the array will have its superclass searched first. The priority value must be zero or negative for arrays and primitive types.
      Returns:
      the value of this element