Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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 2771 times)
0 Members and 1 Guest are viewing this topic.
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


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: 56
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

Senior Member


Projects: 1
Exp: 15 years


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!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 742
Projects: 4
Exp: 16 years


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
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.

ctomni231 (33 views)
2014-07-18 06:55:21

Zero Volt (29 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (26 views)
2014-07-16 23:30:00

Cero (41 views)
2014-07-16 00:42:17

Riven (43 views)
2014-07-14 18:02:53

OpenGLShaders (31 views)
2014-07-14 16:23:47

Riven (30 views)
2014-07-14 11:51:35

quew8 (29 views)
2014-07-13 13:57:52

SHC (65 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!