Skip to content

This utility library helps to determine string representation for generics, arrays, wildcards and other complex types on runtime.

License

Notifications You must be signed in to change notification settings

VladislavSevruk/TypeStringRepresentationResolver

Repository files navigation

Build Status Quality Gate Status Code Coverage Maven Central

Java Type String Representation Resolver

This utility library helps to determine string representation 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-string-representation-resolver</artifactId>
      <version>1.0.0</version>
</dependency>

Gradle

Add the following dependency to your build.gradle:

implementation 'com.github.vladislavsevruk:type-string-representation-resolver:1.0.0'

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 string representation for field of provided class. Library has default implementation of this interface.

ExecutableTypeResolver

ExecutableTypeResolver can be used to determine string representation 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 string representation its fields type. We can use FieldStringRepresentationResolver for this purpose:

FieldTypeResolver<String> fieldTypeResolver = new FieldStringRepresentationResolver();
// get class field to determine its type
Field fieldToResolve = Cake.class.getDeclaredField("ingredients");
String fieldRepresentation = fieldTypeResolver.resolveField(Cake.class, fieldToResolve);

Resulted String will be: java.util.List<java.lang.String>

If we need to determine type of field that use generic parameter(s) we may use subclass of TypeProvider:

FieldTypeResolver<String> fieldTypeResolver = new FieldStringRepresentationResolver();
// 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>>() {};
String fieldRepresentation = fieldTypeResolver.resolveField(typeProvider, fieldToResolve);

And as a result String will be: java.lang.String

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 ExecutableStringRepresentationResolver:

ExecutableTypeResolver<String> executableTypeResolver = new ExecutableStringRepresentationResolver();
// get method to determine its return and argument types
Method methodToResolve = Cake.class.getDeclaredMethod("getIngredients");
String methodReturnTypeRepresentation = executableTypeResolver.getReturnType(Cake.class, methodToResolve);
List<String> methodArgumentsRepresentationList = executableTypeResolver
        .getParameterTypes(Cake.class, methodToResolve);

Resulted String-s will be:

  • Return type (methodReturnTypeRepresentation): java.util.List<java.lang.String>
  • Argument types (methodArgumentsRepresentationList): []

If we need to determine types of method that uses generic parameters we may use subclass of TypeProvider:

ExecutableTypeResolver<String> executableTypeResolver = new ExecutableTypeResolverImpl();
// 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>>() {};
String methodReturnTypeRepresentation = executableTypeResolver.getReturnType(typeProvider, methodToResolve);
List<String> methodArgumentsRepresentationList = executableTypeResolver
        .getParameterTypes(typeProvider, methodToResolve);

And as a result String-s will be:

  • Return type (methodReturnTypeRepresentation): void
  • Argument types (methodArgumentsRepresentationList): java.lang.String

Switching to short names

By default resolvers generate use full names (with package name - java.util.List) for resolved classes but you can configure them to use short names (only class name itself - List) by replacing one of library modules:

StringRepresentationResolvingModuleFactory
        .replaceTypeResolverStorage(ShortNameRepresentationResolverStorage::new);

License

This project is licensed under the MIT License, you can read the full text here.

About

This utility library helps to determine string representation for generics, arrays, wildcards and other complex types on runtime.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages