0

I have the following problem, I am wondering whether anyone knows a solution:

I would like to dynamically generate specially named getters for every private field in every subclass of a certain class using aspectj or some other similar bytecode manipulation tool.

I would like the names of the getters to be based on the corresponding field names, but I can settle for having one method taking a field name string and returning a value, as long as there is no reflection involved.

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
user811521
  • 11
  • 1

2 Answers2

0

As in Matt's answer, I suggest you Lombok. Using the @Getter, @Setter annotations on the class level, corresponding getters/setters based on names of all the non-static fields will be generated at compilation.

Much better than annotating classes by hand is to use static crosscutting of AspectJ (page in the official documentation) to add those annotations globally.

Example for declaring @Getter and @Setter on each persistent Entity :

import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;

public aspect EntityAspect {
    declare @type: @Entity * : @Getter;
    declare @type: @Entity * : @Setter;
}

It requires you either to have the source code, or to use compilation by aspectj on existing jar (instrumentation of an existing library).

JLM
  • 559
  • 6
  • 11
  • Please be aware of some issues when combining [Lombok and AspectJ](http://stackoverflow.com/a/26157868/1082681). I do not know if they are resolved by now. – kriegaex Jun 30 '15 at 21:15
0

Have you looked into Lombok? If you have access to the source and are just trying to save typing, than its @Data annotation might be what you looking for.

Matt
  • 2,139
  • 1
  • 16
  • 20