【问题标题】:Template to show method name and parameter values in Eclipse在 Eclipse 中显示方法名称和参数值的模板
【发布时间】:2011-04-03 05:13:47
【问题描述】:

有什么方法可以在 Eclipse 中生成类似这样的模板(Java -> Editor -> Templates)

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 的方法,也许有一个插件可以做这种事情?

【问题讨论】:

    标签: eclipse templates logging


    【解决方案1】:

    这是一个开始:

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

    产生以下内容:

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

    我还没有找到一种方法来分离每个参数。我发现this page 遇到了同样的问题。

    有关其他 Eclipse 模板的内容,请查看question

    【讨论】:

      【解决方案2】:

      我想现在回答这个问题可能为时已晚,但也许我的回答会对某人有所帮助:

      
      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"
      

      这段代码似乎能够处理任何对象、原始类型和空值。是的,它的目的有点复杂!

      【讨论】:

      • 得到好的答案永远不会太晚 :) 是的,这段代码做了应该做的事情,但是使用起来太痛苦了。
      【解决方案3】:

      我制作了一个小插件,可以添加格式良好的变量:

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

      Eclipse 没有提供任何关于参数类型的信息,所以没有 Arrays.toString(...)

      【讨论】:

      • 非常感谢,它看起来很有希望。不幸的是,我似乎无法让它工作。我在 Eclipse 上安装了它,但没有看到新的 formatted_method_parameters 变量:( - 知道吗?
      • 好的,我现在找到了:对于上下文,您不能使用“Java”(如插件主页所述),而是使用“Java statements”。那么变量就在那里。我接受这个答案,祝贺并感谢这个插件。次要要求:能够配置输出会很好;)例如,我更喜欢“var =” + var 而不是“var:” + var。
      • 我是否必须为此或其他东西安装插件。请给一点指示。 formatted_method_parameters模板中没有这样的变量
      • 这个支持eclipse Marks吗
      【解决方案4】:

      或模板=

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

      给出一个日志语句:

      hesteFras: connect=false, ged=null, frans=sur, cykel=89
      

      【讨论】:

      • 该死的太聪明了!我试图弄清楚如何在其中获得正确数量的 %s 组,最后放弃并将它们全部附加到一个丛中作为“Arrays.toString(new Object[]{${enclosure_method_arguments}});”它有效,但在最后附加了它们,所以你最终会在你的显示中显示 (p1name,p2name)=[p1Value, p2Value] - 你的要好得多!
      【解决方案5】:

      eclipse-log-param 插件对于避免手动输入日志行很有用。但是,为所有新方法自动添加该行会更好。

      这甚至可能吗?看起来在 Windows->Preferences->Code Style->Code Templates->Code 中有一些方法可以配置自动添加的代码,如自动生成的方法(“方法主体”模板,它有一个“自动生成的方法存根”注释)。但是没有办法配置没有生成的新方法。

      此外,变量 formatted_method_parameters 在编辑“方法主体”的模板时不可用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-24
        • 2012-04-08
        • 1970-01-01
        • 1970-01-01
        • 2014-01-03
        • 2018-09-04
        • 1970-01-01
        相关资源
        最近更新 更多