Chapter 11. Classes and Interfaces

Table of Contents

11.1. Annotations
11.2. Method Declarations
11.2.1. Generator Method Declarations
11.2.2. Operator Method Declarations
11.2.3. Instancing Method Declarations

On a large scale, classes and interfaces are declared as defined in the Java Language Specification:

Type Declaration
[13]TypeDeclaration::=ClassDeclaration | InterfaceDeclaration  
[14]ClassDeclaration::=Modifiers class Identifier [ extends Name ] [ implements Name {',' Name } ] '{' { ClassBodyDeclaration } '}'  
[15]ClassBodyDeclaration::=TypeDeclaration | AbstractMethodDeclaration | ConcreteMethodDeclaration | InstancingMethodDeclaration | FieldDeclaration | ConstructorDeclaration | InstanceInitializer | StaticInitializer | ';'  
[16]InterfaceDeclaration::=Modifiers interface Identifier [ extends Name {',' Name } ] '{' { InterfaceBodyDeclaration } '}'  
[17]InterfaceBodyDeclaration::=TypeDeclaration | AbstractMethodDeclaration | FieldDeclaration | ';'  

However, when looking at the details, the XL programming language defines three extensions:

11.1. Annotations

The purpose of an annotation is to associate information with a program element. Annotations are used as modifiers in declarations of classes, interfaces, fields, methods, and constructors. The associated information is an instance of an annotation type. The annotations of the XL programming language are similar to the annotations defined in the Java Language Specification, Third Edition; the syntax of annotations is exactly the same.

An annotation type is a concrete, public class A with a public constructor with no parameters which implements the interface de.grogra.reflect.Annotation. An annotation type defines a number of elements:

  • A has an element named e if and only if A has as member a public and non-static method with a single parameter and the name set_e or init_e. It is a compile-time error if several such methods with differing parameter types exist; A is no legal annotation type in this case.

  • The type of the element e is the type of the method parameter. If a method named set_e exists as a member of A, the element is an element with default-value. Otherwise, only a method named init_e exists as a member of A, and the element is an element without default-value.

Now an annotation is a modifier consisting of the token @, the name of an annotation type, and zero or more element-value pairs, each of which associates a value with a different element of the annotation type:

Modifiers and Annotations
[18]Modifiers::={ Modifier }  
[19]Modifier::=Annotation | public | protected | private | abstract | static | final | synchronized | native | strictfp | transient | volatile | const  
[20]Annotation::=NormalAnnotation | MarkerAnnotation | SingleElementAnnotation  
[21]NormalAnnotation::='@' Name '(' [ ElementValuePairs ] ')'  
[22]MarkerAnnotation::='@' Name  
[23]SingleElementAnnotation::='@' Name '(' ElementValue ')'  
[24]ElementValuePairs::=ElementValuePair { ',' ElementValuePair }  
[25]ElementValuePair::=Identifier '=' ElementValue  
[26]ElementValue::=Annotation | ExpressionNoAssignment | ElementValueArrayInitializer  
[27]ElementValueArrayInitializer::='{' [ ElementValues ] [ ',' ] '}'  
[28]ElementValues::=ElementValue { ',' ElementValue }  

The identifier in an element-value pair of a normal annotation must be the simple name of one of the elements of the annotation type. The value may only be composed of constants of primitive type or String, class literals, annotations, and arrays of those values (using the production ElementValueArrayInitializer). The value is associated with the element, or, if the element type is an array type and the value is not an array, the associated value is an array whose sole element is the original value. It is a compile-time error if the value to associate is not assignable to the element type.

A marker annotation is treated as a normal annotation with no element-value pairs. A single-element annotation is treated as a normal annotation with a single element-value pair where the element identifier is value and the value is the provided value.

It is a compile-time error if more than one value is associated with a single element, or if no value is associated with an element without default-value.

As examples for a normal annotation, a marker annotation, and a single-element annotation, consider the following annotated declarations of fields, which could be interpreted by a graphical user interface in order to generate suitable input components:

@Range(min=0, max=360) private float angle;
@Editable String name;
@Choice({"Center", "Left", "Right"}) int alignment;