mercredi 12 août 2020

How to read the implementation class in default interface method?

I want to create a default interface method that can respond with the annotated value of the implementation class.

Example: in the following, I want all Child implementations to read their own @Generated value field:

public interface Parent {
    default String[] find() {
        return AnnotationUtils.findAnnotation(this.getClass(), Generated.class).value();
    }
}

@Generated(value = {"test"})
public class Child implements Parent {

}

Usage:

System.out.println(new Child().find());

Result: NullPointerException, because the call tries to find the annotation on the Parent interface class, not the implementation class.

It it still possible somehow?





Aucun commentaire:

Enregistrer un commentaire