Test reflection

This commit is contained in:
thomashii@dukefirehawk.com 2023-01-29 20:26:35 +08:00
parent 9b85007072
commit ec68a78c1d
4 changed files with 6250 additions and 112 deletions

View file

@ -0,0 +1,35 @@
import 'dart:mirrors';
void main() {
final stopwatch = Stopwatch()..start();
var reflectedClass = reflect(Shape());
reflectedClass.invoke(#draw, []);
//reflectedClass.invoke(Symbol('draw'), []);
print('Reflection executed in ${stopwatch.elapsed.inMilliseconds} ms');
stopwatch.stop();
printAnnotationValue(String);
printAnnotationValue(Shape);
}
class Shape {
void draw() => print("Draw Shape");
}
void printAnnotationValue(final Type clazz) {
final DeclarationMirror clazzDeclaration = reflectClass(clazz);
final ClassMirror someAnnotationMirror = reflectClass(Shape);
final annotationInstsanceMirror =
clazzDeclaration.metadata.where((d) => d.type == someAnnotationMirror);
if (annotationInstsanceMirror.isEmpty) {
print('Annotation is not on this class');
return;
}
final someAnnotationInstance =
(annotationInstsanceMirror.first.reflectee as Shape);
print("${someAnnotationInstance.draw}");
}

View file

@ -20,7 +20,6 @@ void main() async {
container.registerSingleton<MyController>(MyController()); container.registerSingleton<MyController>(MyController());
var app = Angel(reflector: reflector); var app = Angel(reflector: reflector);
var http = AngelHttp(app); var http = AngelHttp(app);
//await app.mountController<MyController>(); //await app.mountController<MyController>();

File diff suppressed because it is too large Load diff

View file

@ -35,9 +35,11 @@ dev_dependencies:
io: ^1.0.0 io: ^1.0.0
test: ^1.21.0 test: ^1.21.0
lints: ^2.0.0 lints: ^2.0.0
# dependency_overrides: dependency_overrides:
# angel3_container: angel3_container:
# path: ../container/angel_container path: ../../../packages/container/angel_container
angel3_container_generator:
path: ../../../packages/container/angel_container_generator
# angel3_http_exception: # angel3_http_exception:
# path: ../http_exception # path: ../http_exception
# angel3_model: # angel3_model: