com.github.parisoft.resty.utils.ObjectUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of resty Show documentation
Show all versions of resty Show documentation
A minimalist HTTP client made on top of Apache HttpComponets and Jackson Data Processor
package com.github.parisoft.resty.utils;
import static com.github.parisoft.resty.utils.ReflectionUtils.containsConstructor;
import static com.github.parisoft.resty.utils.ReflectionUtils.containsMethod;
import static com.github.parisoft.resty.utils.ReflectionUtils.getConstructor;
import static com.github.parisoft.resty.utils.ReflectionUtils.getMethod;
@SuppressWarnings("unchecked")
public class ObjectUtils {
private static final String METHOD_VALUE_OF_STRING = "valueOf";
public static boolean isInstanciableFromString(Class> someClass) {
return containsConstructor(someClass, String.class)
|| containsMethod(someClass, METHOD_VALUE_OF_STRING, String.class);
}
public static T newInstanceFromString(Class someClass, String string) {
if (containsConstructor(someClass, String.class)) {
try {
return getConstructor(someClass, String.class).newInstance(string);
} catch (Exception ignored) {
}
}
if (containsMethod(someClass, METHOD_VALUE_OF_STRING, String.class)) {
try {
return (T) getMethod(someClass, METHOD_VALUE_OF_STRING, String.class).invoke(null, string);
} catch (Exception ignored) {
}
}
return null;
}
public static boolean isPrimitive(Object object) {
return object.getClass().isPrimitive();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy