Reputation: 20926
private Service generateActionResponse(@Nonnull Class<? extends RetryActionResultDto> response) {
if (response.isSuccess()) {
...
} else if (response.getRetryDecision() {
....
}
}
public interface RetryActionResultDto extends DTO {
public RetryDecision getRetryDecision();
public boolean isSuccess();
}
but I get exception
The method isSuccess() is undefined for the type Class
what i can do?
Upvotes: 0
Views: 84
Reputation: 4671
What you are trying to do here is wrong. Class<? extends RetryActionResultDto>
is a Class
and not an object of a class implementing RetryActionResultDto
.
if you want objects whose class has implemented RetryActionResultDto
to be passed as arguments then you can use
private Service generateActionResponse(@Nonnull RetryActionResultDto response) {
as the passed argument has implemented the interface it will have all the methods declared in the interface and the actual implementation of the methods will be called in the runtime with respect to the passed object.
Upvotes: 0
Reputation: 10632
You can re-write the method definition as this:
private <T extends RetryActionResultDto> String generateActionResponse(
T response) {
..
}
which says that the method parameter accepts instances of RetryActionResultDto
or its subclasses.
Upvotes: 1
Reputation: 4940
private <T> Service generateActionResponse(@Nonnull T extends RetryActionResultDto response) {
if (response.isSuccess()) {
...
} else if (response.getRetryDecision() {
....
}
}
But, since RetryActionResultDto
is an interfce, the method only accepts arguments which are subtypes of RetryActionResultDto
, even without generics.
Upvotes: 1
Reputation: 817
Your argument is a class .. not an instance of that class. Hence the error.
Try changing it to:
private Service generateActionResponse(@Nonnull RetryActionResultDto response) {
if (response.isSuccess()) {
...
} else if (response.getRetryDecision() {
....
}
}
An instance of a subclass would also pass through it.
Upvotes: 3