Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

部分语句Inject会失效 #34

Closed
lizhangqu opened this issue Nov 25, 2019 · 1 comment
Closed

部分语句Inject会失效 #34

lizhangqu opened this issue Nov 25, 2019 · 1 comment

Comments

@lizhangqu
Copy link

比如if当前行,forin当前行和循环体内代码,for当前行和循环体内代码都无法Inject代码,见下面的case

    int a = 10;
    if (a > 5) {
      print('[KWLM]:if1');
    }
    print('[KWLM]:a');
    for (Observer o in observers) {
      print('[KWLM]:Observer1');
      o.onChanged();
      print('[KWLM]:Observer2');
    }
    print('[KWLM]:b');
    for (int i = 0; i < 10; i++) {
      print('[KWLM]:for i $i');
      print('[KWLM]:for i $i');
    }
    print('[KWLM]:c');

if (a > 5) 这一行前面即int a = 10;这一行后面无法用Inject注入代码

for (Observer o in observers) {这一行前面即print('[KWLM]:a');这一行后面无法用Inject注入代码

循环体内无法用Inject注入代码

    for (Observer o in observers) {
      print('[KWLM]:Observer1');
      o.onChanged();
      print('[KWLM]:Observer2');
    }

for (int i = 0; i < 10; i++) {这一行前面即print('[KWLM]:b');这一行后面无法用Inject注入代码

循环体内无法用Inject注入代码

    for (int i = 0; i < 10; i++) {
      print('[KWLM]:for i $i');
      print('[KWLM]:for i $i');
    }

可能和这个函数没有处理for循环有关:

 static Node getNodeToVisitRecursively(Object statement) {
    if (statement is FunctionDeclaration) {
      return statement.function;
    }
    if (statement is LabeledStatement) {
      return statement.body;
    }
    if (statement is IfStatement) {
      return statement.then;
    }
    return null;
  }

可以在Inject注解上增加后向插入

if (statement2InsertPos != -1) {
          _curAopStatementsInsertInfo = null;
          statements.insertAll(statement2InsertPos, aopInsertStatements);
          _curAopLibrary = aopItemInfo.aopMember?.parent?.parent;
          visitNode(node);
          break;
}

后向插入取statement2InsertPos+1
这样就能在if语句和for循环语句前面一行插入,即插入上一行的后面。

@kangwang1988
Copy link
Contributor

@lizhangqu
Thanks for your feedback.
It would be appreciated if a minimal demo project to help reproduce this issue is given.
Besides, I would like to review it if you would like to file a pr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants