AspectJ. Creating inter methods in several classes

If I put:

public CountryState CountryState.find(long id) {
        return (CountryState) findById(CountryState.class, id);
}

I create a find method in the CountryState class.

Is there a way to create a method in multiple classes? Do I need to repeat the code for each class I want to create?

I know that with an aspect I can inherit a class from another, but by doing this I can create one superclass because java does not accept multiple inheritance.

+3
source share
2 answers

This "template" is how you do it in AspectJ.

Declare Interface:

interface Holder {}

Make your intertype declarations in an interface:

public int Holder.getMeAnInt() {
  return 42;
}

, " ". , getMeAnInt(), Holder, getMeAnInt(), .

, , , :

declare parents: @Anno * implements Holder;

, , @Anno, Holder getMeAnInt().

+5

. OOP/OOD. ( , ):

  • . .

  • , find() ( Dependency Injection, ).

, , , , , :

public <T> find(long id, T targetClassObject) {
  Class<? extends T> class = targetClassObject.getClass();
  // do something i.e. call target method via reflection
}
0

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


All Articles