Annotation

Data about program elements, such as classes and methods, which can be processed during compilation or execution.

We are used to classifying the data in our programs and making rules about how they work. Customers can be grouped by region and have payment rules. Often, it is useful to make these kinds of rules about elements of the program itself. Languages usually provide some built-in mechanisms to do this, such as access controls that allow us to mark classes and methods as public or private.

However, there are often things we would like to mark that go beyond what a language supports, or even should reasonably support. We might want to restrict the values that an integer field might take, mark methods that should be run as part of testing, or indicate that a class can safely be serialized.

An Annotation is a piece of information about a program element. We can take this information and manipulate it during runtime, or indeed during compile time if the environment supports this. Annotation thus provide a mechanism to extend the programming language.

I've used the term Annotation here, as that is the term used in the Java programming language. A similar syntax predated this in .NET, but its term "attribute" is too widely used for other concepts, so I prefer to follow the Java terminology. However, the concept here is more broad than the syntax, and the same benefits can be achieved without this kind of special syntax.

For more details see chapter 42 of the DSL book

| Catalog of DSL patterns |