0

Is there a simple way to print any method's signature once it's entered?

My current naive solution is to add a System.out.print("method signature") statement before all the methods which prints its name, but then I have to do that even for every getter and setter which is quite tedious.

steoiatsl
  • 1,892
  • 2
  • 22
  • 39
  • You could write a custom annotation to do so, but you shouldn't necessarily be doing this. The performance impact is pretty significant. – Christopher Schneider Mar 28 '16 at 20:58
  • 5
    There are several ways to do this. Check out this answer: http://stackoverflow.com/questions/442747/getting-the-name-of-the-current-executing-method – kevingreen Mar 28 '16 at 20:59
  • I'm not sure what you're doing, but I suspect you actually want to use a debugger, profiler, or logging library instead. – Jeffrey Bosboom Mar 28 '16 at 22:56

1 Answers1

0

this library https://github.com/JakeWharton/hugo is doing exactly what u want.

It is for android but if you not building for that it can be changed easily for your needs. To remove android dependencies just replace those includes:

import android.os.Build;
import android.os.Looper;
import android.os.Trace;
import android.util.Log;

and where

android.util.Log

is used just put your logs instead.

here is a fast test for java only with an example

It uses aspectj https://eclipse.org/aspectj/ to place logs on specific points.

the use of aspects is quite neat and clean for example:

@Aspect
public class Hugo {
  @Pointcut("execution(@hugo.weaving.DebugLog * *(..))")
  public void method() {}

  @Pointcut("execution(@hugo.weaving.DebugLog *.new(..))")
  public void constructor() {}

Defines pointcuts where logs will be injected. In this example it looks for methods and constructors with annotation @hugo.weaving.DebugLog

Then

  @Around("method() || constructor()")
  public Object logAndExecute(ProceedingJoinPoint joinPoint) throws Throwable {
    enterMethod(joinPoint);

    long startNanos = System.nanoTime();
    Object result = joinPoint.proceed();
    long stopNanos = System.nanoTime();
    long lengthMillis = TimeUnit.NANOSECONDS.toMillis(stopNanos - startNanos);

    exitMethod(joinPoint, result, lengthMillis);

    return result;
  }

Defines how we want to log stuff and what to log. In 'enterMethod' we log method signature in 'exitMethod' we can log the result and the time spent in this method.

It is pretty simple and clean and at the same time quite powerful as is AspectJ intention.

kalin
  • 3,546
  • 2
  • 25
  • 31
  • 1
    :) i believe now i do. adding some more info... – kalin Mar 28 '16 at 21:37
  • Well, you should ask yourself this: what will happen with my answer when the link went down and won't be available anymore. You linked a library and explained how to use it, but your answer will still be obsolete. So instead you should explain how that library solves OPs question ... so how it prints the desired method names. You don't need to post the whole source code (since this may also infringe the copyright), but small extracts with the most important lines seem to be ok (especially since you linked and named the source library). – Tom Mar 28 '16 at 22:18