Fork me on GitHub

Annotation Type

An annotation is a form of syntactic metadata that can be added to SARL source code. Annotations can be reflective in that they can be embedded in binary files generated by the SARL compiler, and may be retained by the Virtual Machine to be made retrievable at run-time.

1. Define an Annotation

For defining an annotation, you could use the annotation keyword.

The following example defines the annotation MyAnnotation. This annotation defines three parameters:

Examples:

annotation MyAnnotation {
  val value : String[]
  val isTricky : boolean = false
  val lotteryNumbers : int[] = #[ 42, 137 ]
}

2. Modifiers

Modifiers are used to modify declarations of types and type members. This section introduces the modifiers for the annotation types. The modifiers are usually written before the keyword for defining the annotation type.

The complete description of the modifiers’ semantic is available on this page.

2.1. Top Annotation Type Modifiers

A top annotation type may be declared with one or more modifiers, which affect its runtime behavior:

Examples:

public annotation TopAnnotationType1 {
}
package annotation TopAnnotationType2 {
}
abstract annotation TopAnnotationType3 {
}

2.2. Nested Annotation Type Modifiers

A nested annotation type may be declared with one or more modifiers, which affect its runtime behavior:

Terminology Nested annotation types are divided into two categories: static and non-static. Nested annotation types that are declared static are called static nested annotation types. Non-static nested annotation types are calledinner annotation types.

Note The modifiers may differ from the previously described, depending on the enclosing type, e.g. agent.

class EnclosingClass {
	public annotation NestedAnnotationType1 {
	}
	protected annotation NestedAnnotationType2 {
	}
	package annotation NestedAnnotationType3 {
	}
	private annotation NestedAnnotationType4 {
	}
	abstract annotation NestedAnnotationType5 {
	}
	static annotation NestedAnnotationType6 {
	}
}

2.3. Value Modifiers

The modifiers for the values in an annotation type are:

Examples:

public val val1 : int
static val val2 : int

3. References

This documentation is based on documentations from the Xtext and Xtend projects, and from the Java tutorials. Thank you to the contributors to these documents.

4. Version Specification

Copyright © 2014-2025 SARL.io, the Original Authors and Main Authors.

Documentation text and medias are licensed under the Creative Common CC-BY-SA-4.0; you may not use this file except in compliance with CC-BY-SA-4.0. You may obtain a copy of CC-BY-4.0.

Examples of SARL code are licensed under the Apache License, Version 2.0; you may not use this file except in compliance with the Apache License. You may obtain a copy of the Apache License.

You are free to reproduce the content of this page on copyleft websites such as Wikipedia.

Generated with the translator docs.generator 0.15.1.