Eclipse’s systrace template is very handy. For the uninitiated: it’s where eclipse allows you to type “systrace”, or even “syst” for that matter, hit ctrl+space, and have eclipse fill in
System.out.println( "enclosingClass.enclosingMethod()" );
I often want to also print out the values of the arguments to the current call, but it’s tedious to go back into the generated template code and edit in all the " + arg1 + ", " + arg2 + " required.
Happily we can add a new template that effectively will do this for us:
Under Preferences/Java/Editor/Templates, add a new template that looks like this:
System.out.printf( "${enclosing_type}.${enclosing_method}( %1s, %2s, %3s, %4s )\n", ${enclosing_method_arguments} );
and call it something like traceargs4. This template will print the values of the method arguments (calling formatTo() on Formattable object args, and toString() on other objects). Unfortunately, the templates cannot detect the number of arguments, and so you’ll need to add in separate templates for different numbers of arguments, e.g.:
traceargs1: System.out.printf( "${enclosing_type}.${enclosing_method}( %1s )\n", ${enclosing_method_arguments} );
traceargs2: System.out.printf( "${enclosing_type}.${enclosing_method}( %1s, %2s )\n", ${enclosing_method_arguments} );
traceargs3: System.out.printf( "${enclosing_type}.${enclosing_method}( %1s, %2s, %3s )\n", ${enclosing_method_arguments} );
Note that you’ll get an runtime exception if you try to format more arguments than exist, e.g.: using traceargs4 in a 1-argument method.
An alternative/addition to having many separate traceargs templates is having one that is large enough for almost all methods, and that you trim to fit for smaller ones:
System.out.printf( "${enclosing_type}.${enclosing_method}( %1s, %2s, %3s, %4s, %5s, %6s, %7s, %8s${cursor} )\n", ${enclosing_method_arguments} );
will leave the cursor ready to delete the unwanted formatting variables.