GokcenG
GokcenG

Reputation: 2811

AspectJ ITD Generic Method Parameter Breaks Compilation


I want to introduce interfaces to XSD generated concrete classes. First I need to show the working version:

public interface IBooking<T extends IChargeList> {
    T getChargesList();
    void setChargesList(T value);
}

public aspect IntertypeDeclarationAspect {
    declare parents:Booking implements IBooking<BookingChargeList>;
    declare parents:BookingChargeList implements IChargeList;
}

That version works but if I move generics to method declarations it doesn't:

public interface IBooking {
    <T extends IChargeList> T getChargesList();
    <T extends IChargeList> void setChargesList(T value);
}

public aspect IntertypeDeclarationAspect {
    declare parents:Booking implements IBooking;
    declare parents:BookingChargeList implements IChargeList;
}

The error message is: Booking.java:144 [error] The type Booking must implement the inherited abstract method IBooking.setChargesList(T)

The interesting part it doesn't give error for getChargesList() method.

What could be the reason for that?

The related part in Booking class is like this:

public BookingChargeList getChargesList() {
    return chargesList;
}
public void setChargesList(BookingChargeList value) {
    this.chargesList = value;
}

Upvotes: 1

Views: 81

Answers (1)

SimonB
SimonB

Reputation: 539

Dealing with the last part first, a return type isn't part of the method signature, and so as getChargesList() has no parameters, the aspect implements that correctly. Its return type will not be bound in the second example, but that doesn't matter.

As to why setChargesList(T) isn't implemented, that is because 'T' is undefined. When an interface method signature uses a method-level generic type definition, that definition must also exist in the implementation. So this is valid:

@Override
public <T extends IChargeList> void setChargesList(final T value) {
}

But this is not:

@Override
public void setChargesList(final T value) {
}

And its that second example that AspectJ is generating

I'm afraid my AspectJ isn't good enough to tell you how to solve your immediate issue, but I hope that helps you work out what's going on.

Upvotes: 2

Related Questions