Skip to content

Commit

Permalink
Get actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriano Santos committed Oct 18, 2023
1 parent 154c5b8 commit 6ccd74f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
32 changes: 29 additions & 3 deletions lib/src/reflect_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,42 @@ class ReflectHelper {
static Map<String, MethodMirror> getMethodsByAnnotation(
List<MethodMirror> allDeclaredMethods, Type annotation) {
final Map<String, MethodMirror> methods = {};
var annotationMirror = reflectClass(annotation);
ClassMirror annotationMirror = reflectClass(annotation);

allDeclaredMethods
.where((elem) => isMethodAnnotationBy(elem, annotationMirror))
.forEach(
(e) => methods[capitalize(MirrorSystem.getName(e.simpleName))] = e);
.forEach((e) {
Optional<Object> annotationAction = getMethodAnnotation(e, Action);

if (annotationAction.isPresent) {
Action actionMetadata = annotationAction.value as Action;
if (actionMetadata.name.isNotEmpty) {
methods[actionMetadata.name] = e;
} else {
methods[MirrorSystem.getName(e.simpleName)] = e;
}
} else {
methods[MirrorSystem.getName(e.simpleName)] = e;
}
});
return methods;
}

static bool isMethodAnnotationBy(
MethodMirror method, ClassMirror annotationMirror) =>
method.metadata.where((test) => test.type == annotationMirror).isNotEmpty;

static Optional<Object> getMethodAnnotation(
DeclarationMirror declaration, Type annotation) {
for (var instance in declaration.metadata) {
if (instance.hasReflectee) {
var reflectee = instance.reflectee;
if (reflectee.runtimeType == annotation) {
return Optional.of(reflectee);
}
}
}

return Optional.empty();
}
}
24 changes: 19 additions & 5 deletions lib/src/stateful_named_actor_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ import 'package:spawn_dart/src/protocol/eigr/functions/protocol/actors/protocol.
import 'package:spawn_dart/src/reflect_helper.dart';

class StatefulNamedActorHandler implements ActorHandler {
static final _logger = Logger(
filter: SpawnLogFilter(),
printer: LogfmtPrinter(),
output: SimpleConsoleOutput(),
);

final Map<String, MethodMirror> actions = {};
Type? actorEntityType;
DeclarationMirror? _mirror;
ClassMirror? _mirror;
StatefulNamedActor? _statefulNamedActorAnnotationInstance;
List<MethodMirror> _allDeclaredMethods = [];

StatefulNamedActorHandler(Type actorEntity) {
actorEntityType = actorEntity;
Expand All @@ -26,9 +32,17 @@ class StatefulNamedActorHandler implements ActorHandler {
(statefulActorAnnotationInstanceMirror?.reflectee
as StatefulNamedActor);

if (_allDeclaredMethods.isEmpty) {
_allDeclaredMethods =
ReflectHelper.getAllMethods(reflectClass(_mirror.runtimeType));
List<MethodMirror> allDeclaredMethods =
ReflectHelper.getAllMethods(_mirror!);

actions.addAll(
ReflectHelper.getMethodsByAnnotation(allDeclaredMethods, Action));

if (actions.isEmpty) {
var methodLength = allDeclaredMethods.length;

_logger.w(
"No Actions were found for this Actor Handler. However, there are $methodLength declared methods.");
}
}

Expand Down

0 comments on commit 6ccd74f

Please sign in to comment.