Mirror API

Mirror API

The Mirror API is used to model the semantic structure of a program.

See:
          Description

Packages
com.sun.mirror.apt Classes used to communicate information between annotation processors and an annotation processing tool.
com.sun.mirror.declaration Interfaces used to model program element declarations.
com.sun.mirror.type Interfaces used to model types.
com.sun.mirror.util Utilities to assist in the processing of declarations and types.

 

The Mirror API is used to model the semantic structure of a program. It provides representations of the entities declared in a program, such as classes, methods, and fields. Constructs below the method level, such as individual statements and expressions, are not represented.

Also included is support for writing annotation processors to examine and process the annotations of program elements. An annotation processor may, as an example, create new source files and XML documents to be used in conjunction with the original code.

Characteristics of the API

A program is represented at the language level, rather than at the level of the virtual machine. Nested classes, for example, are handled as first-class constructs, rather than in the translated form understood by the VM. Both source code and compiled code (class files) may be modeled in this way.

Programs are modeled in their static, or build-time, form. This differs from the reflection API, which provides run-time information about classes and objects.

The API does not provide direct support for generating new code.

Declarations and Types

The mirror API represents program constructs principally through the Declaration interface and its hierarchy of subinterfaces in the package com.sun.mirror.declaration. A Declaration represents a program element such as a package, class, or method. The interface hierarchy is depicted here.

Types are represented by the TypeMirror interface and its hierarchy of subinterfaces in the package com.sun.mirror.type. Types include primitive types, class and interface types, array types, type variables, and wildcards. The interface hierarchy is depicted here.

The API makes a clear distinction between declarations and types. This is most significant for generic types, where a single declaration can define an infinite family of types. For example, the declaration of java.util.Set defines the raw type java.util.Set, the parameterized type java.util.Set<String>, and much more. Only the declaration can be annotated, for example, and only a type can appear in a method signature.

A program being modeled may be incomplete, in that it may depend on an unknown class or interface type. This may be the result of a processing error such as a missing class file, or perhaps the missing type is to be created by an annotation processor. See DeclaredType for information on how such unknown types are handled.

Utilities and Tool Support

The com.sun.mirror.util package provides utilities to assist in the processing of declarations and types. Included is support for using the visitor design pattern when operating on declaration and type objects.

The com.sun.mirror.apt package supports the writing of annotation processors. It provides the mechanism for them to interact with an annotation processing tool.

Since:
1.5

Mirror API

Report a bug or request a feature.
Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Use is subject to license terms.