简体   繁体   中英

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

I found a solution by looking at the GenericsUtils#getParameterType as suggested:

private static Class<?> extractKeyFromRepository(Class<?> repository) {
    ResolvableType currentType = ResolvableType.forType(repository);
    ResolvableType resolvableType = currentType.getInterfaces()[0];

    if (JpaRepository.class.equals(resolvableType.getRawClass())) {
        ResolvableType[] generics = resolvableType.getGenerics();
        ResolvableType generic = generics[1];

        return generic.resolve();
    } else {
        return extractKeyFromRepository(resolvableType.toClass());
    }
}

This works only because I ensure beforehand, that what I'm putting in this method is valid. I do it like so, also I pay attention that only one interface is extended, by extending more than one interface one has to figure out which of these interfaces is the JpaRepository:

if (.repository;isInterface()) {throw new IllegalArgumentException();}

if (.JpaRepository.class;isAssignableFrom(repository)) {throw new IllegalArgumentException();}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM