Java Properties Syntax - Intro

The experimental feature, properties syntax added to Java on language basis - no getters nor setters.

The Java(tm) as a platform is quite rich. It has a lot of features mainly implemented on platform basis, often emulated by extensions or packages described by JSRs. The same is with properties, ones can be found as part of JavaBeans(tm) specification, but not as a language syntax. How many one-line getter or setters is created each day? How many repeatable code firing property change is created too? A lot. Wouldn't be easier to use

@Id
public property Integer id;

Once I thought same and having a bit of time I took a javac source code to add a support for this syntax to compiler. The idea was simple, to have properties, to automatically create accessors (getters and setters), to have a possibility to automatically generate change events, to declare properties in interfaces and so on, everything what can be found in C#, Action Script and now in Swift.

The result can be seen below...
The red underline is caused by Netbeans not understanding new syntax, but looking into output window it can be seen that code is compilable and executable.

Assumptions (not a languages theory)

Property is referenced in code like variable (a public variable), but in opposition to ones all reads, assigns use executable code defined in get {} or set {} block, respectively, or default getter or setter if those aren't provided. The good example is on above picture, where:
  • data.random 'returns' Math.random() + offset,
  • data.offset has default accessors.

What is supported

  • read-write, read only and write only properties,
  • overriding properties,
  • final accessors,
  • different access modifiers for accessor - i.e. public getter, private or protected setter,
  • properties with generic types,
  • static properties,
  • interface level properties and accessors,
  • abstract properties and accessors,
  • 'extending' property with 2nd accessor in subclass or interface implementaion, ie. interface can only declare public getter, while implementation may provide setter.

Source code

The source code can be found here:
https://bitbucket.org/radoslaw_smogura/java_properties-jdk9-langtools
(The projects is slowly uploaded into above repository, as this uploading allows thinking about code once more)

What's next

As it is just a compiler change, there is no support from JDK - no JavaBeans, no PROPERTY retention for annotations, no reflection in Class, and so on - this should be added to JDK.

Fixing or tuning some syntax assumptions.

Of course, any help, questions or remarks are welcome!






SHARE
    Blogger Comment
    Facebook Comment

1 komentarze:

  1. Exciting! Maybe your proof of concept can help to convince Oracle to add this.

    ReplyDelete