From 05cbd7124842b30612023ea5c03b91ae02538397 Mon Sep 17 00:00:00 2001 From: Thomas Hii Date: Sun, 29 Jan 2023 16:04:15 +0000 Subject: [PATCH] Added example --- experiment/container/example1/bin/main.dart | 2 +- experiment/container/example2/bin/main.dart | 43 +++++++++++++++++++ .../container/example2/lib/src/models.dart | 5 +++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 experiment/container/example2/bin/main.dart create mode 100644 experiment/container/example2/lib/src/models.dart diff --git a/experiment/container/example1/bin/main.dart b/experiment/container/example1/bin/main.dart index 47ecf359..55ba50ac 100644 --- a/experiment/container/example1/bin/main.dart +++ b/experiment/container/example1/bin/main.dart @@ -35,7 +35,7 @@ void printAnnotationValue(final Type clazz) { final annotationInstsanceMirror = clazzDeclaration.metadata.where((d) => d.type == someAnnotationMirror); if (annotationInstsanceMirror.isEmpty) { - print('Annotation is not on this class'); + print('No annotated class found'); return; } final someAnnotationInstance = diff --git a/experiment/container/example2/bin/main.dart b/experiment/container/example2/bin/main.dart new file mode 100644 index 00000000..8abe6a16 --- /dev/null +++ b/experiment/container/example2/bin/main.dart @@ -0,0 +1,43 @@ +import 'package:example2/src/models.dart'; + +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); + printAnnotationValue(Square); +} + +class Shape { + void draw() => print("Draw Shape"); +} + +@Person('Will', 'Tom') +class Square { + void greetHii() { + print("Hii Welcome to flutter agency"); + } +} + +void printAnnotationValue(final Type clazz) { + final DeclarationMirror clazzDeclaration = reflectClass(clazz); + final ClassMirror someAnnotationMirror = reflectClass(Person); + final annotationInstsanceMirror = + clazzDeclaration.metadata.where((d) => d.type == someAnnotationMirror); + if (annotationInstsanceMirror.isEmpty) { + print('No annotated class found'); + return; + } + final someAnnotationInstance = + (annotationInstsanceMirror.first.reflectee as Person); + print(someAnnotationInstance.firstName); +} diff --git a/experiment/container/example2/lib/src/models.dart b/experiment/container/example2/lib/src/models.dart new file mode 100644 index 00000000..abc1b6d8 --- /dev/null +++ b/experiment/container/example2/lib/src/models.dart @@ -0,0 +1,5 @@ +class Person { + final String firstName; + final String lastName; + const Person(this.firstName, this.lastName); +}