Sunday, March 23, 2008

Java gets some Properties.

There is a proposal to add properties to java. Properties will eliminate the need for getters/setters in the usual javabeans pattern style. I'm sure you wanna see some code examples, so as usual, i'll give you the way it's done currently, in this case following the JavaBeans spec of specifying a getter/setter. To run this code you'll need to go and check out the prototype compiler from the subversion repository at java.net. You will need to register. After you have done that type:
svn checkout https://kijaro.dev.java.net/svn/kijaro/branches/properties kijaro --username username --password yourpassword
replace username and yourpassword with the username and password you registered with.

Apache Ant is required to build the compiler.

Now to compile it (instructions are for users running linux;
if running windows i assume you know your OS properly to adjust this).
1) type cd kijaro/langtools/make
2) type ant -Dboot.java.home=$JAVA_HOME
3) now to compile the code you'll do /path/to/kijaro/bin/kijaro-javac /path/to/ClassName.java setting your classpath as needed.
4) To run this, you can either use /path/to/kijaro/bin/kijaro-java /path/to/ClassName or you can use your installed JRE and do: java /path/to/ClassName

That's all you need to know to run this code. Questions? email me.


/**
* Created: Mar 23, 2008 9:13:54 PM
*
* @author Robert O'Connor
*/
public class BeanExample {
private String userName;

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public static void main(String[] args) {
BeanExample example = new BeanExample();
example.setUserName("rob");
System.out.println(example.getUserName());
}
}

The getters/setters are boiler-plate code that just isn't necessary. Most IDEs will generate getters/setters for you with the click of a mouse; but still it makes your code verbose. Now I wrote three examples for properties, since there are three types: read-only,read-write and write-only. Each example showcases just one of those types for reasons of simplicity. First, I'll tackle the example of a read-only property.


/**
* Created: Mar 22, 2008 5:37:41 PM
*
* @author Robert O'Connor
*/
public class ReadOnlyPropertiesExample {
private String userName;
public property String name get { return userName; };

public ReadOnlyPropertiesExample() {
this.userName = "rob";
}

public static void main(String[] args) {
System.out.println(new ReadOnlyPropertiesExample().userName);
}

}

Already, you can see that we've removed a great deal; and it's not that as verbose as the JavaBean example. Now let's see what happens if I try and write to the property? It's a compile-time error; or at least it should be. As of the latest prototype compiler, it didn't complain and happily let me write to the property. Now let's see a properties example that behaves properly as defined by the spec.


/**
* Created: Mar 23, 2008 8:11:41 PM
*
* @author Robert O'Connor
*/
public class ReadWritePropertiesExample {

private String userName;
public property String name
get { return userName; }
set(String name) { userName = name; };

public static void main(String[] args) {
ReadWritePropertiesExample example = new ReadWritePropertiesExample();
example.userName = "rob"; // set the name
System.out.println(example.userName); // output it.
example.userName = "robby"; // set it to something different
System.out.println(example.userName); // output the new value
}
}

Okay, this is a read-write property; note that example.userName is NOT using direct field access. The final example is a write-only example; note that it is a compile-time error to attempt to retrieve the value; however the compiler currently let's you do it; which in my opinion is bad. I coded against the spec, so to circumvent that, I defined a getter (I know properties are supposed to eliminate those, but I needed a way to test that the value was set properly.)


/**
* Created: Mar 23, 2008 8:45:02 PM
*
* @author Robert O'Connor
*/
public class WriteOnlyPropertiesExample {
private String userName;
public property String name set;

/**
* this is used purely to check that the property set the field's value properly.
* @return the username
*/
public String getUserName() {
return userName;
}

public static void main(String[] args) {
WriteOnlyPropertiesExample example = new WriteOnlyPropertiesExample();
example.userName = "rob"; // we can only *SET* it since it's write-only
System.out.println(example.getUserName());
example.userName = "robby";
System.out.println(example.getUserName());
}

}

Okay, now that you've gotten a taste of what's to come (if this makes it passed the JCP that is!) go out and play!

No comments: