用于在Eclipse中显示方法名称和参数值的模板

BoD*_*BoD 8 eclipse logging templates

有没有办法在Eclipse中生成一个模板(Java - > Editor - > Templates)来生成这样的东西

debug("methodName arg1=" + arg1 + " arg2=" + arg2 + " arg3=" + arg3);
Run Code Online (Sandbox Code Playgroud)

在方法中使用时.例如:

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

我找不到使用标准模板UI的方法,也许有一个插件来做这种事情?

Kel*_*nch 10

这是一个开始:

debug("${enclosing_method_arguments}: ", ${enclosing_method_arguments});
Run Code Online (Sandbox Code Playgroud)

产生以下内容:

debug("arg1, arg2, arg3: ", arg1, arg2, arg3);
Run Code Online (Sandbox Code Playgroud)

我没有找到分离每个论点的方法.我发现这个页面遇到了同样的问题.

对于其他Eclipse模板的东西,请看这个问题.


小智 6

我想回答这个问题可能有点太晚了,但也许我的回答会对某人有所帮助:


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"));
}

对于这种方法:


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

输出将是:


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

此代码似乎能够处理任何对象,基本类型和空值.是的,它的目的有点复杂!


And*_*nov 5

我做了一个小插件,添加了格式很好的变量:

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

Eclipse不提供有关参数类型的任何信息,因此没有Arrays.toString(...)