commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Strachan" <james_strac...@yahoo.co.uk>
Subject [OT] thoughts on Java pre processor
Date Wed, 06 Mar 2002 01:55:25 GMT
This is kinda off topic but I've been musing lately about a preprocessor for
Java. One of the early attractions to me about Java some 5-6 years ago
coming from a mostly C++ background was that there was no pre processor and
obfuscation, so Java code was very easy to read.

However in theses groovy days of having Ant to do all our building, JUnit
for unit testing and the various logging packages for logging, we rarely
need much from an IDE these days but a basic editor (does anyone even use
debuggers any more?), so maybe diverging from the Java Language spec
wouldn't be too bad. .

So I was thinking that maybe a preprocessor might now gain momentum since

* it can be an Ant task and integrate seamlessly into any build
* it can spit out regular Java code so even though the pre processor would
be 'non standard' it could become useful.

So the aim of the preprocessor would be to allow us to add some syntax
suguar to Java to save us some typing and avoid redundancy. I just wondered
what others thoughts are. Here's a couple of ideas for some syntax sugar...


Easier properties
============
Imagine a typical property definition in a Java bean....

/**
 * This is a foo property
 */
private Foo foo;

/**
 * Gets the value of the foo property
  *
  * @return the foo property
  */
public Foo getFoo() {
    return foo
}

/**
 * Sets the value of the foo property
  *
  * @param foo the new value of the foo property
 */
public void setFoo(Foo foo) {
    this.foo = foo;
}


Its quite a lot to type and takes up quite a bit of code. Also notice the
redundancy in the javadoc comments. We should be able to just describe the
property and the javadoc could be generated. I'd quite like some syntax
sugar (similar to in C#) to allow the property to be javadoc commented just
once and to save typing. e.g.


/** This is a foo property */
private property Foo foo {
    get {
        return foo;
    }
    set {
        this.foo = value;
    }
}


For most simple properies this could be simplified as just

/** This is a foo property */
private property Foo foo;


Maybe even extending this to provide 'lazy construction' properties which
expand to look like this...

/**
 * @return the value of the foo property
 * which will be created lazily if it is null
 */
public Foo getFoo() {
    if ( foo == null ) {
        foo = createFoo();
    }
    return foo;
}

/**
 * Factory method to create a new Foo object
 * which allows derived classes to override this
 * behaviour
 */
protected Foo createFoo() {
    return new Foo();
}


This could maybe appear as syntax sugar as....

private lazy property Foo foo {
    create {
        return new Foo();
    }
}

The get & set methods could be defaulted if not specified.


Easier events
=========
Similar techniques to the above could be done to avoid developers having to
manage event listeners and write code to fire them etc.

foreach?
======
To help avoid repetive code of the form...

for ( Iterator iter = something.iterator(); iter.hasNext(); ) {
    Foo foo = (Foo) iter.next();
    foo.bar();
}

maybe introduce a C#-like foreach statement...

foreach( Foo foo in something.iterator() ) {
    foo.bar();
}


Just thinking aloud here (as always ;-). Thoughts?

James


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message