Java Type Resolver
This utility library helps to determine real types for generics, arrays, wildcards and other complex types on runtime. Receiving type for generics can be a problem because of Java doesn't store information about generic type parameters on runtime so this library helps to solve this problem.
Table of contents
Getting started
To add library to your project perform next steps:
Maven
Add the following dependency to your pom.xml:
<dependency>
<groupId>com.github.vladislavsevruk</groupId>
<artifactId>type-resolver</artifactId>
<version>1.0.3</version>
</dependency>
Gradle
Add the following dependency to your build.gradle:
implementation 'com.github.vladislavsevruk:type-resolver:1.0.3'
Main entities
TypeMeta
TypeMeta represents metadata about type parameter(s) for generics and arrays.
Examples of resulted TypeMeta structure for different cases:
- List<? extends Number>:
{ type: List, wildcard: false, genericTypes: [
{ type: Number, wildcard: true, genericTypes:[] }
] }
- long[]:
{ type: long[], wildcard: false, genericTypes: [
{ type: long, wildcard: false, genericTypes:[] }
] }
- Map<String, Integer>[]:
{ type: Map[], wildcard: false, genericTypes: [
{ type: Map, wildcard: false, genericTypes: [
{ type: String, wildcard: false, genericTypes:[] },
{ type: Integer, wildcard: false, genericTypes:[] }
] }
] }
TypeProvider
TypeProvider provides easy to use method for generating TypeMeta for generics.
- With TypeProvider:
TypeMeta<?> typeMeta = new TypeProvider<Map<String, List<Integer>>>() {}.getTypeMeta();
- Without TypeProvider:
TypeMeta<?> innerTypeMeta1 = new TypeMeta<>(String.class);
TypeMeta<?> deepInnerTypeMeta = new TypeMeta<>(Integer.class);
TypeMeta<?>[] deepInnerTypeMetas = new TypeMeta<?>[] { deepInnerTypeMeta };
TypeMeta<?> innerTypeMeta2 = new TypeMeta<>(List.class, deepInnerTypeMetas);
TypeMeta<?>[] innerTypeMetas = new TypeMeta<?>[] { innerTypeMeta1, innerTypeMeta2 };
TypeMeta<Map> typeMeta = new TypeMeta<>(Map.class, innerTypeMetas);
FieldTypeResolver
FieldTypeResolver can be used to determine TypeMeta for field of provided class. Library has default implementation of this interface.
ExecutableTypeResolver
ExecutableTypeResolver can be used to determine TypeMeta for return and argument types of provided method. Library has default implementation of this interface.
Usage
Determine field type
Let's assume that we have following generic class:
public class Cake<T> {
private List<String> ingredients;
private T filling;
}
and we need to determine its fields type. We can use FieldTypeMetaResolver for this purpose:
FieldTypeResolver<TypeMeta<?>> fieldTypeResolver = new FieldTypeMetaResolver();
// get class field to determine its type
Field fieldToResolve = Cake.class.getDeclaredField("ingredients");
TypeMeta<?> fieldTypeMeta = fieldTypeResolver.resolveField(Cake.class, fieldToResolve);
Resulted TypeMeta will have following structure:
{ type: List, wildcard: false, genericTypes: [
{ type: String, wildcard: false, genericTypes:[] }
] }
If we need to determine type of field that use generic parameter(s) we may use subclass of TypeProvider:
FieldTypeResolver<TypeMeta<?>> fieldTypeResolver = new FieldTypeMetaResolver();
// get class field to determine its type
Field fieldToResolve = Cake.class.getDeclaredField("filling");
// create type provider with generic class where field declared
// we use String as Cake's type parameter for this example
TypeProvider<?> typeProvider = new TypeProvider<Cake<String>>() {};
TypeMeta<?> fieldTypeMeta = fieldTypeResolver.resolveField(typeProvider, fieldToResolve);
And as a result TypeMeta will have following structure:
{ type: String, wildcard: false, genericTypes:[] }
Determine method argument and return types
Let's assume that our generic class have following methods:
public class Cake<T> {
...
public List<String> getIngredients() {
...
}
public void setFilling(T filling) {
...
}
}
To determine their argument or return types we can use ExecutableTypeMetaResolver:
ExecutableTypeResolver<TypeMeta<?>> executableTypeResolver = new ExecutableTypeMetaResolver();
// get method to determine its return and argument types
Method methodToResolve = Cake.class.getDeclaredMethod("getIngredients");
TypeMeta<?> methodReturnTypeMeta = executableTypeResolver.getReturnType(Cake.class, methodToResolve);
List<TypeMeta<?>> methodArgumentsTypeMetaList = executableTypeResolver
.getParameterTypes(Cake.class, methodToResolve);
Resulted TypeMeta items will have following structures:
- Return type (methodReturnTypeMeta):
{ type: List, wildcard: false, genericTypes: [
{ type: String, wildcard: false, genericTypes:[] }
] }
- Argument types (methodArgumentsTypeMetaList):
[]
If we need to determine types of method that uses generic parameters we may use subclass of TypeProvider:
ExecutableTypeResolver<TypeMeta<?>> executableTypeResolver = new ExecutableTypeMetaResolver();
// get method to determine its return and argument types
Method methodToResolve = Cake.class.getDeclaredMethod("setFilling", Object.class);
// create type provider with generic class where field declared
// we use String as Cake's type parameter for this example
TypeProvider<?> typeProvider = new TypeProvider<Cake<String>>() {};
TypeMeta<?> methodReturnTypeMeta = executableTypeResolver.getReturnType(typeProvider, methodToResolve);
List<TypeMeta<?>> methodArgumentsTypeMetaList = executableTypeResolver
.getParameterTypes(typeProvider, methodToResolve);
And as a result TypeMeta items will have following structure:
- Return type (methodReturnTypeMeta):
{ type: void, wildcard: false, genericTypes: [] }
- Argument types (methodArgumentsTypeMetaList):
[{ type: String, wildcard: false, genericTypes:[] }]
License
This project is licensed under the MIT License, you can read the full text here.