Search code examples
javaspringdartaopinterceptor

In Dart, is there a way to intercept accesses and mutations to programming constructs?


For example, the Spring AOP framework for Java offers functionality to provide an interceptor to intercept some processes, for example, when a method is executed - Spring AOP can hijack the executing method, and add extra functionality before or after the method execution.

I want to know if there is something similar to that in Dart. Can you in Dart, for instance, intercept accesses and mutations to a variable? I was not able to find something along those lines in the Dart documentation.

Thank you


Solution

  • You can not do that at runtime for now. You could perhaps do something like this once mirror builders has landed. From Reflection in Dart with Mirrors: An Introduction

    We’d like to support more powerful reflective features in the future. These would include mirror builders, designed to allow programs to extend and modify themselves, and a mirror-based debugging API as well.

    However you can do that at built-time either in a pre-processor (like a build.dart) or with a pub transformer. You also use the analyzer package to get the AST if you need it. This can be seen like APT in Java.