0

I would like to apply an annotation to certain method calls so that I can tell aspectj to modify the execution for those calls.

public interface Door {
  void open() throws DoorException;
  void close() throws DoorException;
}

public class Ferrari {
  @Inject @Lightweight Door door;

  public void drive() {
    open();

    close();

    open();
  }
}

In this case here, I want all open/close operations to effectively be wrapped the same way, but what if I want one to behave differently?

I would like to do something like:

public class Ferrari {
      @Inject Door door;

      public void drive() {
        door.open();

        door.close();

        @Slowly door.open();
      }
    }

I realize that I cannot put an annotation there, but at the same token, I don't want to clobber the open signature to pass in optional metadata.

What is the best way to do this?

I could do this:

public class Ferrari {
          @Inject Door door;

          public void drive() {
            door.open();

            door.close();

            ((@Slowly Door)door).open();
          }
        }
Walter
  • 1,290
  • 2
  • 21
  • 46

2 Answers2

0

I don't think you can do that and if you want to do that than maybe you should rethink how you organized your code.

I'm not sure why you would want to do that, but one guess would be that, maybe for one specific Ferrari you want to open the door another way. Then you could just extend the Ferrari class for a specific model and use a different implementation for the Door interface. You can do that by using Qualifiers in spring or @Named in Java ee, I think.

There is a similar answer in the link below, if it helps. Injecting multiple implementations to a single service in Spring

0

I am planning to use 2 aspects, 1 to mark certain methods as "special" and then place a flag in a registry, and the 2nd will be to wrap all calls to a given interface and look in the registry to see if the flag is set.

Walter
  • 1,290
  • 2
  • 21
  • 46