Dart彙總請點擊這裏
在Dart中,symbol用來反射庫中的元數據
使用反射
新建Animal.dart,輸入以下內容:
library animal_lib;
class Animal {
walk() {
print('animals can walk');
}
sleep() {
print('animals need to sleep');
}
eat() {
print('animals need to eat');
}
}
class Person extends Animal{
work() {
print('a person need to work for money');
}
}
新建AnimalSymbol.dart,輸入以下內容:
import 'dart:core';
import 'dart:mirrors';
import 'Animal.dart';
main() {
Symbol lib = new Symbol('animal_lib');
Symbol className = new Symbol('Animal');
// 像是上面的編譯時常量也可以寫成:Symbol className = #Animal;
// print(#s == new Symbol('s')); //true
testSymbolReflect(lib, className);
}
void testSymbolReflect(Symbol lib, Symbol className) {
MirrorSystem mirrorSystem = currentMirrorSystem();
LibraryMirror libMirror = mirrorSystem.findLibrary(lib);
if (libMirror != null) {
print('there are ${libMirror.declarations.length} classes found in the library:');
// 獲取庫中所有的類
libMirror.declarations.forEach((s, d) => print(s));
// containsKey用來判斷庫中是否存在某一個類
if (libMirror.declarations.containsKey(className)) print('found class');
// 獲取一個指定類的反射
ClassMirror classMirror = libMirror.declarations[className];
print('there are ${classMirror.instanceMembers.length} instance methods in the ${MirrorSystem.getName(className)}: ');
// 獲取該類中所有的實例方法
classMirror.instanceMembers.forEach((s, v) => print(MirrorSystem.getName(s)));
}
}
執行dart AnimalSymbol.dart,輸出結果:
Dart mirror 家族
ClassMirror反映了Dart語言類。.
ClosureMirror反映了一個閉包. […]
CombinatorMirror在庫依賴項上聲明的show / hide組合的鏡像。
Comment用於將註釋編碼爲元數據註解的類。
DeclarationMirror反映了Dart程序中聲明的某個實體.
FunctionTypeMirror表示Dart語言中函數的類型.
InstanceMirror反映Dart語言對象的實例.
IsolateMirror反映了隔離.
LibraryDependencyMirror導入或導出聲明中的鏡像.
LibraryMirror反映了Dart語言庫,提供對庫的變量,函數和類的訪問.
MethodMirror反映Dart語言函數,方法,構造函數,getter或setter.
Mirror 反映了一些Dart語言實體. […]
MirrorsUsed描述如何使用“dart:mirrors”的註解(實驗). […]
MirrorSystem是用於反映一組關聯庫的主要接口. […]
ObjectMirror 是InstanceMirror, ClassMirror, 和 LibraryMirror的公共超接口,表示它們的共享功能. […]
ParameterMirror反映Dart形式參數聲明.
SourceLocation描述Dart源代碼中實體的範圍.
TypedefMirror表示Dart語言程序中的typedef.
TypeMirror反映Dart語言類,typedef,函數類型或類型變量.
TypeVariableMirror表示泛型類型的類型參數.
VariableMirror反映了Dart語言變量聲明.
Functions
currentMirrorSystem() → MirrorSystem
返回當前隔離的MirrorSystem.
reflect(Object reflectee) → InstanceMirror
反映一個實例. […]
reflectClass(Type key) → ClassMirror
反映類聲明. […]
reflectType(Type key, [ List typeArguments ]) → TypeMirror
反映key表示的類型. […]
反射分析
反射類
abstract class ClassMirror implements TypeMirror, ObjectMirror {
ClassMirror get superclass; //父類 , Object的父類爲null
List<ClassMirror> get superinterfaces; //接口列表
bool get isAbstract; //是否抽象類
bool get isEnum; //是否枚舉類
//只包含自己原本的方法(構造方法、setter/getter、普通方法、靜態方法)、成員(普通成員、靜態成員),不包含繼承來的
//注意: 屬性是VariableMirror,實現了setter/getter的屬性爲MethodMirror
Map<Symbol, DeclarationMirror> get declarations;
//包含構造方法、setter/getter、普通方法,包含繼承來的
Map<Symbol, MethodMirror> get instanceMembers;
//靜態方法及靜態屬性的setter/getter方法,包含繼承來的
//與instanceMembers合在一起就是類中全部的方法
Map<Symbol, MethodMirror> get staticMembers;
//如果S = A with B ,那麼ClassMirror(S).mixin 爲 ClassMirror(B),否則返回本身
ClassMirror get mixin;
/**
* 調用構造方法
* constructorName 構造方法名稱(默認構造方法爲空字符串,命名構造方法爲其命名)
* positionalArguments 參數列表
*/
InstanceMirror newInstance(Symbol constructorName, List positionalArguments,
[Map<Symbol, dynamic> namedArguments]);
bool operator ==(other); //判斷是否相等
bool isSubclassOf(ClassMirror other); //判斷是不是other的子類
}
反射方法
abstract class MethodMirror implements DeclarationMirror {
TypeMirror get returnType; //反射類型
String get source; //source code , 不可用返回null
List<ParameterMirror> get parameters; //參數列表
bool get isStatic; //是否靜態方法
bool get isAbstract;//是否抽象方法
bool get isSynthetic;//Synthetic方法(即隱式的setter/getter或構造方法[只定義屬性或無構造函數])
bool get isRegularMethod; //常規方法(即非setter/getter、構造方法)
bool get isOperator; //操作符方法
bool get isGetter; //get方法
bool get isSetter; //set方法
bool get isConstructor; //判斷是否構造方法
Symbol get constructorName; //獲得構造方法的名字,默認構造方法爲空字符串
bool get isConstConstructor; //常量構造方法
bool get isGenerativeConstructor;
bool get isRedirectingConstructor;//重定向構造方法
bool get isFactoryConstructor; //工廠構造方法
bool operator ==(other);
}
反射聲明
abstract class DeclarationMirror implements Mirror {
Symbol get simpleName; //簡稱
Symbol get qualifiedName; //全稱,包含路徑
//庫 -> null
//類、頂級函數或變量、typedef -> 庫
//S with M -> M
//類中的方法、變量 -> 類
//函數中的局部變量、函數 -> 函數
DeclarationMirror get owner; //所有者
bool get isPrivate; //私有
bool get isTopLevel; //頂級
//格式:dart:core/runtime/libobject_patch.dart:53
SourceLocation get location; //來源,標明方法、類、屬性所在文件的位置
List<InstanceMirror> get metadata; //元數據列表
}
反射對象
abstract class InstanceMirror implements ObjectMirror {
ClassMirror get type; //類型
bool get hasReflectee;
get reflectee;//反射一個實體對象(hasReflectee==false,拋異常)
bool operator ==(other);
}
反射屬性
variableMirror 類屬性或頂級屬性
- isStatic 靜態
- isFinal
- isConst
- type 返回類型反射TypeMirror
ParameterMirror 函數的參數
- isOptional 可選位置參數([])
- isNamed 可選命名參數({})
- hasDefaultValue 是否有默認值
- defaultValue 獲取默認值對象反射
反射類型
TypeMirror 比如:List
- reflectedType 類型 List
- typeVariables 類型變量集合 , E( List中類型變量是E )
- typeArguments 類型集合 , int
TypedefMirror
TypeVariableMirror 代指泛型的類型變量(如: T)
- upperBound 上界 , T extends M ,返回 M
- isStatic
TypedefMirror 類型別名
- referent 返回函數類型反射FunctionTypeMirror
FunctionTypeMirror 函數類型反射
//typedef A<T> = int Function(T a, T b);
abstract class FunctionTypeMirror implements ClassMirror {
TypeMirror get returnType; //返回類型反射 int
List<ParameterMirror> get parameters; // 參數反射集合
MethodMirror get callMethod;
}