Annotation Interface Alias


@Retention(RUNTIME) @Target({METHOD,CONSTRUCTOR,FIELD}) public @interface Alias
Mechanism for referring to fields and methods otherwise inaccessible due to Java language access control rules. This enables VM code to directly access a private field or invoke a private method in a JDK class without using reflection. Aliases avoid the boxing/unboxing required by reflection and they type check an aliased field access or method invocation statically.

The idiom for using Alias is somewhat related to the Substitute annotation, but reversed; both are often used in combination. In both cases a separate class is used to declare the aliased and/or substituted methods. In the substitution case occurrences of this actually refer to the instance of the class being substituted. In the aliased case we pretend that the class declaring the aliased method or field is an instance of the aliasee in order to access its fields or invoke its methods. An alias is always called (method alias) or accessed (field alias), whereas a substitution method is only implemented (usually not called directly). In the body of a substitution method, aliases are often called or accessed.

The element can also be annotated with TargetElement to specify additional properties. See TargetClass for an overview of the annotation system.

When aliasing *non-static* inner classes the constructors are passed a hidden argument which is the outer class. When writing an @Alias for a constructor of a *non-static* inner classes, you have to (a) explicitly declare that parameter, and (b) supply it in the calls.

Since:
22.3