8

Is there any way to have a template (Java -> Editor -> Templates) in Eclipse that generate something like this

debug("methodName arg1=" + arg1 + " arg2=" + arg2 + " arg3=" + arg3);

When used in a method. For instance:

public void setImage(long rowId, long contactId, String thinggy) {
   // invoking the template here, produces this:
   debug("setImage rowId=" + rowId + " contactId=" + contactId + " thinggy=" + thinggy);
}

I couldn't find a way to do that with the standard template UI, maybe there exists a plugin to do this kinds of things?

BoD
  • 10,838
  • 6
  • 63
  • 59

5 Answers5

10

This is a start:

debug("${enclosing_method_arguments}: ", ${enclosing_method_arguments});

which produces the following:

debug("arg1, arg2, arg3: ", arg1, arg2, arg3);

I haven't found a way to separate out each argument. I found this page that ran into the same problem.

For other Eclipse template stuff, look at this question.

Community
  • 1
  • 1
Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
6

I guess it's probably a bit too late to answer this question but maybe my answer will help someone :


System.out.println(String.format("%tH:% %s", java.util.Calendar.getInstance(), "${enclosing_package}.${enclosing_type}.${enclosing_method}(${enclosing_method_arguments})"));
String[] lArgsNames = new String("${enclosing_method_arguments}").split(", ");
Object[] lArgsValues = new Object[] {${enclosing_method_arguments}};
for (int i = 0; i < lArgsValues.length; i++) {
    System.out.println("\t" + (lArgsValues[i] != null ? ("(" + lArgsValues[i].getClass().getSimpleName() + ") \"" + lArgsNames[i] + "\" = \"" + lArgsValues[i] + "\"") : "\"" + lArgsNames[i] + "\" is null"));
}

For this method :


public void foo(boolean arg){
    // ...
}

the output would be:


18:43:43:076 > any.package.AnyClass.foo(arg)
    (Boolean) "arg" = "true"

This code seems to be able to handle any object, primitive type and null value. And yes it's a little bit complicated for the purpose!

loak
  • 61
  • 1
  • 1
  • There's never too late for good answer :) And yes, this code does what should, but it's way too painful to be used. – dantuch Mar 27 '12 at 20:16
5

I've made small plugin that adds nicely formatted variable:

https://github.com/dernasherbrezon/eclipse-log-param

Eclipse doesnt provide any information about parameter type, so there is no Arrays.toString(...)

dernasherbrezon
  • 848
  • 7
  • 16
  • Thanks a lot for this, it looks very promising. Unfortunately I can't seem to make it work. I installed it on my eclipse, and I don't see the new formatted_method_parameters variable :( - Any idea? – BoD Jan 22 '12 at 14:17
  • Ok I've found it now: for context, you must not use 'Java' (like the main page of the plugin says) but 'Java statements' instead. Then the variable is there. I accept this answer, and congratulations and thanks for this plugin. Minor request: it would be nice to be able to configure the output ;) For instance I prefer "var=" + var instead of "var: " + var. – BoD Jan 22 '12 at 14:26
  • do i have to install a plugin for this or something. please give a little instruction. `formatted_method_parameters` there is no such variable available in template – Saif Nov 27 '15 at 12:59
  • does this support eclipse Marks – Saif Nov 27 '15 at 13:21
4

or template=

if (aLog.isDebugEnabled()) {
  aLog.debug(String.format("${enclosing_method}:${enclosing_method_arguments}".replaceAll(", ", "=%s, ")+"=%s", ${enclosing_method_arguments}));
}

gives

public static void hesteFras(boolean connect, Object ged, String frans, int cykel) {
    if (aLog.isDebugEnabled()) {
        aLog.debug(String.format("hesteFras: connect, ged, frans, cykel".replaceAll(", ", "=%s, ") + "=%s",
                connect, ged, frans, cykel));
    }

which for

hesteFras(false, null, "sur", 89);

gives a log statement:

hesteFras: connect=false, ged=null, frans=sur, cykel=89
Aksel
  • 75
  • 7
  • Damn that's clever! I was trying to figure out how to get the right number of %s groups in there and finally gave up and appended them all in one clump as "Arrays.toString(new Object[]{${enclosing_method_arguments}});" which works but appends them all at the end so you end up with (p1name,p2name)=[p1Value, p2Value] in your display--yours is much better! – Bill K May 24 '16 at 21:36
2

The eclipse-log-param plugin is useful to avoid having to type the logging line manually. However, it would be even nicer to have the line added automatically for all new methods.

Is this even possible? It looks like in Windows->Preferences->Code Style->Code Templates->Code there are ways to configure automatically added code like auto-generated methods ("Method body" template, which has an "Auto-generated method stub" comment). But there's no way to configure new methods which are not generated.

Furthermore, the variable formatted_method_parameters is not available when editing the template for "Method body".

Carmen
  • 1,574
  • 17
  • 18