Erwin Müller
Erwin Müller

Reputation: 59

Java change direct field access at compile time via auto-generating Java bytecode to getter and setters

Is there a project similar to Lombok that can change direct access to fields to use getters and setters via auto-generating Java bytecode? Lombok already is doing this for all kinds of conveniences like @Data, it then generates all sorts of code for me.

I like to write code like:

val pos = new Pos(10, 11, 12);
val x = pos.x;
val y = pos.y;
val z = pos.z;

Then, at compile time, my code will be changed to use the getter methods, and fed to the Java compiler.

val pos = new Pos(10, 11, 12);
val x = pos.getX();
val y = pos.getY();
val z = pos.getZ();

For example, later this will use the overwritten getters:

class MyPos extends Pos {
    @overwrite
    public int getX() {
        return super.getX() + 1;
    }
}

public void calc(Pos pos) {
    val x = pos.x; // this should be changed to pos.getX();
    val y = pos.y; // this should be changed to pos.getY();
    val z = pos.z; // this should be changed to pos.getZ();
}

calc(new MyPos(1, 2, 3));

The reason behind it, that if I overwrite the getters or setters, I want to take advantage of the overwritten implementation. C# already have something similar with properties. What I do not want are C# properties for Java.

Upvotes: -2

Views: 23

Answers (0)

Related Questions