Creating getters / setters in Java (again)

I am considering writing approaches


class MyClass {
  @Get
  @Set
  protected int aValue;
}

and then automatically generate the get_aValue () and set_AValue () methods for the class.

I found these features:

1a) At compile time. Use the annotation handler to separately process MyClass.java, then write a new MyClass.java and finally use this last (replacing the original) with the rest of .java

1b) At compile time. Use the annotation handler to create the MyClassGenerated.java file with a new class (with get / set methods) that is this subclass of the original MyClass.

2) At runtime. Use java.lang.instrument and an external tool (e.g. BCEL) to weave the new code into MyClass.class.

, : , , , (, lombok BCEL)

a) - ?

b) ?

, 1a),

1b) ( MyClassGeneradted MyClass, , , )

2) ( , ).

+3
6

-, , , :

1c) . , APT, .class ( ), get set -.

, , , - , .


, , . , Java , - javac, . , , , , , . , ..

- , , IDE. , , . Java - .: -)

+4

, getter setter. :

generate getters and setters screenshot

+5
  • a) JVM ( Java),
  • b) Scala

, , , , .

+3

, 3 - IDE eclipse getter setter. (netbeans IntelliJ )

IDE getter/setter ( ) , .

+3

Lombok - , , .

+3

, , , , , , .

, -

Bean { public int getX(); public void setX ( int); }

(, , ). , , , get/set style beans .

(2), , -: , . . , .

, , , (asm, cglib, janino, javassist, ).

lib - , , , , , .

... , IDE , , .: -)

+1

Source: https://habr.com/ru/post/1754391/


All Articles