Avos | Text recognition | SpiralSynthModular | GPhoto | Verispeed | Schemix | JMK | This |
JavaMacrosKit
Summary Language: Java Date: 2004 Platform: Any JVM |
I am putting together a library of useful Java macros, using the Java Syntax Extender. They are all released under the GPL. You can get a (very alpha) copy of the library here. The original paper on the Java Syntax Extender is a great introduction to the subject. My favorite macro in the kit (so far!) is one that handles simple threading for you. It gives you a concurrently{} block that spawns a new Thread for each statement in the block, then keeps track of all of those Threads and waits for them all to finish before the block exits. The following code: import java.io.*; import java.util.*; public class Example { public static void main( String[] args ) { concurrently { for( int i = 0; i < 100; i++ ) { System.out.println( "Statement 1: " + i ); try{ sleep( 100 ); } catch( Exception e ) {} } for( int i = 0; i < 100; i++ ) { System.out.println( "Statement 2: " + i ); try{ sleep( 100 ); } catch( Exception e ) {} } } } } gets expanded by the macro to the following: import java.io.*; import java.util.*; public class Example { public static void main( String [] args ) { java.util.LinkedList __threads8 = new java.util.LinkedList(); (new Thread () { public Thread register( LinkedList list ) { list.add( this ); return this; } public void run() { for( int i = 0; i < 100; i++ ) { System.out.println( "Statement 1: " + i ); try { sleep( 100 ); } catch( Exception e ) { } } } } ).register(__threads8).start(); (new Thread() { public Thread register( LinkedList list ) { list.add( this ); return this; } public void run() { for( int i = 0; i < 100; i++ ) { System.out.println( "Statement 2: " + i ); try { sleep( 100 ); } catch( Exception e ) { } } } } ).register(__threads8).start(); java.util.Iterator __i9 = __threads8.iterator(); while(__i9.hasNext()) { try { ((Thread) __i9.next()).join(); } catch( Exception __e10 ) { } } } } I know which one I'd rather write (and read!). Another couple of interesting macros I've written are ones that let you write things like for( int i = 0; i < 100; i++ ) { System.out.println( lambda(int x){return "x = " + x;}( i ) ); } LinkedList foo = list( "A", "B", "C" ); LinkedList bar = map( .concat(" is a letter"), foo ); LinkedList baz = map( lambda(String x){return "The letter " + x;}, foo ); I haven't included these in the kit yet, as the code's not very tidy! |