The Source for Java Technology Collaboration
User: Password:



Richard Bair's Blog

January 2007 Archives


Remi's Property Proposal

Posted by rbair on January 24, 2007 at 09:39 AM | Permalink | Comments (27)

This is going to be a short one. To begin, be sure to read Remi Forax's blog about his refined property proposal. Read it? Good.

I think Remi is really on the right track, and I can't wait to get the next build of his javac compiler to play with it. In the week or so since I last blogged on properties, I've had a chance to consider and reconsider a bunch of alternatives. I came up with the same basic set of criteria that Remi has. But there are a couple things in his proposal that I'd tweak.

In my last entry, I made the case for needing the property feature to support auto-generating getters/setters so that current frameworks that rely on the JavaBeans spec continue to work with the new properties. Frameworks such as JSR 295 (beans binding), Spring, JPA, etc. jhook pointed out that we actually don't need to do this: as long as the java.beans.Introspector can read the new Property information in the class file, then frameworks would continue to work perfectly, without the need of introducing auto-generated getters/setters.

After thinking about it more, I am not in favor of auto generated getters and setters. It just feels wrong. You can read the source file and there will be no method "setFoo". Yet in another source file, there you are, calling "setFoo"! Called me old fashioned, call me over the hill. It just doesn't sit right.

Instead, I'm of the opinion that if properties are introduced in the language, they should be a clean implementation. Let the Introspector bridge the gap. And stick with Java's core language tenet: keep it readable!

This is why I really like Remi's proposal (which Joe Nuxoll also proposed at one point):


    public property String foo; #read/write property
    public property String bar get; #readonly property
    public property String baz set; #writeonly property
    public property String wow get() {return bar;} set(String w) {bar = w;} #property with code

He then goes on to talk about how to access a properties value. Here I part ways with Remi for a bit. I still think the "." accessor is the best choice. Thus:


    obj.foo = "Hello";
    String s = obj.foo;

One argument that has been made against the use of the "." notation is that it may have unintended side effects for the person making the call. Yes, it may. But then again, obj.setFoo("Hello") may have unintended side effects. If properties receive special attention in the javadoc (as I'm sure they would), then I see the unintended-side-effect argument to be much weaker. Want to know if there is a side effect? Read the docs! When you read the source code or docs for the class that declares the property, it is very clear what is going on.

Remi, Stephen Colebourne, Evan, and others have talked about a Property Literals (aka Property References, etc) which I think is a really, really great thing to be thinking about. Here is my minor tweak to Remi's proposal: use "#" as the operator instead of the ".". We already use # in the javadocs, so it would be a natural and consistent use. For example:


class Person {
    public property surname;
    public property givenName;
    
    /**
      * @return the #surname and #givenName, formatted
      */
    public String formatName(String msgFormatPattern) { ... }
}

...

String s = person.surname; //yields the value of the surname property
Property p = person#surname; //returns the Property object for the surname property
p = Person#surname; //also returns the Property object for the surname property
Method m = Person#formatName(String); //holy toledo! Returns a Method object!

As you can see, there are some benefits to introducing a new operator for this task. # to me, makes a lot of sense.

That's it for now. I hope to be able to chew on this problem a bit more.



Properties in Java? Hoorah!

Posted by rbair on January 08, 2007 at 02:09 PM | Permalink | Comments (45)

Properties in Java? Awesome! As with any new language feature, there has been a lot of debate over whether this is an improvement to the language, or a detriment. And of course, every language-designer-wannabe (myself included!) is pounding the pulpit, declaring the One True Way to Property bliss. Well, sit back and enjoy as I pound the pulpit. Because seriously, I really do have the right solution! I promise!

A Brief Overview of JavaBeans

The JavaBeans spec has been with us for quite a while. Virtually everybody knows about the JavaBeans POJO pattern:


public class Person {
    private String surname;
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
}

Before throwing any monkey wrenches into the works, I'm going to explain a bit about some of the darker corners of the JavaBeans spec. At least, darker to the majority of developers out there.

Many of you are probably familiar with the Reflection APIs. For example, given the class above, I could call the "getSurname()" method using Reflection like so:


    Method m = Person.class.getMethod("getSurname");
    String surname = (String)m.invoke(personInstance);

Built above these core APIs is the JavaBeans Introspector, BeanInfo, PropertyDescriptor, and associated classes. These classes inspect an Object for methods that follow the JavaBean pattern, and create various descriptors automatically representing these "properties". You can even write your own PropertyDescriptors, or specify your own BeanInfos, if you don't want them to be automatically generated. Here's an example which get's all the properties for a bean and prints the name of the property and its value to System.out:


    BeanInfo info = Introspector.getBeanInfo(Person.class);
    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
        String name = pd.getName();
        Method m = pd.getReadMethod();
        if (m != null) {
            System.out.println(name + "=" + m.invoke(personInstance));
        }
    }

The java.beans package is widely used by frameworks and GUI builders. Swing is a huge user of the Bean APIs and patterns. JSR 295 (beans binding) is based on these APIs as well.

Continue Reading...



Five Little Things

Posted by rbair on January 02, 2007 at 05:09 PM | Permalink | Comments (6)

I was just catching up with my emails, and all those blogs, and noticed that my friend Romain Guy tagged me. Now I gotta share 5 little know facts about myself. With no attempt at cleverness:

  • I dated my wife for 6 weeks before we got engaged. 1 month later we were married. 1 month later we were expecting our first :-)
  • I spent 2 years in Atlanta Georgia (and thereabouts) as a missionary, including 6 months in the inner city. Living and working in the inner city was a really great experience
  • The scar on my face has been with me since I was 10 months old. I fell on broken glass. I had been walking since I was 8 months old. Must have scared my mom to death
  • (American) football is my favorite sport. I love the strategy, the athletics, and the sheer willpower needed to succeed. Too bad the college season has (almost) come to a close. Hoorah for Boise State! Hoorah for all the underdogs!
  • When I was about 8 I was run over by a caterpillar tractor. Luckily, it only got my leg. Even more luckily, it didn't even break my leg. Hurray for soft dirt!

Well, I'm going to go ahead and tag Chris Campbell, Ben Galbraith, and Ethan Nichols. No tagbacks!





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds