Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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 2048 times)
0 Members and 1 Guest are viewing this topic.
Offline ryanm
« League of Dukes »

Senior Member


Projects: 1


Used to be bleb


« Posted 2009-11-05 14: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
1  
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:
1  
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.:
1  
2  
3  
4  
5  
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:
1  
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: 48
Projects: 11


Monkey for a head


« Reply #1 - Posted 2009-11-05 15: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?

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline ryanm
« League of Dukes »

Senior Member


Projects: 1


Used to be bleb


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

Indeed you could.  Didn't think of that... Tongue
The method would look something like
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
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
1  
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!
Play the free demo of Revenge of the Titans!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 439
Projects: 4


Hand over your head.


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

Nice!

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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (132 views)
2013-05-17 21:29:12

alaslipknot (141 views)
2013-05-16 21:24:48

gouessej (171 views)
2013-05-16 00:53:38

gouessej (165 views)
2013-05-16 00:17:58

theagentd (175 views)
2013-05-15 15:01:13

theagentd (160 views)
2013-05-15 15:00:54

StreetDoggy (204 views)
2013-05-14 15:56:26

kutucuk (228 views)
2013-05-12 17:10:36

kutucuk (228 views)
2013-05-12 15:36:09

UnluckyDevil (231 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.142 seconds with 21 queries.