0

I want to create a template in Eclipse for printing a log starting from the method signature in the following way:

    void myMethod(Type1 param1, Type2 param2){

         logger.info("myMethod() - [param1: " + param1 + ", param2: " + param2 + "]");

    }

I tried to use the ${enclosing_method_arguments} variable, but I cannot find yet a solution.

Matt
  • 14,906
  • 27
  • 99
  • 149
chrisblo
  • 768
  • 1
  • 13
  • 30

3 Answers3

0

Have a look here to try it with Reflection API

or maybe you want to change your method to use ellipses(...) as;

void myMethod(Object... params){
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append("myMethod() - [");
        for (int i=0; i<params.length; i++) {
            strBuilder.append("param"+(i+1)+": "+params[i]+",");
        }
        strBuilder.deleteCharAt(strBuilder.length()-1); //last comma deleted
        strBuilder.append("]");
        logger.info(strBuilder.toString());

   }
Community
  • 1
  • 1
Yusuf K.
  • 4,195
  • 1
  • 33
  • 69
0

I don't think this is possible using only templates (unless you want to start parsing the generated string).

Take a look at this Eclipse plugin https://github.com/dernasherbrezon/eclipse-log-param. It should be able to produce (something close to) the desired result.

Note: haven't tried it myself.

nyname00
  • 2,496
  • 2
  • 22
  • 25
0

Best I could do with only Eclipse templates is this:

System.out.println("[FINE] ${enclosing_type}.${enclosing_method}(${enclosing_method_arguments}) = " + Stream.of(${enclosing_method_arguments}).map(Object::toString).collect(Collectors.joining(", ")));

This creates something like:

private void updateRenderScales(double sx, double sy) {
    System.out.println("[FINE] Window.updateRenderScales(sx, sy) = " + Stream.of(sx, sy).map(Object::toString).collect(Collectors.joining(", ")));
}
john16384
  • 7,800
  • 2
  • 30
  • 44