|
|
||
Richard Bair's BlogJanuary 2007 ArchivesRemi's Property ProposalPosted 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):
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:
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, 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:
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 JavaBeansThe JavaBeans spec has been with us for quite a while. Virtually everybody knows about the JavaBeans POJO pattern:
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:
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:
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. Five Little ThingsPosted 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:
Well, I'm going to go ahead and tag Chris Campbell, Ben Galbraith, and Ethan Nichols. No tagbacks! | ||
|
|