Use Cases for Java Properties

No more boring boilerplate

Write something like

/** The name of this widget */
 public property String name;

instead of

private String name;
 /**
  * Gets the name of this widget
  * @return the name of this widget
  */
 public String getName() { return name; }
 /**
  * Sets the name of this widget
  * @param name the new name of this widget
  */
 public void setName(String name) { this.name = name; }

Nicer syntax for property access

Use short-hand field-like LHS and RHS syntax, such as

my.property = your.otherProperty;

instead of

my.setProperty(your.getOtherProperty());

Other operators (such as -> or .@) have been proposed to distinguish property access from field access.

This link discusses the readability issue further: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6180800.

Javadoc should document properties

We think of properties as something higher-level than the get/set method implementation, and Javadoc should reflect that. We'd like

  • Fields
  • Properties
  • Constructors
  • Methods
See this proposal by Mikael Grev: http://www.javalobby.org/java/forums/t88090.html?start=0

Bound properties

Many bound property implementations are really boring. Set the new value, maybe check that it isn't null, maybe check that it is different from the old value, then set it and fire the property change.

When you have lots of bound properties (like in Swing), you'd like to reduce this drudgery.

The difficulty is that not all bound property implementations are exactly identical. Maybe a null value is ok. Should one test for equality? Is this property an oddball case that cannot be handled mechanically?

Many solutions have been proposed. Some are based on annotations, others on generics, such as

class Point {
    public property int x bound;
    public property int y bound;

    private <T> void propertyChange(Property<Point, T> prop, T oldValue, T newValue) {
      // do what you want here
    }
 }

Proposed by Remi Forax; details here: http://weblogs.java.net/blog/forax/archive/2007/01/property_reload.html

Get list of properties using core reflection (not JavaBeans) API

Properties are design features, and the get/set patterns are just an implementation detail. The design intent should be discoverable by runtime tools, in particular, core reflection.

This is proposed by Remi Forax (http://weblogs.java.net/blog/forax/archive/2007/01/property_reload.html)

public class Class<T> {
    public Property<T, ?>[] getProperties() {}
    public Property<T, ?> getProperty(String name) {}
  }

Avoid annotation ambiguity

In EJB3, you annotate persistent properties. But to annotate fields or getters, that is the question.

@Id
  private int id;

or

@Id
  public int getId() { ... }

Why should this be an issue? The developer just wants to annotate the property:

@Id
  public property int id;

Statically-verifiable property literals (for use in beans-binding and property reflection)

Expose Property/PropertyDescriptor objects through language syntax, which enables the compiler to validate that the property actually exists.

Syntax proposals abound: Bean.name, Bean#name also here, Bean@name, Bean->name Bean:name

Consider this example which binds the currentPrice property of the visualControl object to the value of the price property of the dataSource object.

binding = Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE, dataSource, ELProperty.create("${price}"), updatedControl, BeanProperty.create("currentPrice"));

If the currentPrice property is renamed, the above code will break and refactoring tools will not be able to catch it. With property literal syntax the above code looks like this:

binding = Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ_WRITE, dataSource, ELProperty.create("${price}"/* ^ */), updatedControl#currentPrice);

This idea (paraphrased here in Beans Bindings 1.0 code) originally came from Stephen Colebourne.

^ Note that the price property reference within the Unified EL expression still suffers from the same problem. Reifying this is an entirely different problem - support for lambda expressions with a code-as-data view ala C# 3.0 would be required.

Use a switch statement to select property in generic property (change) listeners

Remi Forax - http://docs.google.com/View?docid=dfhbvdfw_1f7mzf2

Bean bean=new Bean();
 bean.addPropertyListener(new PropertyListener<Bean>() {
   public <T> propertyChanged(Property<Bean, T> property, T oldValue, T newValue) {
     switch(property) {
       case name:
         frame.setTitle((String)newValue);
         break;
       case value:
         slider.setValue((Integer)newValue);
         break;
     }
   }
 }

Ignore nulls in a chain of accessors

input.address.postcode; // returns null if input.address is null

mentioned here http://jroller.com/scolebourne/entry/property_access_is_it_an

Property Inspection during Debugging

Properties can be listed along with fields in a debug "Watch" or "Locals" view.

The Visual Studio 2008 Locals view for example shows by default only public members (mostly properties) with non-public members hidden in a subtree, which can be expanded to view those members if necessary.

Version 1.99 last modified by Nikolay Botev on 01/04/2008 at 20:12

Comments 80

XWikiGuest | 29.12.2007 at 02:20 PM
I would have thought that the compatibility / encapsulation side of properties should be spelled out, i.e. the reason why the getter/setter convention exists of just public exposed field. I think this discusses that topic reasonably (in a python vs java context) - http://tomayko.com/articles/2005/01/20/getters-setters-fuxors.

XWikiGuest | 08.01.2008 at 10:53 PM
Hello, Your site is great. abra2 url=http://www.abra3.comabra3/url? http://www.abra1.com URL?http://www.abra4.com/URL? Regards, Valiintino Guxxi

XWikiGuest | 19.03.2008 at 06:32 PM

XWikiGuest | 19.03.2008 at 06:32 PM

XWikiGuest | 20.03.2008 at 01:39 PM
Good job., Phentermine 37.5mg 1131, Viagra qevcgt, Cheap Phentermine ygeuh, Viagra 032, Alprazolam vzfv,


XWikiGuest | 20.03.2008 at 03:17 PM
Thank You!, Phentermine %D, Viagra rgfmiy, Buy Viagra bmf, Alprazolam >:-OO, Buy Xanax 874,

XWikiGuest | 20.03.2008 at 04:05 PM
Best Wishes!, Tramadol 565213, Viagra :))), Cialis >:-], Buy Viagra 79130, Buy Viagra 4467,

XWikiGuest | 20.03.2008 at 04:51 PM

XWikiGuest | 20.03.2008 at 05:39 PM
Nise site., Phentermine gwyc, Cialis 244474, Tramadol Online lsl, Xanax nnor, Phentermine online %]],

XWikiGuest | 20.03.2008 at 06:29 PM
Thank You!, Phentermine 8-D, Buy Phentermine 2314, Cialis utnmnb, Cialis >:-P, Viagra kjnmhc,

XWikiGuest | 20.03.2008 at 07:18 PM

XWikiGuest | 20.03.2008 at 08:07 PM
Your Site Is Great!, Buy Viagra 8P, Tramadol 61872, Cialis cpdmma, Buy Viagra 8OOO, Xanax ruirrb,

XWikiGuest | 20.03.2008 at 08:58 PM
Nise site., Cheap Cialis 6084, Order Tramadol 33845, Viagra fpux, Buy Xanax eweu, Order Tramadol 8-P,

XWikiGuest | 20.03.2008 at 09:49 PM
Your Site Is Great!, Phentermine kpnfc, Order Cialis 907396, Tramadol 9007, Buy Phentermine %(, Buy Xanax xhlw,

XWikiGuest | 20.03.2008 at 10:40 PM

XWikiGuest | 20.03.2008 at 11:30 PM
Best Wishes!, Cialis 1685, Phentermine 37.5mg wrun, bad credit loans =-((, Buy Phentermine 8888, Buy Xanax 277290,


XWikiGuest | 21.03.2008 at 01:10 AM
Good job., Viagra qofi, Phentermine 37.5mg 900233, Viagra >:-)), Xanax kldmg, Viagra 719,

XWikiGuest | 21.03.2008 at 02:00 AM
Your Site Is Great!, Buy Tramadol 824861, Tramadol Online 3230, Phentermine ouf, Phentermine >:-[[[, Phentermine online lxgblh,

XWikiGuest | 21.03.2008 at 02:49 AM
Good job., Buy Phentermine 534965, Order Tramadol qrpc, Tramadol jpzpx, Buy Viagra :-[, Phentermine online >:((,

XWikiGuest | 21.03.2008 at 03:38 AM
Thank You!, Viagra 202628, Phentermine iamze, Phentermine %-P, Viagra Online 700200, Buy Viagra kzpy,

XWikiGuest | 21.03.2008 at 04:28 AM
Nise site., Phentermine 27843, Tramadol 914, Buy Phentermine 8(, Cheap Cialis 85004, Phentermine online gtke,

XWikiGuest | 21.03.2008 at 05:18 AM

XWikiGuest | 21.03.2008 at 06:09 AM

XWikiGuest | 21.03.2008 at 06:58 AM

XWikiGuest | 21.03.2008 at 07:48 AM

XWikiGuest | 21.03.2008 at 08:38 AM
Your Site Is Great!, Viagra myaifz, Cheap Cialis gjvh, Phentermine Without Prescription wtq, payday loans 526, Buy Xanax pown,

XWikiGuest | 21.03.2008 at 09:28 AM
Best Wishes!, Cialis 8101, Viagra Online yrv, Buy Cialis %, Buy Tramadol :" href="http://my.mediapost.com/buytramadol"?],

XWikiGuest | 21.03.2008 at 10:19 AM

XWikiGuest | 21.03.2008 at 11:10 AM
Thank You!, Phentermine 8-)), Buy Phentermine edg, Buy Phentermine 45483, Viagra Online zby, Cialis rnsel,

XWikiGuest | 21.03.2008 at 12:00 PM
I like your work!, Ultram das, Cheap Cialis ajmd, Buy Phentermine rwdyoz, Phentermine 8-((, Buy Xanax 521,

XWikiGuest | 21.03.2008 at 12:52 PM
Your Site Is Great!, Viagra Online =((, Order Cialis 5647, Viagra 231, Tramadol 574, Xanax hbtsi,

XWikiGuest | 21.03.2008 at 01:44 PM
Good job., buy-tramadol-for-lowest-pricesbuy-tramadol-online">Order Tramadol %, Buy Phentermine bdvrsk, Buy Viagra %DD, Viagra Online >:-" href="http://www.britney.com/xanax-buy-xanax-buy-xanax-alprazolam-lowest-prices"?,

XWikiGuest | 21.03.2008 at 02:34 PM
Thank You!,

XWikiGuest | 21.03.2008 at 03:24 PM
I like your work!,

XWikiGuest | 21.03.2008 at 04:12 PM
I like your work!,

XWikiGuest | 21.03.2008 at 05:02 PM
Good job.,

XWikiGuest | 21.03.2008 at 05:51 PM
I like your work!,

XWikiGuest | 21.03.2008 at 06:51 PM
I like your work!,

XWikiGuest | 21.03.2008 at 07:40 PM
Thank You!,

XWikiGuest | 21.03.2008 at 08:28 PM
Best Wishes!,

XWikiGuest | 21.03.2008 at 09:16 PM
Good job.,

XWikiGuest | 21.03.2008 at 10:05 PM
Your Site Is Great!,

XWikiGuest | 21.03.2008 at 10:54 PM
I like your work!,

XWikiGuest | 21.03.2008 at 11:42 PM
Your Site Is Great!,

XWikiGuest | 22.03.2008 at 12:30 AM
Best Wishes!,

XWikiGuest | 22.03.2008 at 01:18 AM
Thank You!,

XWikiGuest | 22.03.2008 at 02:07 AM
Nise site.,

XWikiGuest | 22.03.2008 at 02:55 AM
Your Site Is Great!,

XWikiGuest | 22.03.2008 at 03:43 AM
Best Wishes!,

XWikiGuest | 22.03.2008 at 04:33 AM
Your Site Is Great!,

XWikiGuest | 22.03.2008 at 05:18 AM
Good job.,

XWikiGuest | 22.03.2008 at 06:04 AM
I like your work!,

XWikiGuest | 22.03.2008 at 07:14 AM

XWikiGuest | 22.03.2008 at 07:14 AM

XWikiGuest | 22.03.2008 at 07:50 AM

XWikiGuest | 22.03.2008 at 07:50 AM

XWikiGuest | 22.03.2008 at 08:20 AM
Your Site Is Great!,

XWikiGuest | 22.03.2008 at 10:02 AM



XWikiGuest | 22.03.2008 at 10:50 AM
Your Site Is Great!, Order Cialis 314238, Tramadol Online 807024, Cialis 027, Order Tramadol 176, Phentermine 6761,

XWikiGuest | 22.03.2008 at 11:38 AM
Your Site Is Great!, Cialis 8], Buy Tramadol 8-], Phentermine 8831, Cialis :-[, Buy Phentermine %-),

XWikiGuest | 22.03.2008 at 11:38 AM
I like your work!, Phentermine online 4801, Viagra cvrxxx, Phentermine jsxvw, bad credit loans 221, Buy Xanax =-OO,

XWikiGuest | 22.03.2008 at 12:29 PM

XWikiGuest | 22.03.2008 at 12:30 PM
I like your work!, Viagra :-PPP, Phentermine :-]]], Phentermine online flwpb, Phentermine 15774, Tramadol 62540,

XWikiGuest | 22.03.2008 at 01:20 PM
Best Wishes!, Phentermine bche, Viagra 8))), Cheap Tramadol =-OOO, bad credit loans 052097, Tramadol :D,

XWikiGuest | 22.03.2008 at 01:21 PM
I like your work!, Viagra Online 42414, Buy Cialis 05953, Cialis >:-DDD, Buy Phentermine zlcp, Xanax 4917,

XWikiGuest | 22.03.2008 at 02:11 PM

XWikiGuest | 22.03.2008 at 02:11 PM
Nise site., Buy Cialis obxfnt, Phentermine czb, Phentermine :(, Tramadol wscgq, Phentermine widds,

XWikiGuest | 22.03.2008 at 03:02 PM

XWikiGuest | 22.03.2008 at 03:02 PM
Good job., Buy Cialis 8-PP, Viagra 8486, Viagra =[, payday loans %-)), Phentermine ewkxp,

XWikiGuest | 22.03.2008 at 03:55 PM
Your Site Is Great!, Phentermine bpumc, Viagra Online vfjt, Viagra ini, Cheap Tramadol 8PPP, Phentermine xyybun,

XWikiGuest | 22.03.2008 at 03:55 PM
Thank You!, Buy Cialis 04056, Phentermine online %-PPP, Phentermine 37677, Phentermine online =-[[[, Alprazolam 487,

XWikiGuest | 22.03.2008 at 04:46 PM
I like your work!, Tramadol >:-[[, Tramadol =-" href="http://www.cafepress.com/tramadol"?], Alprazolam %-O, Phentermine wan,

XWikiGuest | 22.03.2008 at 04:47 PM

XWikiGuest | 22.03.2008 at 05:37 PM
Best Wishes!, Cialis zxylu, Cheap Cialis 042334, Cialis %]], Tramadol Online efonq, Xanax rogz,

XWikiGuest | 22.03.2008 at 05:37 PM
Nise site., Phentermine :-)), Buy Viagra ibou, Buy Tramadol aio, Phentermine online %-P, Buy Viagra tqy,

XWikiGuest | 01.04.2008 at 08:12 PM
Add Comment...

Attachments 0

No attachments for this document

Creator: Administrator on 2007/10/28 19:39
This wiki is licensed under a Creative Commons license
1.1.1.5166