Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

aspectd

aspectd is an aop(aspect oriented programming) framework for dart. like other traditional aop framework, aspectd provides call&execute grammar. besides, as we can’t use dart:mirrors in flutter, aspectd also provides a way named inject enhancing the dart code manipulation.

besides, aspectd provides a dill transformer container above which developers can implement their own transformers like hook, json, mirrors, etc.

design

aspectd diagram

suppose you have a flutter project named example located in hf_dir.

installation

1. create a dart package named aspectd_impl in hf_dir/example

flutter create --template=package aspectd_impl

2. add aspectd&example dependency to aspectd_impl package

dependencies:
  flutter:
    sdk: flutter
  aspectd:
    git:
      url: [email protected]:xianyutech/aspectd.git
      ref: stable/v2.2.3
  example:
    path: ../example

remember to change the branch matching your flutter environment (stable supported currently).
fetch package dependency in aspectd_impl package

flutter packages get

3. modify aspectd_impl package

aspectd_impl.dart(entrypoint)

import 'package:example/main.dart' as app;
import 'aop_impl.dart';

void main()=> app.main();

aop_impl.dart(aop implementation)

import 'package:aspectd/aspectd.dart';

@aspect()
@pragma("vm:entry-point")
class executedemo {
  @pragma("vm:entry-point")
  executedemo();

  @execute("package:example/main.dart", "_myhomepagestate", "-_incrementcounter")
  @pragma("vm:entry-point")
  void _incrementcounter(pointcut pointcut) {
    pointcut.proceed();
    print('kwlm called!');
  }
}

4. patch flutter_tools to apply aspectd.dart.snapshot

cd path-for-flutter-git-repo
git apply --3way path-for-aspectd-package/0001-aspectd.patch
rm bin/cache/flutter_tools.stamp

on windows, use “git am –reject –whitespace=fix aspectd�001-aspectd.patch” to apply the patch instead.

as flutter_tools doesn’t support hooks now, the aspectd.patch is necessary currently. as flutter is evolving, this patch might fail sometimes. however, it would be simple to resolve the conflicts as aspectd only adds two hooks when building dill. see https://github.com/alibaba-flutter/aspectd/issues/5 for more.

if you want to customize the aspectd_impl package, edit aspectdimplpackagerelpath(aspectd_impl package relative path to the example’s pubspec.yaml) and aspectdimplpackagename(aspectd_impl package folder name and main entry file name) defined in path-for-flutter-git-repo/flutter/packages/flutter_tools/lib/src/aspectd.dart.

const string aspectdimplpackagerelpath = '..';
const string aspectdimplpackagename = 'aspectd_impl';

step 1~3 are expected to run each time you want to add aspectd_impl to a flutter(dart) package. 4 is expected to run only once unless the dart-sdk changes. for example, if you upgrade flutter, you need to check if to re-run 4.

if you’re using example with an aspectd_impl package not generated locally, remember to run flutter packages get in aspectd_impl package to get aspectd and check 4.

if the flutter version you want is not supported yet, see upgrade.md for more.

tutorial

now aspectd provides three ways to do aop programming.

call

every callsites for a specific function would be manipulated.

import 'package:aspectd/aspectd.dart';

@aspect()
@pragma("vm:entry-point")
class calldemo{
  @call("package:app/calculator.dart","calculator","-getcurtime")
  @pragma("vm:entry-point")
  future<string> getcurtime(pointcut pointcut) async{
    print('aspectd:kwlm02');
    print('${pointcut.sourceinfos.tostring()}');
    future<string> result = pointcut.proceed();
    string test = await result;
    print('aspectd:kwlm03');
    print('${test}');
    return result;
  }

  @call("package:app/calculator.dart","calculator","+getcurtemporature")
  @pragma("vm:entry-point")
  string getcurtemporature(pointcut pointcut) {
    print('aspectd:kwlm04');
    print('${pointcut.sourceinfos.tostring()}');
    try{
      string res = pointcut.proceed();
    } catch (error, trace){
      print('aspectd:kwlm05');
    }
    return null;
  }

  @call("package:flutter/src/widgets/binding.dart","","+runapp")
  @pragma("vm:entry-point")
  static void runappkwlm(pointcut pointcut){
    print('aspectd:kwlm07');
    print('${pointcut.sourceinfos.tostring()}');
    pointcut.proceed();
  }
}

in this case, notice that @aspect() is needed to mark a class so that the aspectd will know that this class contains aspectd annotation informations.
@pragma(“vm:entry-point”) is needed so that the class/function will not be removed by tree-shaking.
for @call(“package:app/calculator.dart”,”calculator”,”-getcurtime”), there are several things to know. now call/execute/inject accept three positional parameters, package name, class name(if the procedure is a library method, this part is empty string), and function name. the function name may have a prefix(‘-‘ or ‘+’), ‘-‘ refers to instance method while ‘+’ refers to library static method(like main) and class method. there is also a named parameter linenum for inject so that aspectd know which line to inject a code snippet. the linenum parameter is 1 based and code snippet would be injected before that line.

besides, when you want to manipulate a static method(including library method and class method), your aop method(runappkwlm here) should also be declared static. this requirement also applies when using execute command.

execute

every implementation for a specific function would be manipulated.

import 'package:aspectd/aspectd.dart';

@aspect()
@pragma("vm:entry-point")
class executedemo{
  @execute("package:app/calculator.dart","calculator","-getcurtime")
  @pragma("vm:entry-point")
  future<string> getcurtime(pointcut pointcut) async{
    print('aspectd:kwlm12');
    print('${pointcut.sourceinfos.tostring()}');
    future<string> result = pointcut.proceed();
    string test = await result;
    print('aspectd:kwlm13');
    print('${test}');
    return result;
  }

  @execute("package:app/calculator.dart","calculator","+getcurtemporature")
  @pragma("vm:entry-point")
  string getcurtemporature(pointcut pointcut) {
    print('aspectd:kwlm14');
    print('${pointcut.sourceinfos.tostring()}');
    try{
      string res = pointcut.proceed();
    } catch (error, trace){
      print('aspectd:kwlm15');
    }
    return null;
  }

  @execute("package:flutter/src/widgets/binding.dart","","+runapp")
  @pragma("vm:entry-point")
  static void runappkwlm(pointcut pointcut){
    print('aspectd:kwlm17');
    print('${pointcut.sourceinfos.tostring()}');
    pointcut.proceed();
  }
}

inject

for a original function like below:(package:flutter/src/widgets/gesture_detector.dart)

  @override
  widget build(buildcontext context) {
    final map<type, gesturerecognizerfactory> gestures = <type, gesturerecognizerfactory>{};

    if (ontapdown != null || ontapup != null || ontap != null || ontapcancel != null) {
      gestures[tapgesturerecognizer] = gesturerecognizerfactorywithhandlers<tapgesturerecognizer>(
        () => tapgesturerecognizer(debugowner: this),
        (tapgesturerecognizer instance) {
          instance
            ..ontapdown = ontapdown
            ..ontapup = ontapup
            ..ontap = ontap
            ..ontapcancel = ontapcancel;
        },
      );
    }
...
}
import 'package:aspectd/aspectd.dart';
import 'package:flutter/services.dart';

@aspect()
@pragma("vm:entry-point")
class injectdemo{
  @inject("package:flutter/src/widgets/gesture_detector.dart","gesturedetector","-build", linenum:452)
  @pragma("vm:entry-point")
  static void ontapbuild() {
    object instance; //aspectd ignore
    object context; //aspectd ignore
    print(instance);
    print(context);
    print('aspectd:kwlm25');
  }
}

after that, the original build function will look like below:

  @override
  widget build(buildcontext context) {
    final map<type, gesturerecognizerfactory> gestures = <type, gesturerecognizerfactory>{};

    if (ontapdown != null || ontapup != null || ontap != null || ontapcancel != null) {
      gestures[tapgesturerecognizer] = gesturerecognizerfactorywithhandlers<tapgesturerecognizer>(
        () => tapgesturerecognizer(debugowner: this),
        (tapgesturerecognizer instance) {
          instance
            ..ontapdown = ontapdown
            ..ontapup = ontapup
            ..ontap = ontap
            ..ontapcancel = ontapcancel;
        },
    	print(instance);
    	print(context);
    	print('aspectd:kwlm25');
      );
    }
...
}

notice that //aspectd ignore part when using injection, we need to compile the aop package successfully so we need to declare the instance/context variable. however, when injecting to origin function (build in this case), variable declaration

object instance; //aspectd ignore 
object context; //aspectd ignore

would be discarded to avoid overring the original one.


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.