With AspectJ, you can easily print the signature for the class that calls System.out.
The following is a simple example that will be considered by TraceAspect. The important part is that the class is in the demo package and calls System.out. The aspect will also list all System.out calls from all classes to a subpackage of any depth in the demo package.
package demo;
public class DemoClass {
public void demo() {
System.out.println("inside demo method..");
}
public static void main(String[] args) {
new DemoClass().demo();
}
}
System.out, , :
@Aspect
public class TraceAspect {
@Pointcut("call(* java.io.PrintStream.*(..))")
public void sysoutPointcut() {
}
@Pointcut("within(demo..*)")
public void packagePointcut() {
}
@Before("sysoutPointcut() && packagePointcut()")
public void beforeSysoutCallInsideDemoPackage(JoinPoint joinPoint) {
System.out.print(joinPoint.getThis().getClass().getName() + ":"
+ joinPoint.getSourceLocation().getLine() + " - ");
}
}
DemoClass:
demo.DemoClass:6 - inside demo method..
Eclipse AspectJ " β AspectJ". .
AspectJ @AspectJ .