Coder Social home page Coder Social logo

dpnolte / infer-hierarchy-type-parameter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mazine/infer-hierarchy-type-parameter

0.0 2.0 0.0 67 KB

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

License: Apache License 2.0

Kotlin 100.00%

infer-hierarchy-type-parameter's Introduction

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>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.