0

Guys I need a point cut that filter me the called function by a specific class.

public aspect intLogin {
    private capture c = new capture();
    pointcut login() : execution(public * login(..)) 
    before ():login(){
        c.print();
    }
}

This is my aspect, I want to know which class call login function. Can you help me?

Michael
  • 41,989
  • 11
  • 82
  • 128
  • What are you asking? I can't see a question here other than "Can you help me?" – Michael Nov 02 '18 at 10:51
  • @Michael i need to write a pointcut that intercept a class. Something like this: pointcut login() : execution(public * login(..)) that return me the class called function.. – Cristian Daniele Nov 02 '18 at 12:57
  • You seem to be wanting to know what is the class of the method that is CALLING the login function. Could this be the same as determining the caller class without AOP? https://stackoverflow.com/questions/1696551/how-to-get-the-name-of-the-calling-class-in-java It may not be that simple, because AOP frameworks inject a lot of stuff on to the call stack. Hopefully someone else has a better answer. – Wheezil Nov 02 '18 at 14:00
  • @Wheezil Yes, i want to know what class is calling the login function. I need to work with aspect.. – Cristian Daniele Nov 02 '18 at 14:06
  • Unless someone comes up with a real answer, I'd start with examining the results of "new Execption().printStackTrace()" in your intercept function. You may need to manually walk the stack backwards until you reach something that is not part of the AOP framework – Wheezil Nov 02 '18 at 14:36

2 Answers2

1

Helper class called by all the others:

package de.scrum_master.app;

public class Other {
  public void doOther() {}
}

Driver application with all kinds of inner classes:

Here we have

  • non-static inner class,
  • static inner class,
  • local inner class,
  • anonymous class
  • and of course the normal class.
package de.scrum_master.app;

public class Application {
  public String foo(int number) {
    new Other().doOther();
    return "foo";
  }

  public class MyInner {
    public void doSomething() {
      new Other().doOther();
    }
  }

  public static class MyStaticInner {
    public void doSomethingElse() {
      new Other().doOther();
    }
  }

  public static void main(String[] args) {
    new Application().foo(11);
    new Application().new MyInner().doSomething();
    new Application.MyStaticInner().doSomethingElse();

    class LocalInner {
      public void doWhatever() {
        new Other().doOther();
      }
    }
    new LocalInner().doWhatever();

    new Runnable() {
      @Override public void run() {
        new Other().doOther();
      }
    }.run();
  }
}

Aspect logging caller class names:

package de.scrum_master.aspect;

public aspect CallingClassLogger {
  before(Object caller) : !within(CallingClassLogger) && call(* *(..)) && this(caller) {
    System.out.println(caller.getClass().getName());
  }
}

Console log:

de.scrum_master.app.Application
de.scrum_master.app.Application$MyInner
de.scrum_master.app.Application$MyStaticInner
de.scrum_master.app.Application$1LocalInner
de.scrum_master.app.Application$1

Your aspect would have printed something like

Application.java:5
Application.java:11
Application.java:17
Application.java:28
Application.java:35

which is not so helpful IMO if you are interested in class names.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
-1

I solved the problem using thisJoinPoint.getSourceLocation(). The code is:

public aspect intLogin {
  private capture c = new capture();
  pointcut login(Object a) : call(public * login(..)) && (target(a)) && this(capture);
  before (Object x):login( spring.aop.so_52992365.intLogin) {
    String xString = x.toString();
    System.out.println("The class that is calling the function is:" + thisJoinPoint.getSourceLocation());
    c.print();
  }
}
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Your answer is wrong. `getSourceLocation()` prints something like `Application.java:28`, i.e. the name of a Java class file plus a source code line. Even if that was good enough for you for normal classes, it would print the wrong "class name" for inner classes. I am going to post a correct answer which then you can accept instead of this one. – kriegaex Nov 25 '18 at 12:17