Java Annotions Processing and C# like properties

I’m just thinking whether it would be possible to extend the java compiler with the apt tool or the new mustang api to accept code like the following:


class Foo {
   @Getter(value)
   int getValue() { .. }

   @Setter(value)
   void setValue(int value) { .. }
}

Foo foo = new Foo();
foo.value = 1; // translates to foo.setValue(1)

of course fields with the same name should have a higher property:


class Bar extends Foo {
  int value;
}

Bar bar = new Bar();
bar.value = 1; // should access the field
Foo foo = bar;
foo.value = 1; // still translates to foo.setValue(1);

what do you think, is this possible?

Why would you want to? It’s just another layer of confusion. When I say i = 1 I really expect i to be 1 afterwards but if there’s a method call intercepting it without my knowledge how am I to know i = 1 for sure? Crazy talk!

Cas :slight_smile:

Well, I was wondering, whether it is possible. It’s more about the capabilities of the annotation processor and the new language model exposed in mustang.

On the other hand it can be convenient to use. I noticed that playing with javax.script. With the Javascript reference implemtation, it is possible to access java bean properties as fields. can be nice.

I can understand your concerns hiding complexity, but one don’t has to use it, since the the set- and get-methods will still be present as usual. The best thing is, it would be 100% backward compatible. :slight_smile:

But again, the main reason for the post is that I want to know if it is possible… I’m not saying ‘hey, we need that feature …’