diff --git a/README.md b/README.md index 2c0d9be..748bcf9 100644 --- a/README.md +++ b/README.md @@ -1,202 +1,334 @@ -## Reflection Utils +# reflection-utils -A collection of useful reflection utilities. +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. -Get Owner Type +## Dependency + +```xml + + io.ljunggren + reflection-utils + 1.0.0-SNAPSHOT + +``` + +## Usage + +All methods are static. Import the class and call directly: + +```java +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. ```java Type owner = ReflectionUtils.getOwnerType(type); ``` -Is Parameterized +--- + +### Is Parameterized + +Returns `true` if the given type is a `ParameterizedType` (e.g., `List`). ```java -boolean isParameterized = ReflectionUtils.isParameterized(type); +boolean result = ReflectionUtils.isParameterized(type); ``` -Get Parameterized 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. ```java -Type parameterizedType = ReflectionUtils.getParameterizedType(type); +Type[] types = ReflectionUtils.getParameterizedTypes(type); ``` -Get Parameterized Types +--- + +### Get Parameterized Type + +Returns the single type argument of a parameterized type, or `null` if there is not exactly one type argument. ```java -Type[] parameterizedTypes = ReflectionUtils.getParameterizedTypes(type); +Type type = ReflectionUtils.getParameterizedType(type); ``` -Get Array Component Type +--- + +### Get Array Component Type + +Returns the component type of an array type, or `null` if the type is not an array. ```java Type componentType = ReflectionUtils.getArrayComponentType(type); ``` -Get Object Fields +--- + +### Get Object Fields + +Returns all fields declared on the class and its superclasses, including private fields. ```java List fields = ReflectionUtils.getObjectFields(clazz); ``` -Is Primitive +--- + +### Is Primitive + +Returns `true` if the type represents a Java primitive (e.g., `int`, `boolean`). Parameterized types always return `false`. ```java -boolean isPrimitive = ReflectionUtils.isPrimitive(type); -boolean isPrimitive = ReflectionUtils.isPrimitive(clazz); +boolean result = ReflectionUtils.isPrimitive(type); +boolean result = ReflectionUtils.isPrimitive(clazz); ``` -Is Number +--- + +### Is Number + +Returns `true` if the value or type is assignable from `Number`. ```java -boolean isNumber = ReflectionUtils.isNumber(t); -boolean isNumber = ReflectionUtils.isNumber(type); -boolean isNumber = ReflectionUtils.isNumber(clazz); +boolean result = ReflectionUtils.isNumber(t); // instance check +boolean result = ReflectionUtils.isNumber(type); // Type overload +boolean result = ReflectionUtils.isNumber(clazz); // Class overload ``` -Is Integer +--- + +### Is Integer + +Returns `true` if the value or type is `Integer`. ```java -boolean isInteger = ReflectionUtils.isInteger(t); -boolean isInteger = ReflectionUtils.isInteger(type); -boolean isInteger = ReflectionUtils.isInteger(clazz); +boolean result = ReflectionUtils.isInteger(t); +boolean result = ReflectionUtils.isInteger(type); +boolean result = ReflectionUtils.isInteger(clazz); ``` -Is Double +--- + +### Is Double + +Returns `true` if the value or type is `Double`. ```java -boolean isDouble = ReflectionUtils.isDouble(t); -boolean isDouble = ReflectionUtils.isDouble(type); -boolean isDouble = ReflectionUtils.isDouble(clazz); +boolean result = ReflectionUtils.isDouble(t); +boolean result = ReflectionUtils.isDouble(type); +boolean result = ReflectionUtils.isDouble(clazz); ``` -Is Short +--- + +### Is Short + +Returns `true` if the value or type is `Short`. ```java -boolean isShort = ReflectionUtils.isShort(t); -boolean isShort = ReflectionUtils.isShort(type); -boolean isShort = ReflectionUtils.isShort(clazz); +boolean result = ReflectionUtils.isShort(t); +boolean result = ReflectionUtils.isShort(type); +boolean result = ReflectionUtils.isShort(clazz); ``` -Is Long +--- + +### Is Long + +Returns `true` if the value or type is `Long`. ```java -boolean isLong = ReflectionUtils.isLong(t); -boolean isLong = ReflectionUtils.isLong(type); -boolean isLong = ReflectionUtils.isLong(clazz); +boolean result = ReflectionUtils.isLong(t); +boolean result = ReflectionUtils.isLong(type); +boolean result = ReflectionUtils.isLong(clazz); ``` -Is Float +--- + +### Is Float + +Returns `true` if the value or type is `Float`. ```java -boolean isFloat = ReflectionUtils.isFloat(t); -boolean isFloat = ReflectionUtils.isFloat(type); -boolean isFloat = ReflectionUtils.isFloat(clazz); +boolean result = ReflectionUtils.isFloat(t); +boolean result = ReflectionUtils.isFloat(type); +boolean result = ReflectionUtils.isFloat(clazz); ``` -Is Big Decimal +--- + +### Is BigDecimal + +Returns `true` if the value or type is `BigDecimal`. ```java -boolean isBigDecimal = ReflectionUtils.isBigDecimal(t); -boolean isBigDecimal = ReflectionUtils.isBigDecimal(type); -boolean isBigDecimal = ReflectionUtils.isBigDecimal(clazz); +boolean result = ReflectionUtils.isBigDecimal(t); +boolean result = ReflectionUtils.isBigDecimal(type); +boolean result = ReflectionUtils.isBigDecimal(clazz); ``` -Is Byte +--- + +### Is Byte + +Returns `true` if the value or type is `Byte`. ```java -boolean isByte = ReflectionUtils.isByte(t); -boolean isByte = ReflectionUtils.isByte(type); -boolean isByte = ReflectionUtils.isByte(clazz); +boolean result = ReflectionUtils.isByte(t); +boolean result = ReflectionUtils.isByte(type); +boolean result = ReflectionUtils.isByte(clazz); ``` -Is Boolean +--- + +### Is Boolean + +Returns `true` if the value or type is `Boolean`. ```java -boolean isBoolean = ReflectionUtils.isBoolean(t); -boolean isBoolean = ReflectionUtils.isBoolean(type); -boolean isBoolean = ReflectionUtils.isBoolean(clazz); +boolean result = ReflectionUtils.isBoolean(t); +boolean result = ReflectionUtils.isBoolean(type); +boolean result = ReflectionUtils.isBoolean(clazz); ``` -Is String +--- + +### Is String + +Returns `true` if the value or type is `String`. ```java -boolean isString = ReflectionUtils.isString(t); -boolean isString = ReflectionUtils.isString(type); -boolean isString = ReflectionUtils.isString(clazz); +boolean result = ReflectionUtils.isString(t); +boolean result = ReflectionUtils.isString(type); +boolean result = ReflectionUtils.isString(clazz); ``` -Is Enum +--- + +### Is Enum + +Returns `true` if the value or type is an enum. ```java -boolean isEnum = ReflectionUtils.isEnum(t); -boolean isEnum = ReflectionUtils.isEnum(type); -boolean isEnum = ReflectionUtils.isEnum(clazz); +boolean result = ReflectionUtils.isEnum(t); +boolean result = ReflectionUtils.isEnum(type); +boolean result = ReflectionUtils.isEnum(clazz); ``` -Is Boxed +--- + +### 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`. ```java -boolean isBoxed = ReflectionUtils.isBoxed(type); -boolean isBoxed = ReflectionUtils.isBoxed(clazz); +boolean result = ReflectionUtils.isBoxed(type); +boolean result = ReflectionUtils.isBoxed(clazz); ``` -Is List +--- + +### Is List + +Returns `true` if the value or type is assignable from `List`. ```java -boolean isList = ReflectionUtils.isList(t); -boolean isList = ReflectionUtils.isList(type); -boolean isList = ReflectionUtils.isList(clazz); +boolean result = ReflectionUtils.isList(t); +boolean result = ReflectionUtils.isList(type); +boolean result = ReflectionUtils.isList(clazz); ``` -Is Map +--- + +### Is Map + +Returns `true` if the value or type is assignable from `Map`. ```java -boolean isMap = ReflectionUtils.isMap(t); -boolean isMap = ReflectionUtils.isMap(type); -boolean isMap = ReflectionUtils.isMap(clazz); +boolean result = ReflectionUtils.isMap(t); +boolean result = ReflectionUtils.isMap(type); +boolean result = ReflectionUtils.isMap(clazz); ``` -Is Array +--- + +### Is Array + +Returns `true` if the value or type is an array. ```java -boolean isArray = ReflectionUtils.isArray(t); -boolean isArray = ReflectionUtils.isArray(type); -boolean isArray = ReflectionUtils.isArray(clazz); +boolean result = ReflectionUtils.isArray(t); +boolean result = ReflectionUtils.isArray(type); +boolean result = ReflectionUtils.isArray(clazz); ``` -Is Set +--- + +### Is Set + +Returns `true` if the value or type is assignable from `Set`. ```java -boolean isSet = ReflectionUtils.isSet(t); -boolean isSet = ReflectionUtils.isSet(type); -boolean isSet = ReflectionUtils.isSet(clazz); +boolean result = ReflectionUtils.isSet(t); +boolean result = ReflectionUtils.isSet(type); +boolean result = ReflectionUtils.isSet(clazz); ``` -Is Collection +--- + +### Is Collection + +Returns `true` if the value or type is assignable from `Collection`. ```java -boolean isCollection = ReflectionUtils.isCollection(t); -boolean isCollection = ReflectionUtils.isCollection(type); -boolean isCollection = ReflectionUtils.isCollection(clazz); +boolean result = ReflectionUtils.isCollection(t); +boolean result = ReflectionUtils.isCollection(type); +boolean result = ReflectionUtils.isCollection(clazz); ``` -Is Iterable +--- + +### Is Iterable + +Returns `true` if the value or type is assignable from `Iterable`. ```java -boolean isIterable = ReflectionUtils.isIterable(t); -boolean isIterable = ReflectionUtils.isIterable(type); -boolean isIterable = ReflectionUtils.isIterable(clazz); +boolean result = ReflectionUtils.isIterable(t); +boolean result = ReflectionUtils.isIterable(type); +boolean result = ReflectionUtils.isIterable(clazz); ``` -Has No Args Constructor +--- + +### Has No-Args Constructor + +Returns `true` if the class declares only a no-argument constructor. Returns `false` for parameterized types and `null` inputs. ```java -boolean hasNoArgsConstructor = ReflectionUtils.hasNoArgsConstructor(type); -boolean hasNoArgsConstructor = ReflectionUtils.hasNoArgsConstructor(clazz); +boolean result = ReflectionUtils.hasNoArgsConstructor(type); +boolean result = ReflectionUtils.hasNoArgsConstructor(clazz); ``` -Get Field Value +--- + +### Get Field Value + +Reads the value of a field from a target object, bypassing access modifiers. ```java -Object value = ReflectionUtils.getFieldValue(field, object); +Object value = ReflectionUtils.getFieldValue(field, target); ``` + +## License + +MIT