Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template to show method name and parameter values in Eclipse

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?

like image 317
BoD Avatar asked Aug 24 '10 14:08

BoD


2 Answers

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.

like image 72
Kelly S. French Avatar answered Sep 19 '22 06:09

Kelly S. French


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!

like image 33
loak Avatar answered Sep 19 '22 06:09

loak