Eclipse Can’t Refactor My Log Method ??!! What ???


Eclipse will not be able to re-factor the following method ???:

public void log(String Message);

into

public void log(String Message, String methodName);

with automatic replace of methodName parameter by the enclosing method name. For example the following call in the main method

log("my message");

will be replaced by

log("my message", "main");

I am sur I am missing something. Where ??

Advertisements

3 thoughts on “Eclipse Can’t Refactor My Log Method ??!! What ???

  1. Yes but when I add a new String parameter I can just have an “hardocded” default value. For example if I choose “main” all the calls will be replaced by “main” and this is not my goal. I want all the calls to be replaced with the calling method’s name. Thus I need a variable like ${enclosing_method} used in JDT templates. After several searches it’s seems impossible to do that, do you confirm ?

  2. A different approach to this problem that I have used effectively is to replace the log method rather than attempting to refactor:

      public void log(String message) {
        StackTraceElement[] frames = Thread.currentThread().getStackTrace();
        String methodName = frames[2].getMethodName();
        log(message, methodName);
      }
     

    This approach is rather efficient and avoids needing to explicitly set the method name in the arg to the log(String message, String methodName) call, which will often be missed in subsequent method-rename refactoring.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s