注解(Annotation)是ArkTS的语言特性,用于为类、方法等声明附加结构化的元数据。本章介绍注解的声明、使用、导入导出及源码态注解等内容。
注解通过@interface声明,以@前缀使用,为类和方法等声明添加元数据。
元数据是描述代码的信息,本身不属于程序逻辑。注解不会改变被标注代码的运行行为,其价值在于元数据可以被编译器和工具链读取并处理。作者、版本这类信息写在注释里无法被程序化识别,注解则将其结构化地附加到声明上,随代码一起编译,典型场景包括:
注解是ArkTS的扩展特性,只能在.ets与.d.ets文件中声明和使用,在.ts文件中无法声明或使用。注解不是类型,不能作为类型使用,也不能对注解使用类型别名,否则会出现编译错误。
注解的声明使用@interface关键字,使用时以@为前缀。
// 注解的声明:
@interface ClassAuthor {
authorName: string;
}
// 注解的使用:
@ClassAuthor({authorName: "Bob"})
class MyClass {
// ...
} 对于要使用的注解,其名称必须以符号@(例如:@MyAnno)为前缀。
ClassAuthor({authorName: "Bob"}) // 编译错误:注解需要'@'为前缀 如果在使用位置无法访问注解名称,则会发生编译错误。
注解声明可以导出并在其他文件中使用。
多个注解可以应用于同一个声明(注解间的先后顺序不影响使用)。
@MyAnno()
@ClassAuthor({authorName: "John Smith"})
class MyClass1 {
// ...
}
应用开发中,在release模式下构建源码HAR,并同时开启混淆时,由于编译产物为JS文件,而在JS中没有注解的实现机制,因此会在编译过程中被移除,导致无法通过注解实现AOP插桩。
为避免因此引起的功能异常,禁止在JS HAR(编译产物中存在JS的HAR包)中使用注解。
如果需要在release模式并且开启混淆的情况下构建含有注解的HAR包,可以构建字节码HAR。
上文示例中的注解声明与使用均位于.ets源码文件。除.ets源码文件外,注解也可以出现在.d.ets声明文件中:在.d.ets文件中,通过环境声明(ambient declaration)声明注解。
ambientAnnotationDeclaration: 'declare' userDefinedAnnotationDeclaration ;
// NameAnno.d.ets
export declare @interface ClassAuthor3 {} 上述声明中:
// NameAnno.d.ets
export declare @interface NameAnno {name: string = ""} // MyAnno.ets
export @interface NameAnno {name: string = ""} // ok 环境声明的注解和class类似,也可以被import使用。
// NameAnno.d.ets
export declare @interface MyAnno {} // ImportMyAnno.ets
import { MyAnno } from './NameAnno';
@MyAnno
class C {
// ...
} 编译器自动生成的.d.ets文件
当编译器根据.ets代码自动生成.d.ets文件时,存在以下2种情况。
当注解定义被导出时,源代码中的注解定义会在.d.ets文件中保留。
// MyAnno.ets
export @interface ClassAuthor5 {}
@interface MethodAnno { // 没导出
data: number;
} // NameAnno.d.ets
export declare @interface ClassAuthor5 {}
// 注:未导出的 @interface MethodAnno 不会出现在生成的 .d.ets 文件中 当下面所有条件成立时,源代码中实体的注解实例会在.d.ets文件中保留。
// MyAnno.ets
import { ClassAuthor4 } from './Author';
export @interface MethodAnno4 {
data: number = 0;
}
@ClassAuthor4
export class MyClass {
@MethodAnno4({data: 123})
foo() {}
@MethodAnno4({data: 456})
private bar() {}
} // NameAnno.d.ets 编译器生成的声明文件
import { ClassAuthor4 } from './Author';
export declare @interface MethodAnno4 {
data: number = 0;
}
@ClassAuthor4
export declare class MyClass {
@MethodAnno4({ data: 123 })
foo(): void;
private bar; // 私有方法不保留注解,签名不导出,生成为 private bar;
} 开发者生成的.d.ets文件
开发者生成的.d.ets文件中的注解信息不会自动应用到实现的源代码中。
// NameAnno.d.ets 开发者生成的声明文件
@interface ClassAuthor6 {}
@ClassAuthor6 // 声明文件中有注解
class C {
// ...
} // MyAnno.ets 开发者对声明文件实现的源代码
@interface ClassAuthor6 {}
// 实现文件中没有注解
class C {
// ...
} 在最终编译产物中,class C没有注解。
用户自定义注解的定义与interface的定义类似,其中的interface关键字以符号@为前缀。
注解字段仅限于下面列举的类型:
注解字段的默认值必须使用常量表达式来指定。常量表达式的场景如下所示:
如果枚举值不能在编译时确定,会引发编译错误。
function foo(): number {
return 1;
}
@interface Position {
data: number = foo(); // 编译错误:注解字段的默认值必须使用常量表达式
} 注解必须定义在顶层作用域(top-level),且名称不能与注解定义所在作用域内可见的其他实体名称相同,否则会出现编译错误。
namespace ns {
@interface MetaInfo { // 编译错误:注解必须定义在顶层作用域
// ...
}
}
@interface Position {
// ...
}
class Position { // 编译错误:注解的名称不能与注解定义所在作用域内可见的其他实体名称相同
// ...
}
@interface ClassAuthor {
name: string;
}
@interface ClassAuthor { // 编译错误:注解的名称不能与注解定义所在作用域内可见的其他实体名称相同
data: string;
} 注解不是类型,把注解当类型使用时会出现编译错误(例如:对注解使用类型别名)。
@interface Position {}
type Pos = Position; // 编译错误:注解不是类型 注解不支持在类的getter和setter方法中添加,若添加注解将导致编译错误。
@interface ClassAuthor {
authorName: string;
}
@ClassAuthor({authorName: 'John Smith'})
class MyClass {
private _name: string = 'Bob';
@ClassAuthor({authorName: 'John Smith'}) // 编译错误:注解不支持在类的getter和setter方法中添加
get name() {
return this._name;
}
@ClassAuthor({authorName: 'John Smith'}) // 编译错误:注解不支持在类的getter和setter方法中添加
set name(authorName: string) {
this._name = authorName;
}
} 当前仅允许对类声明和方法声明使用注解,对类和方法可以同时使用同一个注解。注解不支持在类的getter和setter方法上添加,若添加将导致编译错误。
@ClassPreamble({authorName: "John", revision: 2})
class C1 {
// ...
}
@ClassPreamble({authorName: "Bob"}) // revision的默认值为1
class C2 {
// ...
}
@MyAnno() // 对类和方法可以同时使用同一个注解
class C3 {
@MyAnno()
foo() {}
@MyAnno()
static bar() {}
} 注解声明示例如下:
@interface ClassPreamble {
authorName: string;
revision: number = 1;
}
@interface MyAnno {} 注解中的字段顺序不影响使用。
@interface ClassPreamble1 {
authorName: string;
revision: number = 1;
}
@ClassPreamble1({authorName: "John", revision: 2})
class C5 {}
// 等价于:
@ClassPreamble1({revision: 2, authorName: "John"})
class C6 {} 使用注解时,必须给所有没有默认值的字段赋值,否则会发生编译错误。
赋值应当与注解声明的类型一致,所赋的值与注解字段默认值的要求一样,只能使用常量表达式。
@ClassPreamble() // 编译错误:authorName字段未赋值
class C1 {
// ...
} 如果注解中定义了数组类型的字段,则使用数组字面量来设置该字段的值。
@interface ClassPreamble2 {
authorName: string;
revision: number = 1;
reviewers: string[];
}
@ClassPreamble2(
{
authorName: "Alice",
reviewers: ["Bob", "Clara"]
}
)
class C0 {
// ...
} 如果不需要定义注解字段,可以省略注解名称后的括号。
@MyAnno
class C4 {
// ...
} 注解也可以被导入导出。针对导出,当前仅支持在定义时的导出,即export @interface的形式。
// MyAnno.ets
export @interface MyAnno2 {}
export @interface ClassAuthor2 {} 针对导入,当前仅支持以下两种方式:
// Annotation.ets
import { MyAnno2 } from './MyAnno';
import * as ns from './MyAnno';
// ...
@MyAnno2
@ns.ClassAuthor2
class C {
// ...
} 导入导出存在以下限制:
不允许在import中对注解进行重命名。
import { MyAnno as Anno } from './a'; // 编译错误:不允许在import中对注解进行重命名 由于注解不是类型,因此禁止使用type符号进行导入和导出。
import type { MyAnno } from './a'; // 编译错误:注解不允许使用'type'符号进行导入和导出 不允许对注解使用其他任何形式的import/export,这会导致编译错误。
此外,如果仅从模块导入注解,则不会触发模块的副作用:
// MyAnno.ets
export @interface Anno {}
export @interface ClassAuthor1 {}
console.info('hello'); // Annotation.ets
import { Anno } from './MyAnno';
// 仅引用了Anno注解,不会导致MyAnno.ets的console.info执行 同一个实体不能重复使用同一注解,否则会导致编译错误。
@interface MyAnno3 { name: string; value: number }
@MyAnno3({name: "123", value: 456})
@MyAnno3({name: "321", value: 654}) // 编译错误:不允许重复注解
class C {
// ...
} 子类不会继承基类的注解,也不会继承基类方法的注解。
不支持对抽象类或抽象方法使用注解,否则将导致编译错误。
@MyAnno3 // 编译错误:不允许在抽象类和抽象方法上使用注解
abstract class C {
@MyAnno3
abstract foo(): void; // 编译错误:不允许在抽象类和抽象方法上使用注解
} 从API version 24开始,支持用户自定义源码态注解。
源码态注解为一类特殊形式的注解。源码态注解的生命周期只在编译期,不会影响编译产物。
开发者可以通过使用ArkTS提供的Retention接口来构造自定义源码态注解。源码态注解有更广的使用范围,支持在以下声明上使用:
源码态注解的声明和使用示例如下所示:
import { Retention, RetentionPolicy } from '@kit.ArkTS';
// 构造用户自定义源码态注解
@Retention({policy: RetentionPolicy.SOURCE})
@interface SourceAnnotation {}
// 源码态注解可以在类和类成员上使用
@SourceAnnotation
class C {
@SourceAnnotation
private name_: string = '';
@SourceAnnotation
get name(): string {
return this.name_;
}
}
// 源码态注解可以在变量声明上使用
@SourceAnnotation
let a = 1;
// 源码态注解可以在接口和接口成员上使用
@SourceAnnotation
interface I {
@SourceAnnotation
foo(): void
}
// 源码态注解可以在注解上使用
@SourceAnnotation
@interface Anno {}
// 源码态注解可以在函数上使用
@SourceAnnotation
function func() {}
// 源码态注解可以在命名空间上使用
@SourceAnnotation
namespace ns {}
// 源码态注解可以在类型别名上使用
@SourceAnnotation
type A = number;
// 源码态注解可以在枚举上使用
@SourceAnnotation
enum ColorSet { RED, GREEN, BLUE }