Generic Reflection

Java reflection with generic resolver

License

License

GroupId

GroupId

com.github.zerkseez
ArtifactId

ArtifactId

generic-reflection
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Generic Reflection
Java reflection with generic resolver
Project URL

Project URL

https://github.com/zerkseez/generic-reflection
Source Code Management

Source Code Management

https://github.com/zerkseez/generic-reflection/tree/master

Download generic-reflection

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.zerkseez/generic-reflection/ -->
<dependency>
    <groupId>com.github.zerkseez</groupId>
    <artifactId>generic-reflection</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.zerkseez/generic-reflection/
implementation 'com.github.zerkseez:generic-reflection:1.1.0'
// https://jarcasting.com/artifacts/com.github.zerkseez/generic-reflection/
implementation ("com.github.zerkseez:generic-reflection:1.1.0")
'com.github.zerkseez:generic-reflection:jar:1.1.0'
<dependency org="com.github.zerkseez" name="generic-reflection" rev="1.1.0">
  <artifact name="generic-reflection" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.zerkseez', module='generic-reflection', version='1.1.0')
)
libraryDependencies += "com.github.zerkseez" % "generic-reflection" % "1.1.0"
[com.github.zerkseez/generic-reflection "1.1.0"]

Dependencies

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

generic-reflection

Java reflection with generic resolver

Synopsis

This library makes it easy to obtain type information including generic type parameter bindings easily at runtime.

For example:

class BaseClass<A, B> {
    public Map<A, List<B>> doSomethingCrazy(A[] arg1, Collection<? extends B>[] arg2) { ... }
}

class SubClass extends BaseClass<String, Integer> {
}

To find out the return type and parameter types of SubClass.doSomethingCrazy():

TypeInfo<?> typeInfo = Reflection.getTypeInfo(SubClass.class);
MethodInfo methodInfo = typeInfo.getPublicMethods().get(0);

TypeInfo<?> returnType = methodInfo.getReturnType();
System.out.println(returnType.toString()); // Prints java.util.Map<java.lang.String, java.util.List<java.lang.Integer>>

List<ParameterInfo> parameters = methodInfo.getParameters();
System.out.println(parameters.get(0).getType().toString()); // Prints java.lang.String[]
System.out.println(parameters.get(1).getType().toString()); // Prints java.util.Collection<? extends java.lang.Integer>[]

Setup

Maven

<dependency>
    <groupId>com.github.zerkseez</groupId>
    <artifactId>generic-reflection</artifactId>
    <version>1.0.0</version>
</dependency>

Basic Use Cases

To obtain information for any type:

TypeInfo<?> typeInfo1 = Reflection.getTypeInfo(MyClass.class);
TypeInfo<?> typeInfo2 = Reflection.getTypeInfo("com.mycompany.MyClass");

Get all public methods, including inherited ones:

List<MethodInfo> publicMethods = typeInfo.getPublicMethods();

Get return type and the parameter types for a method:

MethodInfo method = ...
TypeInfo<?> returnType = method.getReturnType();
List<ParameterInfo> parameters = method.getParameters();
for (ParameterInfo parameter : parameters) {
    TypeInfo<?> parameterType = parameter.getType();
    ...
}

License

Apache License, Version 2.0

Versions

Version
1.1.0
1.0.0