Home

Awesome

Java generics runtime resolver

License CI Appveyor build status codecov

DOCUMENTATION https://xvik.github.io/generics-resolver

About

class Base<T, K> {
  T doSomething(K arg);
}

class Root extends Base<Integer, Long> {...}

Library was created to support reflection analysis (introspection) with all available types information.

// compute generics for classes in Root hierarchy
GenericsContext context = GenericsResolver.resolve(Root.class)
        // switch current class to Base (to work in context of it)
        .type(Base.class);

context.generics() == [Integer.class, Long.class]

MethodGenericsContext methodContext = context
    .method(Base.class.getMethod("doSomething", Object.class))
// method return class (in context of Root)
methodContext.resolveReturnClass() == Integer.class
// method parameters (in context of Root)
methodContext.resolveParameters() == [Long.class]

Features:

NOTE: Java 8 lambdas are not supported because there is no official way to analyze lambdas due to implementation. It is possible to use some hacks to resolve lambda geneics in some cases, but it's quite fragile (may break on future java releases or not work on other java implementations).

Library targets actual classes analysis and, personally, I never really need to analyze lambdas.

Library was originally written for guice-persist-orient to support repositories analysis and later used in dropwizard-guicey for extensions analysis.

Compatible with Java 6 and above.

Alternatives

For simple cases (e.g. to resolve class/interface generic value), look, maybe you already have required tool in the classpath (and it will be enough):

Setup

Maven Central

Maven:

<dependency>
  <groupId>ru.vyarus</groupId>
  <artifactId>generics-resolver</artifactId>
  <version>3.0.3</version>
</dependency>

Gradle:

implementation 'ru.vyarus:generics-resolver:3.0.3'

Snapshots

<details> <summary>Snapshots may be used through JitPack</summary> </details>

Usage

Read documentation


java lib generator