Skip to main content

Properties proposal

2 replies [Last post]
noctarius
Offline
Joined: 2009-02-22
Points: 0

Hey guys,

not really sure if this is the right place (maybe one of the mailinglists would be better) but I'll thought just to try it.

Since some longer time I think about somethink like the properties in C# (or many other languages) and how they can be integrated with Java standards like Java Beans. I just want to show up a first draft and have some discussion about how this could be beautified or made more practical.

The current draft depends on some kind of compiler magic and new JSR 335 features. The following example is what I thought a property could look like (in some different kinds):

public class PropertyClass {
  property int propertyOne;
 
  property readonly propertyTwo;
 
  property int propertyThree {
    () -> { propertyThree + 1}
    (value) -> { propertyThree = value + 1 }
  }
 
  property int propertyFour {
    (value) -> { propertyFour = value; }
  }
}

In this example there are four different int properties in different flavors. Properties are meant to be implicitly private members using property accessor methods. They can be read/write or readonly and default getter and setter can be overridden using lambda expressions. In the last example only the setter is overridden which means getter is defaulting the generated one.

They "auto-generated" access methods which obviously could implemented using invokedynamic directly but even could be made into compiler magic.
Using the later approach would generate something like the next example:

public class PropertyClass {
  private int propertyOne;
  private int propertyTwo;
  private int propertyThree;
 
  public int propertyOne() { return propertyOne; }
  public void propertyOne(int propertyOne) { this.propertyOne = propertyOne; }
  public int getPropertyOne() -> PropertyClass::propertyOne
  public void setPropertyOne(int propertyOne) -> PropertyClass::propertyOne

  public int propertyTwo() { return propertyTwo; }
  public int getPropertyTwo() -> PropertyClass::propertyTwo;
 
  public int propertyThree() { return propertyThree + 1; }
  public int getPropertyThree() -> PropertyClass::propertyThree;
 
  public void propertyFour(int propertyFour) { this.propertyFour = propertyFour; }
  public void setPropertyFour(int propertyFour) -> PropertyClass::propertyFour
}

The generated compiler magic would use method references to map the legacy getters / setters to the property accessors.

So far to my considerations. I'm not quite sure if there is / was a similar approach someday but it would be nice to see some opinions and hopefully some more improvements or suggestions.

Cheers Chris

edit: My considerations go far beyond that simple cases.
For exmaple it would be nice too make this applicable to array-accessors to make a public-readonly array which could prevent System.arraycopy for arrays coming in or going out.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
noctarius
Offline
Joined: 2009-02-22
Points: 0

As I recognized in our german Java forum maybe not all people see the actual advantage of property accessors so I'll try to make a second example.
Let's guess we have a pretty ugly designed code which made all fields available as public members - we all know there's a lot of software out there looking like that ;-)

Now we introduce property accessors to indirectly hide the direct field access:

public class Foo {
  property int bla {
    (value) -> {
      if (value < 0 || value > 10) throw new IllegalArgumentException("value out of legal range");
      bla = value;
    }
  }
}

public class Bar {
  public static void main(String[] args) {
    Foo foo = new Foo();
    foo.bla = 6;  // this one is ok
    foo.bla = 15; // that one would throw the exception
  }
}

So all old code could benefit from the property accessors as being programmed against the Java Beans Setter conventions.

Hopefully that makes it a bit more clear what property accessor means to me :-)

noctarius
Offline
Joined: 2009-02-22
Points: 0

Today I had some time to write a deeper introduction into what I imagine Properties and PropertyAccessors should be and on what they look like.

Here's the blog article:
https://www.sourceprojects.org/default/2013/01/05/1357395720000.html