vendredi 24 avril 2020

Java extract generic type parameters with reflection from interface

I'm writing a custom Java annotation for processing CrudRepositories with Reflection in Java Spring. With the org.reflections.reflections library. I'm getting all interfaces annotated with my annotation as a class file like so:

Reflections reflections = new Reflections("basePackage");
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(MyAnnotation.class);

Only interfaces, which at some point extend JpaRepository are annotated with my @MyAnnotation at the class level.


My repository structure is as follows: There are two cases,

first case:
public interface SomeRepo extends JpaRepository<SomeEntity, Long> {...}

the second case is composed out of a inheritance hierarchy:
public interface SuperClassRepo <T extends SomeRandomEntity> extends JpaRepository<T, String> {...}
public interface SubClassRepo extends SuperClassRepo<SubEntityOfSomeRandomEntity> {...}

My goal is now to extract the generic type parameters of the underlying JpaRepository. I achieved to do that if the annotated class is a Java class, not an interface. How can I achieve the same for an interface? I guess I'm also having trouble because of the inheritance. I guess I have to get the "super class" until I reach the JpaRepository and then somewhat extract the generic type arguments.

Help is very much appreciated, thanks in advance





Aucun commentaire:

Enregistrer un commentaire