infer-hierarchy-type-parameter

null

License

License

Categories

Categories

Infer Application Testing & Monitoring Code Analysis
GroupId

GroupId

com.github.mazine
ArtifactId

ArtifactId

infer-hierarchy-type-parameter
Last Version

Last Version

1.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

infer-hierarchy-type-parameter
null
Project URL

Project URL

https://github.com/mazine/infer-hierarchy-type-parameter
Source Code Management

Source Code Management

https://github.com/mazine/infer-hierarchy-type-parameter

Download infer-hierarchy-type-parameter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.mazine/infer-hierarchy-type-parameter/ -->
<dependency>
    <groupId>com.github.mazine</groupId>
    <artifactId>infer-hierarchy-type-parameter</artifactId>
    <version>1.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.mazine/infer-hierarchy-type-parameter/
implementation 'com.github.mazine:infer-hierarchy-type-parameter:1.2.0'
// https://jarcasting.com/artifacts/com.github.mazine/infer-hierarchy-type-parameter/
implementation ("com.github.mazine:infer-hierarchy-type-parameter:1.2.0")
'com.github.mazine:infer-hierarchy-type-parameter:jar:1.2.0'
<dependency org="com.github.mazine" name="infer-hierarchy-type-parameter" rev="1.2.0">
  <artifact name="infer-hierarchy-type-parameter" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.mazine', module='infer-hierarchy-type-parameter', version='1.2.0')
)
libraryDependencies += "com.github.mazine" % "infer-hierarchy-type-parameter" % "1.2.0"
[com.github.mazine/infer-hierarchy-type-parameter "1.2.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib jar 1.3.10
org.jetbrains.kotlin : kotlin-reflect jar 1.3.10

test (3)

Group / Artifact Type Version
com.google.truth : truth jar 0.31
junit : junit jar 4.12
org.jetbrains.kotlin : kotlin-test-junit jar 1.3.10

Project Modules

There are no modules declared in this project.

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>

Versions

Version
1.2.0
1.1.0
1.0.6
1.0.5
1.0.4
1.0.3