Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (549)
Games in Android Showcase (135)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Eclipse tip: better systrace template  (Read 3023 times)
0 Members and 1 Guest are viewing this topic.
Offline ryanm

Senior Devvie

Projects: 1
Exp: 15 years

Used to be bleb

« Posted 2009-11-05 13:42:34 »

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.
Offline Orangy Tang

JGO Kernel

Medals: 57
Projects: 11

Monkey for a head

« Reply #1 - Posted 2009-11-05 14:23:06 »

That's pretty handy. Smiley

If you wrote your own System.out.println couldn't you write a var-args version that didn't need different base strings, and therefore use one macro all the time without editing it?

[ - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline ryanm

Senior Devvie

Projects: 1
Exp: 15 years

Used to be bleb

« Reply #2 - Posted 2009-11-05 15:25:50 »

Indeed you could.  Didn't think of that... Tongue
The method would look something like
public class SysTrace
   public static void trace( String prefix, Object... args )
      StringBuilder buff = new StringBuilder( prefix );
      buff.append( "( " );
      for( int i = 0; i < args.length - 1; i++ )
         buff.append( args[ i ] );
         buff.append( ", " );
      buff.append( args[ args.length - 1 ] );
      buff.append( " )" );
      System.out.println( buff.toString() );

and the template like
SysTrace.trace( "${enclosing_type}.${enclosing_method}", ${enclosing_method_arguments} );

It won't automatically insert an import for SysTrace - templates are just text replacement, but I've got eclipse set to organise imports on every save anyway.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

« JGO Overlord »

Medals: 875
Projects: 4
Exp: 16 years

Hand over your head.

« Reply #3 - Posted 2009-11-05 15:40:33 »


It won't automatically insert an import for SysTrace - templates are just text replacement, but I've got eclipse set to organise imports on every save anyway.
Just use the fully specified class name.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.


Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

basil_ (25 views)
2015-01-17 22:29:32

wxwsk8er (21 views)
2015-01-16 21:42:21

wxwsk8er (19 views)
2015-01-16 20:44:20

basil_ (25 views)
2015-01-14 09:46:59

basil_ (20 views)
2015-01-14 09:46:52

wxwsk8er (30 views)
2015-01-13 20:42:16

wxwsk8er (33 views)
2015-01-13 03:23:59

vctt (53 views)
2015-01-12 03:21:07

wxwsk8er (30 views)
2015-01-12 03:11:59

vctt (26 views)
2015-01-12 03:10:30
2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!