How to invork method like this: <U> Subquery<U> subquery(Class<U> type);


#1
import org.springframework.data.jpa.domain.Specification

class SubBasicSpec<Entity, SubEntity> : Specification<Entity> {
    override fun toPredicate(root: Root<Entity>?, query: CriteriaQuery<*>?, cb: CriteriaBuilder?): Predicate? {
        val subQuery = query?.subquery<Entity>(Entity::class.java) // not ok
        val subRoot = subQuery.from(SubEntity::class.java)  // not ok, the same
 
        throw UnsupportedOperationException()
    }

}

and I have difficulty to define a reified fun:

inline fun <reified Entity> CriteriaQuery<Entity>.subquery1() = this.subquery<Entity>(Entity::class.java)

#2

In case if you want to get java class of a reified type parameter of inline function you need to specify the upper bound of that type parameter to be Any to be able to invoke java extension property on a kotlin class. That’s how that property is declared:

val <T : Any> KClass<T>.java: Class<T>

So try the following:

inline fun <reified Entity : Any> CriteriaQuery<Entity>.subquery1()