playframework / play-enhancer

14 stars 9 forks source link

Automatically call self-written accessors #4

Open aschiffmann opened 9 years ago

aschiffmann commented 9 years ago

Currently accessors will only be called automatically, if they are generated by the PropertiesEnhancer. Accessors which are self-written have to be called explicitly. Wouldn't it be nice to call all accessors automatically? I think, this is what developers would intuitively excpect and it makes coding easier since you don't have to worry about explicitly calling getters and setters in case they are self written.

jroper commented 9 years ago

We could make it an option to turn this on.

UnknownNPC commented 6 years ago

I hope this may help somebody.

PropertiesEnhancer based on two key methods generateAccessors (generates get/set methods, addsGeneratedGetAccessor, GeneratedSetAccessor annotations) and rewriteAccess (replace direct access with get and direct value assignment with set).

rewriteAccess searches fields with annotations GeneratedGetAccessor.class and GeneratedSetAccessor.class generated by the generateAccessors.

We can trigger rewriteAccess for OUR OWN get/set methods if manually annotate field with GeneratedGetAccessor.class or GeneratedSetAccessor.class (or both). Example: Our own class

public class A {

    public A() {
    }

    @PropertiesEnhancer.GeneratedSetAccessor
    public String name;

    public String getName() {
        return "my name is: " + name;
    }

    public void setName(String name) {
        this.name = name + "!!!";
    }

}

Class usage:

    A a = new A();
    a.name = "VZ";
    String get = a.name;

Decompilation result:

    A a = new A();
    var1 = "VZ";
    a.setName(var1);
        String get = a.name;

If we want replace a.name with getter, GeneratedGetAccessor should be added as well.