Annotation Interface GenerateLibrary.Abstract

Enclosing class:
GenerateLibrary

@Retention(CLASS) @Target(METHOD) public static @interface GenerateLibrary.Abstract
Makes a library message abstract, but allows to keep a default implementation. By default, abstract messages throw an AbstractMethodError if they are not exported for a given receiver type. To customize this behavior the library message can specify a method body and annotate it with GenerateLibrary.Abstract to keep requiring an implementation from exports.

For example:

 @GenerateLibrary
 public abstract class ArrayLibrary extends Library {

     @Abstract(ifExported = "read")
     public boolean isArray(Object receiver) {
         return false;
     }

     @Abstract(ifExported = "isArray")
     public int read(Object receiver, int index) {
         throw new UnsupportedOperationException();
     }
 }
 
In this example a receiver that does not export the ArrayLibrary will return false for isArray and throw an UnsupportedOperationException for read calls. A message may be made conditionally abstract by specifying the ifExported() attribute.
Since:
19.0
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Specifies a message to be abstract only if another message is implemented.
    Specifies a message to be abstract only if another message is implemented.
  • Element Details

    • ifExported

      String[] ifExported
      Specifies a message to be abstract only if another message is implemented. Multiple other messages can be specified. If the list is empty, the message is always abstract unless ifExportedAsWarning() is not empty. If the list is not empty it takes precedence over ifExportedAsWarning().

      For example:

       @GenerateLibrary
       public abstract class ArrayLibrary extends Library {
      
           @Abstract(ifExported = "read")
           public boolean isArray(Object receiver) {
               return false;
           }
      
           @Abstract(ifExported = "isArray")
           public int read(Object receiver, int index) {
               throw new UnsupportedOperationException();
           }
       }
       
      In this example the isArray message only needs to be exported if the read message is exported and vice-versa.
      Since:
      19.0
      Default:
      {}
    • ifExportedAsWarning

      String[] ifExportedAsWarning
      Specifies a message to be abstract only if another message is implemented. Multiple other messages can be specified. The message is not actually made abstract unless it is in the ifExported() list. Only a warning is produced that prompts the user to implement the message

      For example:

       @GenerateLibrary
       public abstract class MaybeNumberLibrary extends Library {
      
           public boolean isNumber(Object receiver) {
               return false;
           }
      
           @Abstract(ifExportedAsWarning = "isNumber")
           public Number getNumber(Object receiver) {
               throw new UnsupportedOperationException();
           }
       }
       
      In this example, if the isNumber message is exported and the getNumber message is not, the user gets a warning.
      Since:
      23.0
      Default:
      {}