generics-resolver

Java generics runtime resolver

License

License

GroupId

GroupId

ru.vyarus
ArtifactId

ArtifactId

generics-resolver
Last Version

Last Version

3.0.3
Release Date

Release Date

Type

Type

jar
Description

Description

generics-resolver
Java generics runtime resolver
Project URL

Project URL

https://github.com/xvik/generics-resolver
Source Code Management

Source Code Management

https://github.com/xvik/generics-resolver.git

Download generics-resolver

How to add to project

<!-- https://jarcasting.com/artifacts/ru.vyarus/generics-resolver/ -->
<dependency>
    <groupId>ru.vyarus</groupId>
    <artifactId>generics-resolver</artifactId>
    <version>3.0.3</version>
</dependency>
// https://jarcasting.com/artifacts/ru.vyarus/generics-resolver/
implementation 'ru.vyarus:generics-resolver:3.0.3'
// https://jarcasting.com/artifacts/ru.vyarus/generics-resolver/
implementation ("ru.vyarus:generics-resolver:3.0.3")
'ru.vyarus:generics-resolver:jar:3.0.3'
<dependency org="ru.vyarus" name="generics-resolver" rev="3.0.3">
  <artifact name="generics-resolver" type="jar" />
</dependency>
@Grapes(
@Grab(group='ru.vyarus', module='generics-resolver', version='3.0.3')
)
libraryDependencies += "ru.vyarus" % "generics-resolver" % "3.0.3"
[ru.vyarus/generics-resolver "3.0.3"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Java generics runtime resolver

License Build Status 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:

  • Resolves generics for hierarchies of any depth (all subclasses and interfaces on any level)
  • Supports
    • composite generics (e.g. Smth<T, K extends List<T>>)
    • method generics (<T> T getSmth())
    • constructor generics (<T> Some(T arg))
    • outer class generics (Outer<T>.Inner)
  • Context api completely prevents incorrect generics resolution (by doing automatic context switching)
  • Sub contexts: build context from Type in current context to properly solve root generics
  • Generics backtracking: track higher type generics from some known middle type
  • To string types converter (useful for logging/reporting)
  • General types comparison api (assignability, compatibility, specificity checks)

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

JCenter Maven Central

Maven:

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

Gradle:

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

Snapshots

Snapshots may be used through JitPack
  • Go to JitPack project page
  • Select Commits section and click Get it on commit you want to use (top one - the most recent)
  • Follow displayed instruction:
    • Add jitpack repository: maven { url 'https://jitpack.io' }
    • Use commit hash as version: ru.vyarus:generics-resolver:56537f7d23 (or use master-SNAPSHOT)

Usage

Read documentation


java lib generator

Versions

Version
3.0.3
3.0.2
3.0.1
3.0.0
2.0.1
2.0.0
1.2.1
1.2.0
1.1.0
1.0.0