Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Infer hierarchy type parameters

Travis

Java/Kotlin utility to infer actual type for inherited type parameters

Infer generic types for inherited classes

If you have a class hierarchy where base class has type parameters, and you need to access actual type defined in inherited class you may pass it to constructor or defined an abstract method for that.

Without the library

abstract class Dao<E: Any> {
  abstract val entityType: Class<E>
  fun getTypeName() = entityType.name
}

class UserDao<User>: Dao<User>() {
  override val entityType = User::class.java
}

class GroupDao<Group>: Dao<Group>() {
  override val entityType = Group::class.java
}

With the library

But information about the type variable value for the class UserDao is actually available. You can get it using the method inferTypeParameter from the library.

abstract class Dao<E: Any> {
  open val entityType: Class<E> = inferTypeParameterClass(Dao::class.java, "E", javaClass)
  fun getTypeName() = entityType.name
}

class UserDao<User>: Dao<User>()

class GroupDao<Group>: Dao<Group>()

Check if a method overrides another method considering generics

open class Figure<A> {
    open fun compare(that: A, p: String, i: Int) = false
} 

class Rectangle<B> : Figure<B>() {
    override fun compare(that: B, o: String, i: Int) = true
}

fun check() {
    Rectangle<*>::compare.isOverriding(Figure<*>::compare) // true
}    

Gradle and Maven

Maven Central

Gradle

repositories {
    mavenCentral()
}
dependencies {
     compile 'com.github.mazine:infer-hierarchy-type-parameter:$version'
}

Maven

<dependency>
    <groupId>com.github.mazine</groupId>
    <artifactId>infer-hierarchy-type-parameter</artifactId>
    <version>$version</version>
</dependency>

About

Java/Kotlin utility to infer actual type for inherited type parameters

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages