Reflection Utils

src Fix null safety bugs, typo in public API, and add missing tests 6 months ago
.gitignore Initial commit 5 years ago
README.md Update README with full developer reference documentation 6 months ago
pom.xml updated nexus url 6 months ago
README.md

reflection-utils

A lightweight Java utility library that provides static helper methods for working with the Java Reflection API. It simplifies common type-inspection tasks — checking whether a type is a primitive, a collection, a boxed wrapper, an enum, and more — so you do not have to write repetitive boilerplate when building frameworks, serializers, or other reflection-heavy code.

Dependency

<dependency>
    <groupId>io.ljunggren</groupId>
    <artifactId>reflection-utils</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

Usage

All methods are static. Import the class and call directly:

import io.ljunggren.reflectionUtils.ReflectionUtils;

Method Reference

Get Owner Type

Returns the raw (erased) type of a parameterized type, or null if the type is not parameterized.

Type owner = ReflectionUtils.getOwnerType(type);

Is Parameterized

Returns true if the given type is a ParameterizedType (e.g., List<String>).

boolean result = ReflectionUtils.isParameterized(type);

Get Parameterized Types

Returns all type arguments of a parameterized type as an array. Returns an empty array if the type is not parameterized.

Type[] types = ReflectionUtils.getParameterizedTypes(type);

Get Parameterized Type

Returns the single type argument of a parameterized type, or null if there is not exactly one type argument.

Type type = ReflectionUtils.getParameterizedType(type);

Get Array Component Type

Returns the component type of an array type, or null if the type is not an array.

Type componentType = ReflectionUtils.getArrayComponentType(type);

Get Object Fields

Returns all fields declared on the class and its superclasses, including private fields.

List<Field> fields = ReflectionUtils.getObjectFields(clazz);

Is Primitive

Returns true if the type represents a Java primitive (e.g., int, boolean). Parameterized types always return false.

boolean result = ReflectionUtils.isPrimitive(type);
boolean result = ReflectionUtils.isPrimitive(clazz);

Is Number

Returns true if the value or type is assignable from Number.

boolean result = ReflectionUtils.isNumber(t);       // instance check
boolean result = ReflectionUtils.isNumber(type);    // Type overload
boolean result = ReflectionUtils.isNumber(clazz);   // Class<?> overload

Is Integer

Returns true if the value or type is Integer.

boolean result = ReflectionUtils.isInteger(t);
boolean result = ReflectionUtils.isInteger(type);
boolean result = ReflectionUtils.isInteger(clazz);

Is Double

Returns true if the value or type is Double.

boolean result = ReflectionUtils.isDouble(t);
boolean result = ReflectionUtils.isDouble(type);
boolean result = ReflectionUtils.isDouble(clazz);

Is Short

Returns true if the value or type is Short.

boolean result = ReflectionUtils.isShort(t);
boolean result = ReflectionUtils.isShort(type);
boolean result = ReflectionUtils.isShort(clazz);

Is Long

Returns true if the value or type is Long.

boolean result = ReflectionUtils.isLong(t);
boolean result = ReflectionUtils.isLong(type);
boolean result = ReflectionUtils.isLong(clazz);

Is Float

Returns true if the value or type is Float.

boolean result = ReflectionUtils.isFloat(t);
boolean result = ReflectionUtils.isFloat(type);
boolean result = ReflectionUtils.isFloat(clazz);

Is BigDecimal

Returns true if the value or type is BigDecimal.

boolean result = ReflectionUtils.isBigDecimal(t);
boolean result = ReflectionUtils.isBigDecimal(type);
boolean result = ReflectionUtils.isBigDecimal(clazz);

Is Byte

Returns true if the value or type is Byte.

boolean result = ReflectionUtils.isByte(t);
boolean result = ReflectionUtils.isByte(type);
boolean result = ReflectionUtils.isByte(clazz);

Is Boolean

Returns true if the value or type is Boolean.

boolean result = ReflectionUtils.isBoolean(t);
boolean result = ReflectionUtils.isBoolean(type);
boolean result = ReflectionUtils.isBoolean(clazz);

Is String

Returns true if the value or type is String.

boolean result = ReflectionUtils.isString(t);
boolean result = ReflectionUtils.isString(type);
boolean result = ReflectionUtils.isString(clazz);

Is Enum

Returns true if the value or type is an enum.

boolean result = ReflectionUtils.isEnum(t);
boolean result = ReflectionUtils.isEnum(type);
boolean result = ReflectionUtils.isEnum(clazz);

Is Boxed

Returns true if the type is a boxed primitive wrapper (e.g., Integer, Boolean). Determined by the presence of a TYPE field in the java.lang package. Parameterized types and raw primitives always return false.

boolean result = ReflectionUtils.isBoxed(type);
boolean result = ReflectionUtils.isBoxed(clazz);

Is List

Returns true if the value or type is assignable from List.

boolean result = ReflectionUtils.isList(t);
boolean result = ReflectionUtils.isList(type);
boolean result = ReflectionUtils.isList(clazz);

Is Map

Returns true if the value or type is assignable from Map.

boolean result = ReflectionUtils.isMap(t);
boolean result = ReflectionUtils.isMap(type);
boolean result = ReflectionUtils.isMap(clazz);

Is Array

Returns true if the value or type is an array.

boolean result = ReflectionUtils.isArray(t);
boolean result = ReflectionUtils.isArray(type);
boolean result = ReflectionUtils.isArray(clazz);

Is Set

Returns true if the value or type is assignable from Set.

boolean result = ReflectionUtils.isSet(t);
boolean result = ReflectionUtils.isSet(type);
boolean result = ReflectionUtils.isSet(clazz);

Is Collection

Returns true if the value or type is assignable from Collection.

boolean result = ReflectionUtils.isCollection(t);
boolean result = ReflectionUtils.isCollection(type);
boolean result = ReflectionUtils.isCollection(clazz);

Is Iterable

Returns true if the value or type is assignable from Iterable.

boolean result = ReflectionUtils.isIterable(t);
boolean result = ReflectionUtils.isIterable(type);
boolean result = ReflectionUtils.isIterable(clazz);

Has No-Args Constructor

Returns true if the class declares only a no-argument constructor. Returns false for parameterized types and null inputs.

boolean result = ReflectionUtils.hasNoArgsConstructor(type);
boolean result = ReflectionUtils.hasNoArgsConstructor(clazz);

Get Field Value

Reads the value of a field from a target object, bypassing access modifiers.

Object value = ReflectionUtils.getFieldValue(field, target);

License

MIT