@Retention(value=CLASS) @Target(value=METHOD) public static @interface GenerateLibrary.Abstract
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 GenerateLibrary.Abstract.ifExported()
attribute.GenerateLibrary.Abstract.ifExported()
Modifier and Type | Optional Element and Description |
---|---|
String[] |
ifExported
Specifies a message to be abstract only if another message is implemented.
|
String[] |
ifExportedAsWarning
Specifies a message to be abstract only if another message is implemented.
|
public abstract String[] ifExported
GenerateLibrary.Abstract.ifExportedAsWarning()
is not empty. If the list is not empty it takes
precedence over GenerateLibrary.Abstract.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.
public abstract String[] ifExportedAsWarning
GenerateLibrary.Abstract.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.