|
|
@@ -15,12 +15,22 @@ import java.util.Optional;
|
|
|
import java.util.function.Function;
|
|
|
|
|
|
/**
|
|
|
+ * Utility methods for type transformation
|
|
|
*
|
|
|
* @author Ranides Atterwim {@literal <ranides@gmail.com>}
|
|
|
*/
|
|
|
@UtilityClass
|
|
|
public final class ClassUtils {
|
|
|
|
|
|
+ /**
|
|
|
+ * Tries to cast provided object. Returns none if impossible.
|
|
|
+ * Does not throw ClassCastException
|
|
|
+ *
|
|
|
+ * @param type type
|
|
|
+ * @param value value
|
|
|
+ * @param <T> T
|
|
|
+ * @return optional
|
|
|
+ */
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public static <T> Optional<T> cast(IClass<T> type, Object value) {
|
|
|
if(value != null && type.isInstance(value)) {
|
|
|
@@ -30,6 +40,15 @@ public final class ClassUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Tries to cast provided object. Returns none if impossible.
|
|
|
+ * Does not throw ClassCastException
|
|
|
+ *
|
|
|
+ * @param type type
|
|
|
+ * @param value value
|
|
|
+ * @param <T> T
|
|
|
+ * @return optional
|
|
|
+ */
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public static <T> Optional<T> cast(Class<T> type, Object value) {
|
|
|
if(value != null && type.isInstance(value)) {
|
|
|
@@ -39,28 +58,78 @@ public final class ClassUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns function which casts object into type.
|
|
|
+ * Function does not throw ClassCastException but returns empty optional if cast is impossible.
|
|
|
+ *
|
|
|
+ * @param type type
|
|
|
+ * @param <T> T
|
|
|
+ * @return Function
|
|
|
+ */
|
|
|
public static <T> Function<Object, Optional<T>> cast(IClass<T> type) {
|
|
|
return value -> cast(type, value);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns function which casts object into type.
|
|
|
+ * Function does not throw ClassCastException but returns empty optional if cast is impossible.
|
|
|
+ *
|
|
|
+ * @param type type
|
|
|
+ * @param <T> T
|
|
|
+ * @return Function
|
|
|
+ */
|
|
|
public static <T> Function<Object, Optional<T>> cast(Class<T> type) {
|
|
|
return value -> cast(type, value);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * If "source" is subclass of "target" returns target class.
|
|
|
+ * Returns empty optional otherwise.
|
|
|
+ *
|
|
|
+ * @param target target
|
|
|
+ * @param source source
|
|
|
+ * @param <T> T
|
|
|
+ * @return Optional
|
|
|
+ */
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
public static <T> Optional<Class<? extends T>> asSubclass(Class<T> target, Class<?> source) {
|
|
|
return (Optional)Optional.ofNullable(source).filter(target::isAssignableFrom);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * If "source" is subclass of "target" returns target class.
|
|
|
+ * Returns empty optional otherwise.
|
|
|
+ *
|
|
|
+ * @param target target
|
|
|
+ * @param source source
|
|
|
+ * @param <T> T
|
|
|
+ * @return Optional
|
|
|
+ */
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
public static <T> Optional<IClass<? extends T>> asSubclass(IClass<T> target, IClass<?> source) {
|
|
|
return (Optional)Optional.ofNullable(source).filter(target::isSuper);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * If this class is primitive type, returns boxed type.
|
|
|
+ * Returns input parameter otherwise.
|
|
|
+ *
|
|
|
+ * @param ic ic
|
|
|
+ * @param <S> S
|
|
|
+ * @return class
|
|
|
+ */
|
|
|
public static <S> IClass<S> box(IClass<S> ic) {
|
|
|
return ic.attributes().has(IAttribute.PRIMITIVE) ? IClass.typeinfo(box(ic.raw())) : ic;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * If this class is primitive type, returns boxed type.
|
|
|
+ * Returns input parameter otherwise.
|
|
|
+ *
|
|
|
+ * @param clazz clazz
|
|
|
+ * @param <S> S
|
|
|
+ * @return class
|
|
|
+ */
|
|
|
@SuppressWarnings({"PMD", "unchecked"})
|
|
|
public static <S> Class<S> box(Class<S> clazz) {
|
|
|
if (byte.class.equals(clazz)) {
|
|
|
@@ -85,11 +154,27 @@ public final class ClassUtils {
|
|
|
return clazz;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * If this class is boxed primitive type, returns unboxed type.
|
|
|
+ * Returns input parameter otherwise.
|
|
|
+ *
|
|
|
+ * @param ic ic
|
|
|
+ * @param <S> S
|
|
|
+ * @return class
|
|
|
+ */
|
|
|
public static <S> IClass<S> unbox(IClass<S> ic) {
|
|
|
return ic.attributes().has(IAttribute.BOXED) ? IClass.typeinfo(unbox(ic.raw())) : ic;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * If this class is boxed primitive type, returns unboxed type.
|
|
|
+ * Returns input parameter otherwise.
|
|
|
+ *
|
|
|
+ * @param clazz clazz
|
|
|
+ * @param <S> S
|
|
|
+ * @return class
|
|
|
+ */
|
|
|
@SuppressWarnings({"PMD", "unchecked"})
|
|
|
public static <S> Class<S> unbox(Class<S> clazz) {
|
|
|
if (Byte.class.equals(clazz)) {
|
|
|
@@ -116,8 +201,9 @@ public final class ClassUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sprawdza, czy podany obiekt {@code get} jest instancją klasy {@code ssuper}.
|
|
|
- * Uwzględnia autoboxing argumentów.
|
|
|
+ * Checks if provided object is instance of "type"
|
|
|
+ * It supports autoboxing.
|
|
|
+ *
|
|
|
* @param type type
|
|
|
* @param value value
|
|
|
* @return boolean
|
|
|
@@ -127,11 +213,12 @@ public final class ClassUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sprawdza, czy odpowiednie obiekty z tablicy {@code values} są istancjami
|
|
|
- * odpowiednich klas z tablicy {@code supers}. Uwzględnia autoboxing argumentów.
|
|
|
- * @param type
|
|
|
- * @param values
|
|
|
- * @return
|
|
|
+ * Checks if objects from provided arrays are instance of according type from "type" array.
|
|
|
+ * It supports autoboxing.
|
|
|
+ *
|
|
|
+ * @param type type
|
|
|
+ * @param values values
|
|
|
+ * @return boolean
|
|
|
*/
|
|
|
public static boolean isInstance(Class<?>[] type, Object[] values) {
|
|
|
if(type.length != values.length) {
|
|
|
@@ -143,6 +230,12 @@ public final class ClassUtils {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns true if default classloader already loaded class with provided fully qualified name.
|
|
|
+ *
|
|
|
+ * @param name name
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
public static boolean isLoaded(String name) {
|
|
|
for(ClassLoader c = ClassLoaderUtils.getDefault(); c!=null; c=c.getParent()) {
|
|
|
if(null != MethodUtils.invoke(Li.FIND_LOADED_CLASS, c, name) ) {
|
|
|
@@ -152,6 +245,14 @@ public final class ClassUtils {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Uses default classloader to resolve provided fully qualified name.
|
|
|
+ * Returns empty optional if class is not found.
|
|
|
+ * Does not throw exceptions.
|
|
|
+ *
|
|
|
+ * @param type type
|
|
|
+ * @return Optional
|
|
|
+ */
|
|
|
public static Optional<Class<?>> forName(String type) {
|
|
|
try {
|
|
|
return Optional.of(Class.forName(type, true, ClassLoaderUtils.getDefault()));
|