代码之家  ›  专栏  ›  技术社区  ›  BoD

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

  •  8
  • BoD  · 技术社区  · 15 年前

    在Eclipse中有一种模板(Java -gt,编辑器和gt;模板)可以生成这样的东西吗?

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

    在方法中使用时。例如:

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

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

    5 回复  |  直到 8 年前
        1
  •  5
  •   dernasherbrezon    8 年前

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

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

    Eclipse不提供任何关于参数类型的信息,因此没有数组.ToString(…)

        2
  •  10
  •   Community Mohan Dere    9 年前

    这是一个开始:

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

    产生以下结果:

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

    我还没有找到一种方法来区分每一个论点。我发现 this page 也遇到了同样的问题。

    对于其他Eclipse模板的内容,请看这个 question .

        3
  •  5
  •   loak    15 年前

    我想现在回答这个问题可能有点晚了,但我的回答可能会有帮助:

    
    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"
    

    此代码似乎能够处理任何对象、基元类型和空值。是的,这有点复杂!

        4
  •  4
  •   Aksel    15 年前

    或模板=

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

    给予

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

    哪一个

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

    给出LOG语句:

    hesteFras: connect=false, ged=null, frans=sur, cykel=89
    
        5
  •  1
  •   Carmen    13 年前

    Eclipse日志参数插件可以避免手动键入日志行。但是,自动为所有新方法添加行会更好。

    这是可能的吗?在Windows->首选项->代码样式->代码模板->代码中,有一些方法可以配置自动添加的代码,如自动生成的方法(“方法体”模板,其中包含“自动生成的方法存根”注释)。但是没有办法配置未生成的新方法。

    此外,在编辑“方法体”模板时,变量格式的“方法体”参数不可用。